Working with Value Types Variables

SheoNarayan
Posted by in C# category on for Intermediate level | Views : 6070 red flag

This article describes about most of the Value Types variables, its uses and how to use them.
Introduction
Variables are of two types. Reference and Value types. In this article, we are going to cover Value Types variables. Value types are variables that contain their actual data directly instead of reference of the data stored elsewhere in the memory. Instances of value types variables are stored in an area of memory called Stack. Stack is a place where runtime can create, read, update and remove the data quickly.

Value types variables can be of two types. Build-in and User defined. Lets see each one of them one by one.
Built-in Value Types Variables

There are many built-in value types variables and we can use them based on our requirement.

Type

Byte

Range

Used for

sbyte
(System.SByte)

1

-128 to 127

Signed byte values

byte
(System.Byte)

1

0 to 255

Unsigned byte

short
(System.Int16)

2

-32768 to 32767

To store small numbers

int
(System.Int32)

4

–2147483648 to 2147483647

Whole numbers

uint
(System.UInt32)

4

0 to 4294967295

Only Positive whole numbers

long
(System.Int64)

8

–9223372036854775808 to 9223372036854770000

Large whole numbers

float
(System.Single)

4

–3.402823E+38 to 3.402823E+38

Floating point numbers

double
(System.Double)

8

–1.79769313486232E+308 to 1.79769313486232E+308

Precise or large floating numbers

decimal
(System.Decimal)

16

–79228162514264337593543950335 to 79228162514264337593543950335

Financial and scientific calculations


Type

Byte

Range

Used for

char
(System.Char)

2

Single unicode character

Boolean
(System.Boolean)

4

True/False

DateTime
(System.DateTime)

8

1/1/0001 12:00:00 AM to 12/31/9999 11:59:59 PM

To store date time

Use of int and uint for variables are recommended as these variables are optimzed by runtime. For floating point use double for the same reason.


Declaring Value Types Variables

Declaring simple variable

Declaring value types variables are not very tough. You can declare by specifying the name of the data type like below.

int i = 0;

Here int is the data type of the variable, i is the name of the variable and 0 is the default value I have used to initialize the variable.

Declaring nullable variables

There are situation where you don't want anything to store in the variable, in that case you can go ahead with nullable variables.

Nullable<int> i = null;
int? ii = null;

Note that by default you can't assign null to a Value types variables, so if you are planning to store null in the value types variables, you should declared them as nullable variables.

Now there is a question, how to know whether a nullable variable value has been set or not? To do that use use following code.

// to detech, use HashValue
if (i.HashValue)
{
   // variable value has been set
   int i = i.Value; // get the value using i.Value
}

 

User Defined Types  - Struct

User-defined types are also called sructs. According to MSDN, A struct type is a value type that can contain constructors, constants, fields, methods, properties, indexers, operators, events, and nested types. As with other value types, instances of user-defined types are stored on the stack and they contain their data directly. Structures are a composite of other types that make it easier to work with related data. Structs are used to represent light weight object. Struct can't inherit a class but it can inherit an interface.

struct Person

{

public string firstName;

public string lastName;

public Person(string _firstName, string _lastName)

{

firstName = _firstName;

lastName = _lastName;

}

public override string ToString()

{

return firstName + " : " + lastName;

}

}

To call the above struct, write following code.

Person p = new Person("Sheo", "Narayan");

string myName = p.ToString();

Notice that I have not initialized the firstName and lastName variables in the struct, if you do so it will throw error at compile time(Person.firstName': cannot have instance field initializers in structs, in case you are trying to initialize firstName).

 

Enumerations

Enumerations are group of symbols that have fixed value. The enum keyword is used to declare an enumeration, a distinct type consisting of a set of named constants called the enumerator list.

enum tasteType

{

hot,

sour,

sweet,

salty

}

to call this enumerations, use following code.

tasteType.hot

Enumerations simplifies the coding by providing limited set of options in readable and understandable forat to the developers for a particular object.

Conclusion
In this article, we have gone through almost all Value types variables that are generally used while developing applications. Hope you enjoyed this. Thanks for reading.

Page copy protected against web site content infringement by Copyscape

About the Author

SheoNarayan
Full Name: Sheo Narayan
Member Level: HonoraryPlatinum
Member Status: Administrator
Member Since: 7/8/2008 6:32:14 PM
Country: India
Regards, Sheo Narayan http://www.dotnetfunda.com
http://www.snarayan.com
Ex-Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001. Connect me on http://www.facebook.com/sheo.narayan | https://twitter.com/sheonarayan | http://www.linkedin.com/in/sheonarayan

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)