Data Types in C# .NET

In this section I’ll discuss here about value types, initialization, type information and type conversion:

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 = "Minhajur Rahman Khan";
char grade = 'B';
DateTime today = DateTime.Parse("07/04/2008 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)

Add a Comment