Tutorial by Examples

// assign string from a string literal string s = "hello"; // assign string from an array of characters char[] chars = new char[] { 'h', 'e', 'l', 'l', 'o' }; string s = new string(chars, 0, chars.Length); // assign string from a char pointer, derived from a string string s; uns...
// single character s char c = 's'; // character s: casted from integer value char c = (char)115; // unicode character: single character s char c = '\u0073'; // unicode character: smiley face char c = '\u263a';
// assigning a signed short to its minimum value short s = -32768; // assigning a signed short to its maximum value short s = 32767; // assigning a signed int to its minimum value int i = -2147483648; // assigning a signed int to its maximum value int i = 2147483647; // assigning a s...
// assigning an unsigned short to its minimum value ushort s = 0; // assigning an unsigned short to its maximum value ushort s = 65535; // assigning an unsigned int to its minimum value uint i = 0; // assigning an unsigned int to its maximum value uint i = 4294967295; // assigning an...
// default value of boolean is false bool b; //default value of nullable boolean is null bool? z; b = true; if(b) { Console.WriteLine("Boolean has true value"); } The bool keyword is an alias of System.Boolean. It is used to declare variables to store the Boolean values, true...
If value types are assigned to variables of type object they are boxed - the value is stored in an instance of a System.Object. This can lead to unintended consequences when comparing values with ==, e.g.: object left = (int)1; // int in an object box object right = (int)1; // int in an object bo...
Boxed value types can only be unboxed into their original Type, even if a conversion of the two Types is valid, e.g.: object boxedInt = (int)1; // int boxed in an object long unboxedInt1 = (long)boxedInt; // invalid cast This can be avoided by first unboxing into the original Type, e.g.: lon...

Page 1 of 1