Go to DotNetFunda.com
 Welcome, Guest!  
LoginLogin  
Expose your profile for FREE in Xpose
Submit: Article | Interview Question | Code | Question | Xpose | Joke | Link || Search  
 Skip Navigation Links Home > Articles > Working with Value Types Variables

All Articles | Post Articles |  Subscribe to RSS

Working with Value Types Variables

 Posted on: 9/20/2008 10:06:20 PM by SheoNarayan | Views: 734 | Category: C# | Level: Intermediate | Print Article
ASP.NET 3.5 Hosting and MS SQL 2008!
This article describes about most of the Value Types variables, its uses and how to use them.

Advertisement

Showcase
Are you an employee, employer or a service provider? Showcase your profile in Xpose section to get better opportunity.

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.


Interesting?  Bookmark and Share kick it on DotNetKicks.com


About Sheo Narayan

Experience:6 year(s)
Home page:http://sheonarayan.myfunda.net/
Member since:Tuesday, July 08, 2008
Biography:Throughout 1st in all educational exams.
Major qualifications: HDCS, ADCA, MCA, MCTS
Location: Hyderabad, India
 Latest post(s) from SheoNarayan

   ◘ Consuming ASP.NET Web Service in Silverlight posted on 1/5/2009 3:56:01 AM
   ◘ How to work with multiple xaml file in a single Silverlight application posted on 1/5/2009 12:13:48 AM
   ◘ Before sending an official email posted on 12/29/2008 11:04:19 PM
   ◘ Creating dynamic image from text posted on 12/21/2008 6:12:05 AM
   ◘ Classes in C# posted on 11/18/2008 7:44:13 PM




About Us | Contact Us | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
All rights reserved to DotNetFunda.Com. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks.
(Best viewed in IE 6.0+ or Firefox 2.0+ at 1024 * 768 or higher)