Tuesday, July 2, 2013

Built-in Data Types

Built-in Data Types

Data Types are very important in C# language before storing a value in the variable/memory it needs to be specified a type. click on every Data Type to see example.
Data Type Range
byte 0 to 255
sbyte -128 to 127
short -32,768 to 32,767
ushort 0 to 65,535
int -2,147,483,648 to 2,147,483,647
uint 0 to 4,294,967,295
long -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
ulong 0 to 18,446,744,073,709,551,615
float -3.402823e38 to 3.402823e38
double -1.79769313486232e308 to 1.79769313486232e308
decimal -79228162514264337593543950335 to 79228162514264337593543950335
char A Unicode character.
string A string of Unicode characters.
bool True or False.
object An object.
        

byte(0 to 255)

byte x = 200; //Ok byte y = 256;//Error: Constant value '256' cannot be converted to a 'byte' byte x = 10, y = 20; byte z = x + y; //Error: Cannot implicitly convert type 'int' to 'byte'. An explicit conversion exists (are you missing a cast?)
 

sbyte(-128 to 127)

sbyte num = 255;//Error: Constant value '255' cannot be converted to a 'sbyte' Solution: sbyte can store -128 to 127 //You can save sbyte value in integer value directly as shown below sbyte num = 127; int tes = num;
        

short(-32,768 to 32,767)

short num = 32768;//Error: Constant value '32768' cannot be converted to a 'short' Solution: short can store -32,768 to 32,767 //You can save sbyte value in integer value directly as shown below short x = 32767, y = 32767; int tes = x + y; short z = tes;//Error: Cannot implicitly convert type 'int' to 'short'. An explicit conversion exists (are you missing a cast?) short z = (short)tes;//65534 converting into short OutPut: -2 This is going cycling order.... no stop i mean positive values to negative values keep going :).
        

ushort(0 to 65,535)

ushort is Unsinged 16-bit integer ushort num = 65535;//this is good ushort num = 65536;//Error: Constant value '65536' cannot be converted to a 'ushort' Solution: ushort limits are 0 to 65,535 ushort x = 5, y = 6; ushort z = x + y;//Error: Cannot implicitly convert type 'int' to 'ushort'. An explicit conversion exists (are you missing a cast?) Solution: Addition of two ushort values will funtion with int data type so we have compile error. ushort z = (ushort)(x + y);//OK

INT (-2,147,483,648 to 2,147,483,647)

The int keyword denotes an integral type int i = 100; //Declare and initialize a variable of the type long vallong = 22; int i = vallong;//Cannot implicitly convert type 'long' to 'int'. An explicit conversion exists (are you missing a cast?) int i = (int)vallong; //Good
        

Unit(0 to 4,294,967,295)

Unit num = 123;//Ok long vallong = 123; Unit num = vallong;//Cannot implicitly convert type 'long' to 'System.Web.UI.WebControls.Unit'. Unit num = (Unit)vallong;//Ok
        

long(–9,223,372,036,854,775,808 to 9,223,372,036,854,775,807)

long is a integeral type variable is nothing but Signed 64-bit integer. long num = 4294967296;//Ok int i = 123; long j = i; //Ok
        

ulong(0 to 18,446,744,073,709,551,615)

ulong will accept Unsigned 64-bit integer values. ulong num = 9827876347898279057; unsigned value can be long or ulong but it varies depends on the value size. long vallong = 9827876347898279057; //Cannot implicitly convert type 'ulong' to 'long'. The above value is too big to be stored in long data type and use ulong to store this type of big values. ulong num = 100.0;//Error: Cannot implicitly convert type 'double' to 'ulong'. ulong num = (ulong)100.0; //Ok
        

float (-3.402823e38 to 3.402823e38)

The float keyword signifies a simple type that stores 32-bit floating-point values. Usaually the right side operator is treated as double. So specify f or F to initialize float variable as shown below float num = 1.2f; A floating-point expression can contain the following sets of values: •Positive and negative zero •Positive and negative infinity •Not-a-Number value (NaN) •The finite set of nonzero values
        

double(-1.79769313486232e308 to 1.79769313486232e308)

double is value with 15-16 digits it stores more value than float initialize the double as shown below double num = 3.0; A floating-point expression can contain the following sets of values: •Positive and negative zero. •Positive and negative infinity. •Not-a-Number value (NaN). •The finite set of nonzero values.
        

decimal(-79228162514264337593543950335 to 79228162514264337593543950335)

The decimal keyword denotes a 128-bit data type. Compared to floating-point types, the decimal type has a greater precision and a smaller range, which makes it suitable for financial and monetary calculations. The approximate range and precision for the decimal type are shown in the following table. decimal num = 100.3m;//If you want a numeric real literal to be treated as decimal, use the suffix m or M decimal num = 3.5;//Literal of type double cannot be implicitly converted to type 'decimal'; use an 'M' suffix to create a literal of this type decimal num = 3.5m;//Ok
        

char(A Unicode character.)

The char keyword is used to declare an instance of the System.Char structure that the .NET Framework uses to represent a Unicode character. The value of a Char object is a 16-bit numeric (ordinal) value. char x = 'A'; char y = 'ABC';//Error: Too many characters in character literal char x = 100;//Error: Constant value '100' cannot be converted to a 'char' char x = (char)100;//Ok but Output will be d
        

string(A string of Unicode characters.)

The string type represents a sequence of zero or more Unicode characters. string is an alias for String in the .NET Framework. string a = "hello"; string b = "h"; // Append to contents of 'b' b += "ello"; Console.WriteLine(a == b); Console.WriteLine((object)a == (object)b); This displays "True" and then "False" because the content of the strings are equivalent, but a and b do not refer to the same string instance.
        bool(True or False.)
        The bool keyword is an alias of System.Boolean. It is used to declare variables to store the Boolean values, true and false.
        

object(An object.)

The object type is an alias for System.Object in the .NET Framework. You can assign values of any type to variables of type object. All data types, predefined and user-defined, inherit from the System.Object class. The object data type is the type to and from which objects are boxed.

No comments:

Post a Comment