#26 – Null Literal

The keyword null represents a null literal.  A null value indicates that a reference points to no object.

Examples:

 object o1 = null;
 if (o1 == null) Console.WriteLine("Yup, it's null");

 string s1 = null;    // Strings can also be null
 int n1 = s1.Length;  // Throws NullReferenceException, since s1 doesn't point to anything
Advertisements

#24 – Character Literals

There are several different ways of specifying single character literals in C#.  Each character is a 2-byte Unicode character and is of type char.

Here are some examples of character literals:

char c1 = 'A';
char c2 = '^';
char c3 = '\'';    // Escape: single quote
char c4 = '\\';    // Escape: backslash
char c5 = '\x45';  // Hex code for ASCII 'E'
char c6 = '\x20AC';  // Hex code for Euro sign
char c7 = '€';     // Euro sign directly in code

#23 – Real Literals

There are several ways to indicate real number (floating point) literals in C#.  Real literals are assumed to be of type double, but may also be of type float or decimal, depending on the suffix included with the literal.

Suffix / type:

  • f – float
  • d – double
  • m – decimal

Here are some examples:

 // Suffixes
 double d1 = 1.0;    // ok
 object o1 = 1.0;    // Also double
 float f1 = 3f;      // float
 float f2 = 1.0f;    // Must have 'f' suffix for float
 double d2 = 1.0d;   // Optional 'd' suffix
 decimal d3 = 1.0m;

 // Exponents
 double d4 = 1.2E3;    // 1200
 double d5 = 1.2E+3;   // 1200
 double d6 = 1.2E-3;   // 0.0012
 double d7 = 2E3;      // 2000
 double d8 = 2E-3;     // 0.002
 float f3 = 2E-3f;
 decimal d9 = 2E-3m;

#22 – Integer Literals

There are several ways to specify integer literals in C#.  The type is inferred from the value specified.  The type used will be the first type in the following list that the literal fits into: int, uint, long or ulong.  A suffix can also be used to help indicate the type of the literal.

Types chosen based on suffix (in order preferred):

  • u suffix – uint, ulong
  • l suffix – long, ulong
  • ul suffix – ulong

Here are a few examples:

// Decimal literals, suffixes
object o1 = 42;           // int
object o2 = 2147483647;   // int
object o3 = 2147483648;   // uint
uint u1 = 2;              // uint
object u2 = 100U;         // uint
object o4 = 4294967296;   // long
object o5 = 4294967296U;  // ulong
object o6 = 42L;          // long
object o7 = 9223372036854775808L;  // ulong
object o8 = 42UL;         // ulong
int n1 = 003;

// Hexadecimal literals
int x1 = 0x1A2;     // 418
object o12 = 0xFFFFFFFF;   // 4294967295 uint
object o13 = 0xFUL;   // 15, ulong

Lowercase suffixes are allowed, but you should use uppercase for clarity.

#21 – Boolean Literals

A boolean literal is represented by one of two keywords–either true or false.  A boolean literal is always of type bool.

Here are a few examples:

 bool happy = true;
 bool rich = false;
 if (happy == true) Console.WriteLine("Happy guy");
 File.Copy(@"C:\From.txt", @"C:\To.txt", false);  // don't overwrite
 while (true) Console.WriteLine("Loop forever");

#20 – Literals

A literal in C# is a representation, in your source code, of a constant value.  You can use literals when assigning a value to a variable, when passing a parameter value to a method, or wherever a variable of the corresponding type is expected.

C# defines six different types of literals:

  • Boolean literal:  true | false
  • Integer literal:  e.g. 12, 24L, 100UL, 0x7DC
  • Real literal:  e.g. 1.0, 2.2F, 4, 6.02E23, 123.45m
  • Character literal:  e.g. ‘a’, ‘Z’, ‘#’, ‘\n’, ‘\”, ‘\\’, ‘\x45’, ‘\x04DA’
  • String literal: e.g. “Sean”, @”Print\nMe”, “Look: \x04DA”, “Look: Ӛ”, “Line 1\nLine 2”
  • Null literal:  null