Week 1: Notes

This week's topics are covered in Essential C#: see ch. 1 Introducing C#, ch. 2 Data Types, ch. 3 More with Data Types (Arrays), ch. 4 Operators and Control Flow.

Here is a summary of the C# elements we discussed:

hello, world

using System;

class Hello {
  static void Main() {
    Console.WriteLine("hello, world");
  }
}

local variable declarations

A local variable declaration declares one or more local variables and optionally gives them initial values:

    int i = 3, j = 4;

Implicitly typed variables

When you declare a local variable, you can use the var keyword to tell the compiler to infer its type. For example:

    var s = "distant hill";

This is equivalent to

    string s = "distant hill";

comments

A single-line comment begins with // and extends to the end of the line:

x += 1;    // increment x 

Comments delimited with /* and */ can extend over multiple lines:

/*
  this is a comment with
  multiple lines
*/

integer types

A literal integer (or floating-point value) may contain embedded underscores for readability:

    int i = 1_000_000_000;    // 1 billion

The inferred type of an integer literal is the first of these types which can hold it: int, uint, long, ulong.

floating-point types

The inferred type of a floating-point literal (e.g. 3.4) is double by default. To create a literal of type float, append the character 'f', e.g. 3.4f.

numeric conversions

C# will perform an implicit conversion between two numeric types if every possible value of the source type is valid in the destination type. For example:

    short s;
    ...
    int i = s;   // implicit conversion

You can also implicitly convert a char to any numeric type that can hold a 16-bit unsigned value:

    char c = 'ř';
    int i = c;   // now i holds the Unicode value for 'ř', i.e. 345

You can use an explicit conversion to force a conversion between any two numeric types. This is accomplished with a type cast:

    int i = 1000;
    short s = (short) i;

If the destination type cannot hold the source value, it will wrap around or be truncated.

You can explicitly convert any numeric type to a char:

    int i = 100;
    char c = (char) i;  // now c is 'd', which is ASCII/Unicode character 100

bool

The bool type represents a Boolean value, namely either true or false.

char

A char is a 16-bit Unicode character.

A character constant is enclosed in single quotes, e.g. 'x' or 'ř'. A character constant containing a backslash is an escape sequence. Here are some common escape sequences:

To create a character constant representing a single quote or backslash, use one of the sequences above:

    WriteLine('\\');  // writes a backslash
    WriteLine('\'');  // writes a single quote

string

A string is an immutable sequence of characters. A string literal is enclosed in double quotes, e.g. "hello". Ordinary string literals may contain escape sequences.

You may access individual characters of a string using square brackets:

    string s = "spire";

    char c = s[2];    // now c = 'i'

Note that characters are indexed starting from 0.

interpolated values

A string beginning with $ may contain interpolated values enclosed in braces:

    int a = 3, b = 4;

    WriteLine($"{a} plus {b} equals {a + b}");

A interpolated value may be followed by a format specifier (preceded by a colon).

There are many predefined format specifiers. Many format specifiers can be followed by an integer called the precision, whose meaning varies among specifiers. Here are two useful specifiers:

For example,

    const double pi = 3.14159;
    const int i = 1357;
    
    WriteLine($"{pi:f2} {i:n0}");

writes

    3.14 1,357

verbatim strings

A literal string preceded with @ is a verbatim string, and differs from an ordinary string literal in two ways:

To include a double quote character (") in a verbatim string, type it twice. For example:

string s = @"He said, ""Where is she?"""

Console.WriteLine(s)

prints

He said, "Where is she?"

arrays

You can allocate an array like this:

    int[] i = new int[10];

With this form of allocation all elements are initialized to the element type's default value. For example, the default value for integers is 0.

Alternatively, you can allocate an array and initialize its elements to specific values:

    int[] i = { 3, 4, 5 };

Arrays are indexed from 0. The Length property returns the length of an array.

arithmetic operators

boolean operators

equality operators

relational operators

These operators are defined on numbers and characters, but not on strings.

compound assignment operators

if

    if (i > 0) {
      WriteLine("positive");
      WriteLine("definitely positive");
    } else if (i == 0)
      WriteLine("zero");
    else
      WriteLine("negative");

An if statement executes a statement (or block) if the given value is true. If the statement has an else clause, it is executed if the given value is false.

while

    while (i < 10) {
      sum = sum + i;
      i += 1;
    }

A while loop loops as long as the given condition is true.

do / while

    do {
      s = ReadLine();
    } while (s != "yes" && s != "no");

A do/while loop is like a while loop, but checks the loop condition at the bottom of the loop body.

for

    for (int i = 0 ; i < 10 ; i += 1)
      WriteLine(i);

A for statement contains three clauses, separated by semicolons, plus a loop body.