Monday, March 3, 2008

Data Types of C#.

_____________________________________________

Data Types of C#.

_____________________________________________


Value Types
bool
byte, sbyte
char
short, ushort, int, uint, long, ulong
float, double
decimal
DateTime (not a built-in C# type)

Reference Types
object
string

Initializing
bool correct = true;
byte b = 0x2A; // hex

object person = null;
string name = "Dwight";
char grade = 'B';
DateTime today = DateTime.Parse("12/31/2007 12:15:00");
decimal amount = 35.99m;
float gpa = 2.9f;
double pi = 3.14159265;
long lTotal = 123456L;
short sTotal = 123;
ushort usTotal = 123;
uint uiTotal = 123;
ulong ulTotal = 123;

Type Information
int x;
Console.WriteLine(x.GetType()); // Prints System.Int32
Console.WriteLine(typeof(int)); // Prints System.Int32
Console.WriteLine(x.GetType().Name); // prints Int32

Type Conversion
float d = 3.5f;
int i = (int)d; // set to 3 (truncates decimal)

____________________________________________________________________

Note:Applicable for .net also asp.net.

____________________________________________________________________

No comments: