#30 – Types, Variables, Values, Instances

In C#, a type dictates what kind of values can be stored in a variable.  A variable is a storage location for some data.  Every variable is an instance of a specific type and will have a value that can change during the lifetime of a program.

Constants are variables whose values do not change.  They also have a specific type.

Expressions resolve to a particular value when they are evaluated.  They also have a specific type.

There are a number of built-in types in C#  (e.g. int, float) as well as constructs that allow you to create your own types (e.g. class, enum).

#29 – Comments

Comments in a C# program denote text that is ignored by the compiler and therefore has no effect on the behavior of the program.

There are two types of comments in C#:

  • Single line comments:  start with //, extend to end of line
  • Delimited comments:  surrounded by /*  */, can span multiple lines


 string s1 = "Hi";        // This is a single-line comment

 /*  Multiple line
  *  comment.  Nice convention to use
  *  asterisk at the start of each line. */

 string s2 = "/* This is a string, rather than a comment */";
 string s3 = "// same here";

 // Nested comments /* ignored */

 // Even better style for block comment


#28 – Operator Precedence

Each operator has an associated precedence, which indicates the order in which the operators are evaluated when evaluating the  expression.

When an expression has operators of the same precedence, they are evaluated according to their rules of associativity–left to right for all binary expressions, except for assignment and conditional operators, which are right to left.

Here are the C# operators, in order of precedence.  Operators in each group are of equal precedence.

  • Primary:  x.y  f(x)  a[x]  x++  x–  new  typeof  checked  unchecked
  • Unary:  +  –  !  ~  ++x  –x  (T)x
  • Multiplicative:  *  /  %
  • Additive:  +  –
  • Shift:  <<  >>
  • Relational:  <  >  <=  >=  is  as
  • Equality:  ==  !=
  • Logical AND:  &
  • Logical XOR:  ^
  • Logical OR:  |
  • Conditional AND:  &&
  • Conditional OR:  ||
  • Conditional:  ?:
  • Assignment:  =  *=  /=  %=  +=  -=  <<=  >>=  &=  ^=  |=

Precedence can be changed by using parentheses, the inner expressions being evaluated first.


#27 – Expressions, Operators and Operands

Expressions are sequences of operators and operands.  Evaluating an expression results in a single value for the entire expression.

Operators are the symbols used in expressions, indicating what operations apply to the operands in the expression.

Operands are the values acted upon by the operators, e.g. literals, results of function calls, other expressions.

There are three kinds of operators:

  • Unary – Takes one operand.  E.g.  x++
  • Binary – Takes two operands.  E.g.  x+y
  • Ternary – Takes three operands (the conditional operator).  E.g.  e ? x : y


#26 – Null Literal

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


 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

#25 – String Literals

A string literal in C# represents a sequence of Unicode characters.  There are two main types of string literals in C#–regular string literals and verbatim string literals.  Verbatim string literals allow including special characters in a string directly, rather than having to specify them using an escape sequence.  All string literals are of type string.

Here are some examples of string literals:

 string s1 = "Hi";
 string s2 = @"Hi";       // Verbatim string literal--same thing
 string s3 = "C:\\Dir";   // C:\Dir  (escape seq for backslash)
 string s4 = @"C:\Dir";   // No escape seq required
 string s5 = "\x48\x69";  // Hi  (hex codes for each character)
 string s6 = "\x20AC 1.99";  // € 1.99
 string s7 = "€ 1.99";    // Unicode directly in string

 // UTF-32 characters using surrogate pairs
 string s8 = "\U00020213";      // U+20213 (UTF-32)
 string s9 = "\ud840\ude13";    // Equiv surrogate pair
 string s10 = "𠈓";             // Same character


  • \u is followed by 4-byte UTF16 character code
  • \U is followed by 8-byte UTF32 character code, which is then converted to equivalent surrogate pair

#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