SlideShare a Scribd company logo
Introduction to C#
  The New Language for                .

            H.Mössenböck
      University of Linz, Austria
    moessenboeck@ssw.uni-linz.ac.at
Contents
Introduction to C#                             Advanced C#
1.   Overview                                   7. Inheritance
2.   Types                                      8. Interfaces
3.   Expressions                                9. Delegates
4.   Declarations                               10. Exceptions
5.   Statements                                 11. Namespaces and Assemblies
6.   Classes and Structs                        12. Attributes
                                                13. Threads
                                                14. XML Comments


References:
• B.Albahari, P.Drayton, B.Merrill: C# Essentials. O'Reilly, 2001
• S.Robinson et al: Professional C#, Wrox Press, 2001
• Online documentation on the .NET SDK CD

                                                                                2
Features of C#

    Very similar to Java
    70% Java, 10% C++, 5% Visual Basic, 15% new


As in Java                                     As in C++
•    Object-orientation (single inheritance)   • (Operator) Overloading
•    Interfaces                                • Pointer arithmetic in unsafe code
•    Exceptions                                • Some syntactic details
•    Threads
•    Namespaces (like Packages)
•    Strong typing
•    Garbage Collection
•    Reflection
•    Dynamic loading of code
•    ...


                                                                                     3
New Features in C#

Really new (compared to Java)         "Syntactic Sugar"
•   Reference and output parameters   • Component-based programming
•   Objects on the stack (structs)      - Properties
•   Rectangular arrays                  - Events
•   Enumerations                      • Delegates
•   Unified type system               • Indexers
•   goto                              • Operator overloading
•   Versioning                        • foreach statements
                                      • Boxing/unboxing
                                      • Attributes
                                      • ...




                                                                      4
Hello World

File Hello.cs
using System;
                                           •   uses the namespace System
                                           •   entry point must be called Main
class Hello {
                                           •   output goes to the console
                                           •   file name and class name
    static void Main() {
                                               need not be identical
       Console.WriteLine("Hello World");
    }

}



Compilation (in the Console window)
    csc Hello.cs
Execution
    Hello

                                                                                 5
Structure of C# Programs

                                    Programm


                   File F1.cs         File F2.cs       File F3.cs



         namespace A {...}         namespace B {...}          namespace C {...}



                   class X {...}      class Y {...}    class Z {...}


•   If no namespace is specified => anonymous default namespace
•   Namespaces may also contain structs, interfaces, delegates and enums
•   Namespace may be "reopened" in other files
•   Simplest case: single class, single file, default namespace
                                                                                  6
A Program Consisting of 2 Files
Counter.cs
 class Counter {                                 Compilation
    int val = 0;
    public void Add (int x) { val = val + x; }    csc Counter.cs Prog.cs
    public int Val () { return val; }             => generates Prog.exe
 }
                                                 Execution
Prog.cs                                           Prog
 using System;

 class Prog {
                                                 Working with DLLs
     static void Main() {
        Counter c = new Counter();                csc /target:library Counter.cs
        c.Add(3); c.Add(5);                       => generates Counter.dll
        Console.WriteLine("val = " + c.Val());
     }                                            csc /reference:Counter.dll Prog.cs
 }                                                => generates Prog.exe


                                                                                   7
Types
Unified Type System
                                        Types


                Value Types                               Reference Types         Pointers



Simple Types                  Enums   Structs      Classes Interfaces Arrays Delegates
bool   sbyte   byte     float
char   short   ushort   double
       int     uint     decimal
       long    ulong
                                                User-defined Types



All types are compatible with object
- can be assigned to variables of type object
- all operations of type object are applicable to them


                                                                                         9
Value Types versus Reference Types
                    Value Types        Reference Types
variable contains   value              reference
stored on           stack              heap
initialisation      0, false, '0'     null
assignment          copies the value   copies the reference


example             int i = 17;        string s = "Hello";
                    int j = i;         string s1 = s;

                    i    17             s
                                                             Hello
                    j    17            s1




                                                                     10
Simple Types
          Long Form        in Java   Range
sbyte     System.SByte     byte      -128 .. 127
byte      System.Byte      ---       0 .. 255
short     System.Int16     short     -32768 .. 32767
ushort    System.UInt16    ---       0 .. 65535
int       System.Int32     int       -2147483648 .. 2147483647
uint      System.UInt32    ---       0 .. 4294967295
long      System.Int64     long      -263 .. 263-1
ulong     System.UInt64    ---       0 .. 264-1
float     System.Single    float     ±1.5E-45 .. ±3.4E38 (32 Bit)
double    System.Double    double    ±5E-324 .. ±1.7E308 (64 Bit)
decimal   System.Decimal   ---       ±1E-28 .. ±7.9E28 (128 Bit)
bool      System.Boolean   boolean   true, false
char      System.Char      char      Unicode character


                                                                    11
Compatibility Between Simple Types

decimal   double         float   long    int    short    sbyte

             only with           ulong   uint   ushort   byte
             type cast

                                                         char




                                                                 12
Enumerations
List of named constants

Declaration (directly in a namespace)
   enum Color {red, blue, green} // values: 0, 1, 2
   enum Access {personal=1, group=2, all=4}
   enum Access1 : byte {personal=1, group=2, all=4}



Use

   Color c = Color.blue;   // enumeration constants must be qualified

   Access a = Access.personal | Access.group;
   if ((Access.personal & a) != 0) Console.WriteLine("access granted");




                                                                          13
Operations on Enumerations

  Compare            if (c == Color.red) ...
                     if (c > Color.red && c <= Color.green) ...
  +, -               c = c + 2;
  ++, --             c++;
  &                  if ((c & Color.red) == 0) ...
  |                  c = c | Color.blue;
  ~                  c = ~ Color.red;


  The compiler does not check if the result is a valid enumeration value.


Note
  - Enumerations cannot be assigned to int (except after a type cast).
  - Enumeration types inherit from object (Equals, ToString, ...).
  - Class System.Enum provides operations on enumerations
      (GetName, Format, GetValues, ...).
                                                                            14
Arrays
One-dimensional Arrays


   int[] a = new int[3];
   int[] b = new int[] {3, 4, 5};
   int[] c = {3, 4, 5};
   SomeClass[] d = new SomeClass[10]; // Array of references
   SomeStruct[] e = new SomeStruct[10]; // Array of values (directly in the array)

   int len = a.Length; // number of elements in a




                                                                                     15
Multidimensional Arrays
Jagged (like in Java)           a
                                                     a[0][1]

   int[][] a = new int[2][];        a[0]
   a[0] = new int[3];               a[1]
   a[1] = new int[4];

   int x = a[0][1];
   int len = a.Length; // 2
   len = a[0].Length; // 3

Rectangular (more compact, more efficient access)
   int[,] a = new int[2, 3];    a          a[0, 1]

   int x = a[0, 1];
   int len = a.Length;   // 6
   len = a.GetLength(0); // 2
   len = a.GetLength(1); // 3

                                                               16
Class System.String
Can be used as standard type string
   string s = "Alfonso";


Note
• Strings are immutable (use StringBuilder if you want to modify strings)
• Can be concatenated with +: "Don " + s
• Can be indexed: s[i]
• String length: s.Length
• Strings are reference types => reference semantics in assignments
• but their values can be compared with == and != : if (s == "Alfonso") ...
• Class String defines many useful operations:
   CompareTo, IndexOf, StartsWith, Substring, ...




                                                                              17
Structs
Declaration
   struct Point {
        public int x, y;                                             // fields
        public Point (int x, int y) { this.x = x; this.y = y; }      // constructor
        public void MoveTo (int a, int b) { x = a; y = b; }          // methods
   }

Use
   Point p = new Point(3, 4);         // constructor initializes object on the stack
   p.MoveTo(10, 20);                  // method call




                                                                                       18
Classes
Declaration
   class Rectangle {
       Point origin;
       public int width, height;
       public Rectangle() { origin = new Point(0,0); width = height = 0; }
       public Rectangle (Point p, int w, int h) { origin = p; width = w; height = h; }
       public void MoveTo (Point p) { origin = p; }
   }

Use
   Rectangle r = new Rectangle(new Point(10, 20), 5, 5);
   int area = r.width * r.height;
   r.MoveTo(new Point(3, 3));




                                                                                         19
Differences Between Classes and Structs
Classes                                 Structs

Reference Types                         Value Types
(objects stored on the heap)            (objects stored on the stack)

support inheritance                     no inheritance
(all classes are derived from object)   (but compatible with object)

can implement interfaces                can implement interfaces

may have a destructor                   no destructors allowed




                                                                        20
Boxing and Unboxing
Value types (int, struct, enum) are also compatible with object!

Boxing
   The assignment
       object obj = 3;
   wraps up the value 3 into a heap object
         obj

                    3



Unboxing
  The assignment
       int x = (int) obj;
   unwraps the value again


                                                                   21
Boxing/Unboxing
Allows the implementation of generic container types
   class Queue {
       ...
       public void Enqueue(object x) {...}
       public object Dequeue() {...}
       ...
   }

This Queue can then be used for reference types and value types
   Queue q = new Queue();

   q.Enqueue(new Rectangle());
   q.Enqueue(3);

   Rectangle r = (Rectangle) q.Dequeue();
   int x = (int) q.Dequeue();




                                                                  22
Expressions
Operators and their Priority
Primary            (x) x.y f(x) a[x] x++ x-- new typeof sizeof 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        c?x:y
Assignment         = += -= *= /= %= <<= >>= &= ^= |=

Operators on the same level are evaluated from left to right


                                                                             24
Overflow Check
Overflow is not checked by default

   int x = 1000000;
   x = x * x; // -727379968, no error


Overflow check can be turned on

   x = checked(x * x); //    System.OverflowException

   checked {
       ...
       x = x * x;    //     System.OverflowException
       ...
   }


Overflow check can also be turned on with a compiler switch

   csc /checked Test.cs

                                                              25
typeof and sizeof
typeof
• Returns the Type descriptor for a given type
  (the Type descriptor of an object o can be retrieved with o.GetType()).

  Type t = typeof(int);
  Console.WriteLine(t.Name);    //   Int32



sizeof
• Returns the size of a type in bytes.
• Can only be applied to value types.
• Can only be used in an unsafe block (the size of structs may be system dependent).
  Must be compiled with csc /unsafe xxx.cs

  unsafe {
    Console.WriteLine(sizeof(int));
    Console.WriteLine(sizeof(MyEnumType));
    Console.WriteLine(sizeof(MyStructType));
  }


                                                                                       26
Declarations
Declaration Space
The program area to which a declaration belongs

Entities can be declared in a ...
-   namespace:                  Declaration of classes, interfaces, structs, enums, delegates
-   class, interface, struct:   Declaration of fields, methods, properties, events, indexers, ...
-   enum:                       Declaration of enumeration constants
-   block:                      Declaration of local variables


Scoping rules
- A name must not be declared twice in the same declaration space.
- Declarations may occur in arbitrary order.
  Exception: local variables must be declared before they are used

Visibility rules
- A name is only visible within its declaration space
  (local variables are only visible after their point of declaration).
- The visibility can be restricted by modifiers (private, protected, ...)
                                                                                               28
Namespaces
File: X.cs
     namespace A {
       ... Classes ...
       ... Interfaces ...
       ... Structs ...
       ... Enums ...
       ... Delegates ...
       namespace B { // full name: A.B
            ...
       }
     }

File: Y.cs
     namespace A {
       ...
       namespace B {...}
     }

     namespace C {...}

Equally named namespaces in different files constitute a single declaration space.
Nested namespaces constitute a declaration space on their own.
                                                                                     29
Using Other Namespaces
Color.cs                           Figures.cs                         Triangle.cs
 namespace Util {                   namespace Util.Figures {           namespace Util.Figures {
   public enum Color {...}            public class Rect {...}            public class Triangle {...}
 }                                    public class Circle {...}        }
                                    }

 using Util.Figures;

 class Test {
    Rect r;         // without qualification (because of using Util.Figures)
    Triangle t;
    Util.Color c;   // with qualification
 }



Foreign namespaces
• must either be imported (e.g. using Util;)
• or specified in a qualified name (e.g. Util.Color)

Most programs need the namespace System => using System;
                                                                                                   30
Blocks
Various kinds of blocks
  void foo (int x) {                     // method block
         ... local variables ...

         {                               // nested block
              ... local variables ...
         }

         for (int i = 0; ...) {          // structured statement block
               ... local variables ...
         }
  }



Note
• The declaration space of a block includes the declaration spaces of nested blocks.
• Formal parameters belong to the declaration space of the method block.
• The loop variable in a for statement belongs to the block of the for statement.
• The declaration of a local variable must precede its use.

                                                                                       31
Declaration of Local Variables
void foo(int a) {
    int b;
    if (...) {
           int b;                // error: b already declared in outer block
           int c;                // ok so far, but wait ...
           int d;
           ...
    } else {
           int a;                // error: a already declared in outer block
           int d;                // ok: no conflict with d from previous block
    }
    for (int i = 0; ...) {...}
    for (int i = 0; ...) {...}   // ok: no conflict with i from previous loop
    int c;                       // error: c already declared in this declaration space
}




                                                                                          32
Statements
Simple Statements
Empty statement
  ;                                // ; is a terminator, not a separator


Assigment
  x = 3 * y + 1;


Method call
  string s = "a,b,c";
  string[] parts = s.Split(',');   // invocation of an object method (non-static)

  s = String.Join(" + ", parts); // invocation of a class method (static)




                                                                                    34
if Statement
if ('0' <= ch && ch <= '9')
      val = ch - '0';
else if ('A' <= ch && ch <= 'Z')
      val = 10 + ch - 'A';
else {
      val = 0;
      Console.WriteLine("invalid character {0}", ch);
}




                                                        35
switch Statement
switch (country) {
    case "Germany": case "Austria": case "Switzerland":
         language = "German";
         break;
    case "England": case "USA":
         language = "English";
         break;
    case null:
         Console.WriteLine("no country specified");
         break;
    default:
         Console.WriteLine("don't know language of {0}", country);
         break;
}


Type of switch expression
numeric, char, enum or string (null ok as a case label).
No fall-through!
Every statement sequence in a case must be terminated with break (or return, goto, throw).
If no case label matches   default
If no default specified  continuation after the switch statement
                                                                                             36
switch with Gotos
E.g. for the implementation of automata
                        b
                   a         c
              0         1          2
                        c

int state = 0;
int ch = Console.Read();
switch (state) {
    case 0: if (ch == 'a') { ch = Console.Read(); goto case 1; }
             else if (ch == 'c') goto case 2;
             else goto default;
    case 1: if (ch == 'b') { ch = Console.Read(); goto case 1; }
             else if (ch == 'c') goto case 2;
             else goto default;
    case 2: Console.WriteLine("input valid");
             break;
    default: Console.WriteLine("illegal character {0}", ch);
             break;
}



                                                                   37
Loops
while
      while (i < n) {
          sum += i;
          i++;
      }

do while
      do {
          sum += a[i];
          i--;
      } while (i > 0);

for                                 Short form for
      for (int i = 0; i < n; i++)      int i = 0;
            sum += i;                  while (i < n) {
                                           sum += i;
                                           i++;
                                       }


                                                         38
foreach Statement
For iterating over collections and arrays

   int[] a = {3, 17, 4, 8, 2, 29};
   foreach (int x in a) sum += x;


   string s = "Hello";
   foreach (char ch in s) Console.WriteLine(ch);


   Queue q = new Queue();
   q.Enqueue("John"); q.Enqueue("Alice"); ...
   foreach (string s in q) Console.WriteLine(s);




                                                   39
Jumps
break;         For exiting a loop or a switch statement.
               There is no break with a label like in Java (use goto instead).


continue;      Continues with the next loop iteration.


goto case 3:   Can be used in a switch statement to jump to a case label.



myLab:
...
goto myLab;    Jumps to the label myLab.
               Restrictions:
               - no jumps into a block
               - no jumps out of a finally block of a try statement



                                                                                 40
return Statement
Returning from a void method

   void f(int x) {
      if (x == 0) return;
      ...
   }


Returning a value from a function method

   int max(int a, int b) {
       if (a > b) return a; else return b;
   }

   class C {
      static int Main() {
         ...
         return errorCode;         // The Main method can be declared as a function;
      }                            // the returned error code can be checked with the
                                   // DOS variable errorlevel
   }

                                                                                        41
Classes and Structs
Contents of Classes or Structs
class C {
   ... fields, constants ...               // for object-oriented programming
   ... methods ...
   ... constructors, destructors ...

    ... properties ...                     // for component-based programming
    ... events ...

    ... indexers ...                       // for amenity
    ... overloaded operators ...

    ... nested types (classes, interfaces, structs, enums, delegates) ...
}




                                                                                43
Classes
  class Stack {
     int[] values;
     int top = 0;
      public Stack(int size) { ... }
      public void Push(int x) {...}
      public int Pop() {...}
  }



• Objects are allocated on the heap (classes are reference types)
• Objects must be created with new
  Stack s = new Stack(100);

• Classes can inherit from one other class (single code inheritance)
• Classes can implement multiple interfaces (multiple type inheritance)




                                                                          44
Structs
  struct Point {
     int x, y;
     public Point(int x, int y) { this.x = x; this.y = y; }
     public MoveTo(int x, int y) {...}
  }

• Objects are allocated on the stack not on the heap (structs are value types)
  + efficient, low memory consumption, no burden for the garbage collector.
  - live only as long as their container (not suitable for dynamic data structures)
• Can be allocated with new
  Point p;         // fields of p are not yet initialized
  Point q = new Point();

• Fields must not be initialized at their declaration
  struct Point {
     int x = 0;        // compilation error
  }

• Parameterless construcors cannot be declared
• Can neither inherit nor be inherited, but can implement interfaces
                                                                                      45
Visibility Modifiers (excerpt)
public      visible where the declaring namespace is known
            - Members of interfaces and enumerations are public by default.
            - Types in a namespace (classes, structs, interfaces, enums, delegates)
              have default visibility internal (visible in the declaring assembly)
private     only visible in declaring class or struct
            - Members of classes and structs are private by default
              (fields, methods, properties, ..., nested types)


Example

   public class Stack {
     private int[] val;              // private is also default
     private int top;                // private is also default
     public Stack() {...}
     public void Push(int x) {...}
     public int Pop() {...}
   }


                                                                                      46
Fields and Constants
class C {

     int value = 0;               Field
                                  - Initialization is optional
                                  - Initialization must not access other fields or methods
                                    of the same type
                                  - Fields of a struct must not be initialized

     const long size = ((long)int.MaxValue + 1) / 4;

                                  Constant
                                  - Value must be computable at compile time

     readonly DateTime date;

                                  Read Only Field
                                  - Must be initialized in their declaration or in a constructor
                                  - Value needs not be computable at compile time
                                  - Consumes a memory location (like a field)
}

Access within C                              Access from other classes
... value ... size ... date ...              C c = new C();
                                             ... c.value ... c.size ... c.date ...
                                                                                                   47
Static Fields and Constants
Belong to a class, not to an object
     class Rectangle {
         static Color defaultColor;      // once per class
         static readonly int scale;      // -- " –
         // static constants are not allowed
         int x, y, width,height;         // once per object
         ...
     }



Access within the class                 Access from other classes
... defaultColor ... scale ...          ... Rectangle.defaultColor ... Rectangle.scale ...




                                                                                             48
Methods
Examples
class C {
    int sum = 0, n = 0;

    public void Add (int x) {    // procedure
        sum = sum + x; n++;
    }

    public float Mean() {        // function (must return a value)
        return (float)sum / n;
    }
}



Access within the class          Access from other classes
this.Add(3);                     C c = new C();
float x = Mean();                c.Add(3);
                                 float x = c.Mean();



                                                                     49
Static Methods
Operations on class data (static fields)

    class Rectangle {
        static Color defaultColor;

        public static void ResetColor() {
            defaultColor = Color.white;
        }
    }


Access within the class                     Access from other classes
ResetColor();                               Rectangle.ResetColor();




                                                                        50
Parameters
Value Parameters (input values)                       - "call by value"
  void Inc(int x) {x = x + 1;}                        - formal parameter is a copy of the
  void f() {                                            actual parameter
     int val = 3;                                     - actual parameter is an expression
     Inc(val); // val == 3
  }

ref Parameters (transition values)                    - "call by reference"
  void Inc(ref int x) { x = x + 1; }                  - formal parameter is an alias for the
  void f() {                                            actual parameter
     int val = 3;                                       (address of actual parameter is passed)
     Inc(ref val); // val == 4                        - actual parameter must be a variable
  }

out Parameters (output values)
                                                      - similar to ref parameters
  void Read (out int first, out int next) {
     first = Console.Read(); next = Console.Read();     but no value is passed by the caller.
  }                                                   - must not be used in the method before
  void f() {                                            it got a value.
     int first, next;
     Read(out first, out next);
  }
                                                                                            51
Variable Number of Parameters
Last n parameters may be a sequence of values of a certain type.

           keyword
                                     array type
            params

   void Add (out int sum, params int[] val) {
       sum = 0;
       foreach (int i in val) sum = sum + i;
   }


   params cannot be used for ref and out parameters



Use
   Add(out sum, 3, 5, 2, 9); // sum == 19




                                                                   52
Method Overloading
Methods of a class may have the same name
- if they have different numbers of parameters, or
- if they have different parameter types, or
- if they have different parameter kinds (value, ref/out)

Examples
  void F (int x) {...}
  void F (char x) {...}
  void F (int x, long y) {...}
  void F (long x, int y) {...}
  void F (ref int x) {...}

Calls
  int i; long n; short s;
  F(i);         // F(int x)
  F('a');       // F(char x)
  F(i, n);      // F(int x, long y)
  F(n, s);      // F(long x, int y);
  F(i, s);      // cannot distinguish F(int x, long y) and F(long x, int y); => compilation error
  F(i, i);      // cannot distinguish F(int x, long y) and F(long x, int y); => compilation error

Overloaded methods must not differ only in their function types, in the presence of params
or in ref versus out!
                                                                                                    53
Constructors for Classes
Example

    class Rectangle {
        int x, y, width, height;
        public Rectangle (int x, int y, int w, int h) {this.x = x; this.y = y; width = x; height = h; }
        public Rectangle (int w, int h) : this(0, 0, w, h) {}
        public Rectangle () : this(0, 0, 0, 0) {}
        ...
    }

    Rectangle r1 = new Rectangle();
    Rectangle r2 = new Rectangle(2, 5);
    Rectangle r3 = new Rectangle(2, 2, 10, 5);


•   Constructors can be overloaded.

•   A construcor may call another constructor with this
    (specified in the constructor head, not in its body as in Java!).

•   Before a construcor is called, fields are possibly initialized.

                                                                                                          54
Default Constructor
If no constructor was declared in a class, the compiler generates a
parameterless default constructor:
   class C { int x; }
   C c = new C();       // ok

   The default constructor initializes all fields as follows:
     numeric        0
     enum           0
     bool           false
     char           '0'
     reference      null

If a constructor was declared, no default constructor is generated:
   class C {
      int x;
      public C(int y) { x = y; }
   }

   C c1 = new C(); // compilation error
   C c2 = new C(3); // ok

                                                                      55
Constructors for Structs
Example
    struct Complex {
         double re, im;
         public Complex(double re, double im) { this.re = re; this.im = im; }
         public Complex(double re) : this (re, 0) {}
         ...
    }

    Complex c0;                            // c0.re and c0.im are still uninitialized
    Complex c1 = new Complex();            // c1.re == 0, c1.im == 0
    Complex c2 = new Complex(5);           // c2.re == 5, c2.im == 0
    Complex c3 = new Complex(10, 3);       // c3.re == 10, c3.im == 3


•   For every struct the compiler generates a parameterless default constructor
    (even if there are other constructors).
    The default constructor zeroes all fields.

•   Programmers must not declare a parameterless constructor for structs
    (for implementation reasons of the CLR).


                                                                                        56
Static Constructors
Both for classes and for structs

    class Rectangle {
        ...
        static Rectangle() {
            Console.WriteLine("Rectangle initialized");
        }
    }

    struct Point {
         ...
         static Point() {
             Console.WriteLine("Point initialized");
         }
    }


•   Must be parameterless (also for structs) and have no public or private modifier.
•   There must be just one static constructor per class/struct.
•   Is invoked once before this type is used for the first time.



                                                                                       57
Destructors
    class Test {

        ~Test() {
          ... finalization work ...
          // automatically calls the destructor of the base class
        }

    }


•   Correspond to finalizers in Java.
•   Called for an object before it is removed by the garbage collector.
•   No public or private.
•   Is dangerous (object resurrection) and should be avoided.




                                                                          58
Properties
Syntactic sugar for get/set methods
    class Data {           property type
                                                 property name
       FileStream s;

        public string FileName {
          set {
              s = new FileStream(value, FileMode.Create);
          }
          get {
                                                                 "input parameter"
              return s.Name;
                                                                 of the set method
          }
        }
    }


Used as "smart fields"
    Data d = new Data();

    d.FileName = "myFile.txt";    // invokes set("myFile.txt")
    string s = d.FileName;        // invokes get()

JIT compilers often inline get/set methods     no efficiency penalty
                                                                                     59
Properties (continued)
get or set can be omitted
    class Account {
       long balance;

        public long Balance {
          get { return balance; }
        }
    }

    x = account.Balance;            // ok
    account.Balance = ...;          // compilation error


Why are properties a good idea?
•   Interface and implementation of data may differ.
•   Allows read-only and write-only fields.
•   Can validate a field when it is assigned.
•   Substitute for fields in interfaces.



                                                           60
Indexers
Programmable operator for indexing a collection
    class File {           type of the               name          type and name
       FileStream s;   indexed expression        (always this)   of the index value

        public int this [int index] {
          get { s.Seek(index, SeekOrigin.Begin);
                  return s.ReadByte();
              }
          set { s.Seek(index, SeekOrigin.Begin);
                  s.WriteByte((byte)value);
              }
        }
    }

Use
    File f = ...;
    int x = f[10];     // calls f.get(10)
    f[10] = 'A';       // calls f.set(10, 'A')

•   get or set method can be omitted (write-only / read-only)
•   Indexers can be overloaded with different index types

                                                                                      61
Indexers (other example)
class MonthlySales {
    int[] product1 = new int[12];
    int[] product2 = new int[12];
    ...
    public int this[int i] {            // set method omitted => read-only
        get { return product1[i-1] + product2[i-1]; }
    }

    public int this[string month] {   // overloaded read-only indexer
      get {
          switch (month) {
              case "Jan": return product1[0] + product2[0];
              case "Feb": return product1[1] + product2[1];
              ...
          }
      }
    }
}


MonthlySales sales = new MonthlySales();
...
Console.WriteLine(sales[1] + sales["Feb"]);

                                                                             62
Overloaded Operators
Static method for implementing a certain operator
    struct Fraction {
       int x, y;
       public Fraction (int x, int y) {this.x = x; this.y = y; }

            public static Fraction operator + (Fraction a, Fraction b) {
              return new Fraction(a.x * b.y + b.x * a.y, a.y * b.y);
            }
    }

Use
    Fraction a = new Fraction(1, 2);
    Fraction b = new Fraction(3, 4);
    Fraction c = a + b; // c.x == 10, c.y == 8

•   The following operators can be overloaded:
        –     arithmetic:           +, - (unary and binary), *, /, %, ++, --
        –     relational:           ==, !=, <, >, <=, >=
        –     bit operators:        &, |, ^
        –     others:               !, ~, >>, <<, true, false
•   Must return a value

                                                                               63
Conversion Operators
Implicit conversion
-   If the conversion is always possible without loss of precision
-   e.g. long = int;

Explicit conversion
-   If a run time check is necessary or truncation is possible
-   e.g. int = (int) long;


Conversion operators for custom types
    class Fraction {
       int x, y;
       ...
       public static implicit operator Fraction (int x) { return new Fraction(x, 1); }
       public static explicit operator int (Fraction f) { return f.x / f.y; }
    }

Use
    Fraction f = 3;     // implicit conversion, f.x == 3, f.y == 1
    int i = (int) f;    // explicit conversion, i == 3


                                                                                         64
Nested Types
    class A {
       int x;
       B b = new B(this);
       public void f() { b.f(); }
        public class B {
          A a;
          public B(A a) { this.a = a; }
          public void f() { a.x = ...; ... a.f(); }
        }
    }

    class C {
       A a = new A();
       A.B b = new A.B(a);
    }

For auxiliary classes that should be hidden
- Inner class can access all members of the outer class (even private members).
- Outer class can access only public members of the inner class.
- Other classes can access an inner class only if it is public.
Nested types can also be structs, enums, interfaces and delegates.
                                                                                  65

More Related Content

PPT
ISTQB, ISEB Lecture Notes- 2
onsoftwaretest
 
PPT
ISTQB, ISEB Lecture Notes- 4
onsoftwaretest
 
DOCX
Automation Testing Syllabus - Checklist
Sunil Kumar Gunasekaran
 
PDF
Building a Test Automation Strategy for Success
Lee Barnes
 
PDF
Java Test Automation for REST, Web and Mobile
Elias Nogueira
 
PPT
ISTQB / ISEB Foundation Exam Practice - 5
Yogindernath Gupta
 
PDF
Chapter 1 - Fundamentals of Testing V4.0
Neeraj Kumar Singh
 
ISTQB, ISEB Lecture Notes- 2
onsoftwaretest
 
ISTQB, ISEB Lecture Notes- 4
onsoftwaretest
 
Automation Testing Syllabus - Checklist
Sunil Kumar Gunasekaran
 
Building a Test Automation Strategy for Success
Lee Barnes
 
Java Test Automation for REST, Web and Mobile
Elias Nogueira
 
ISTQB / ISEB Foundation Exam Practice - 5
Yogindernath Gupta
 
Chapter 1 - Fundamentals of Testing V4.0
Neeraj Kumar Singh
 

What's hot (20)

PPT
ISTQB / ISEB Foundation Exam Practice - 4
Yogindernath Gupta
 
PDF
API Testing following the Test Pyramid
Elias Nogueira
 
PPTX
O papel do qa (testador) em um time ágil
Mariana Elisa Moisés , CTFL-AT,MBA
 
PPT
Software Testing
Dhanasekaran Nagarajan
 
PPTX
Codeless Test Automation - A Webinar Presentation
Inflectra
 
PDF
Unit testing with JUnit
Pokpitch Patcharadamrongkul
 
PDF
Test Automation - Keytorc Approach
Keytorc Software Testing Services
 
PPTX
Selenium sunum
Ali Mert AVCI
 
PPT
Manual testing concepts course 1
Raghu Kiran
 
DOCX
Mainframes project
Balachandra_Golla
 
PDF
Istqb foundation level training 2018 syllabus - day1 intro
Hassan Muhammad
 
PPTX
Sta unit 5(abimanyu)
Abhimanyu Mishra
 
PPTX
Automation testing
Mona M. Abd El-Rahman
 
PPTX
Quickly Testing Qt Desktop Applications
Clare Macrae
 
PDF
Alpha & Beta Testing
Mithilesh Singh
 
PPS
ISTQB Foundation - Chapter 3
Chandukar
 
PDF
Test Automation
nikos batsios
 
PDF
Chapter 2 - Testing Throughout SDLC V4.0
Neeraj Kumar Singh
 
PPT
Testing concepts ppt
Rathna Priya
 
PDF
Chapter 3 - Static Testing (Review) V4.0
Neeraj Kumar Singh
 
ISTQB / ISEB Foundation Exam Practice - 4
Yogindernath Gupta
 
API Testing following the Test Pyramid
Elias Nogueira
 
O papel do qa (testador) em um time ágil
Mariana Elisa Moisés , CTFL-AT,MBA
 
Software Testing
Dhanasekaran Nagarajan
 
Codeless Test Automation - A Webinar Presentation
Inflectra
 
Unit testing with JUnit
Pokpitch Patcharadamrongkul
 
Test Automation - Keytorc Approach
Keytorc Software Testing Services
 
Selenium sunum
Ali Mert AVCI
 
Manual testing concepts course 1
Raghu Kiran
 
Mainframes project
Balachandra_Golla
 
Istqb foundation level training 2018 syllabus - day1 intro
Hassan Muhammad
 
Sta unit 5(abimanyu)
Abhimanyu Mishra
 
Automation testing
Mona M. Abd El-Rahman
 
Quickly Testing Qt Desktop Applications
Clare Macrae
 
Alpha & Beta Testing
Mithilesh Singh
 
ISTQB Foundation - Chapter 3
Chandukar
 
Test Automation
nikos batsios
 
Chapter 2 - Testing Throughout SDLC V4.0
Neeraj Kumar Singh
 
Testing concepts ppt
Rathna Priya
 
Chapter 3 - Static Testing (Review) V4.0
Neeraj Kumar Singh
 
Ad

Viewers also liked (11)

PPT
C# basics
Dinesh kumar
 
PPTX
C# for Beginners
Zaory Zaory
 
PPT
For Beginners - C#
Snehal Harawande
 
PPT
Learn C# at ASIT
ASIT
 
PPT
Visula C# Programming Lecture 3
Abou Bakr Ashraf
 
PPT
Visula C# Programming Lecture 1
Abou Bakr Ashraf
 
PPT
Visula C# Programming Lecture 2
Abou Bakr Ashraf
 
PPTX
C sharp part 001
Ralph Weber
 
PPT
Programming in c#
Shehrevar Davierwala
 
PPTX
C# Tutorial
Jm Ramos
 
PPT
Data Structure In C#
Shahzad
 
C# basics
Dinesh kumar
 
C# for Beginners
Zaory Zaory
 
For Beginners - C#
Snehal Harawande
 
Learn C# at ASIT
ASIT
 
Visula C# Programming Lecture 3
Abou Bakr Ashraf
 
Visula C# Programming Lecture 1
Abou Bakr Ashraf
 
Visula C# Programming Lecture 2
Abou Bakr Ashraf
 
C sharp part 001
Ralph Weber
 
Programming in c#
Shehrevar Davierwala
 
C# Tutorial
Jm Ramos
 
Data Structure In C#
Shahzad
 
Ad

Similar to C# for beginners (20)

PPT
Introduction to c#
OpenSource Technologies Pvt. Ltd.
 
PDF
Intro to .NET and Core C#
Jussi Pohjolainen
 
PDF
Dr archana dhawan bajaj - csharp fundamentals slides
Dr-archana-dhawan-bajaj
 
PPTX
Oop c++class(final).ppt
Alok Kumar
 
PPT
Csharp
Swaraj Kumar
 
PPTX
C#unit4
raksharao
 
PPTX
Net framework
Abhishek Mukherjee
 
PPT
Learn c sharp at amc square learning
ASIT Education
 
PPT
Csharp_mahesh
Ananthu Mahesh
 
PPT
Abhishek lingineni
abhishekl404
 
PPT
CPlusPus
rasen58
 
PDF
Introduction to clojure
Abbas Raza
 
PPT
C++_programs.ppt
Dikshithkumar10
 
PPT
C++_programs.ppt
EPORI
 
PPT
C++_programs.ppt
JayarAlejo
 
PPT
C++_programs.ppt
TeacherOnat
 
PPT
C++_programs.ppt
Infotech27
 
PPT
C++ programming: Basic introduction to C++.ppt
yp02
 
PPT
C++_programs.ppt
JayarAlejo
 
PDF
201005 accelerometer and core Location
Javier Gonzalez-Sanchez
 
Intro to .NET and Core C#
Jussi Pohjolainen
 
Dr archana dhawan bajaj - csharp fundamentals slides
Dr-archana-dhawan-bajaj
 
Oop c++class(final).ppt
Alok Kumar
 
Csharp
Swaraj Kumar
 
C#unit4
raksharao
 
Net framework
Abhishek Mukherjee
 
Learn c sharp at amc square learning
ASIT Education
 
Csharp_mahesh
Ananthu Mahesh
 
Abhishek lingineni
abhishekl404
 
CPlusPus
rasen58
 
Introduction to clojure
Abbas Raza
 
C++_programs.ppt
Dikshithkumar10
 
C++_programs.ppt
EPORI
 
C++_programs.ppt
JayarAlejo
 
C++_programs.ppt
TeacherOnat
 
C++_programs.ppt
Infotech27
 
C++ programming: Basic introduction to C++.ppt
yp02
 
C++_programs.ppt
JayarAlejo
 
201005 accelerometer and core Location
Javier Gonzalez-Sanchez
 

More from application developer (20)

PPTX
Chapter 26
application developer
 
PPTX
Chapter 25
application developer
 
PPTX
Chapter 23
application developer
 
DOCX
Assignment
application developer
 
DOCX
Next step job board (Assignment)
application developer
 
PPTX
Chapter 19
application developer
 
PPTX
Chapter 18
application developer
 
PPTX
Chapter 17
application developer
 
PPTX
Chapter 16
application developer
 
DOCX
Week 3 assignment
application developer
 
PPTX
Chapter 15
application developer
 
PPTX
Chapter 14
application developer
 
PPTX
Chapter 13
application developer
 
PPTX
Chapter 12
application developer
 
PPTX
Chapter 11
application developer
 
PPTX
Chapter 10
application developer
 
DOCX
C # test paper
application developer
 
PPTX
Chapter 9
application developer
 
PPTX
Chapter 8 part2
application developer
 
PPTX
Chapter 8 part1
application developer
 
Next step job board (Assignment)
application developer
 
Week 3 assignment
application developer
 
C # test paper
application developer
 
Chapter 8 part2
application developer
 
Chapter 8 part1
application developer
 

Recently uploaded (20)

PPTX
How to Track Skills & Contracts Using Odoo 18 Employee
Celine George
 
PPTX
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
PPTX
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
PPTX
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
PPTX
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
PPTX
How to Apply for a Job From Odoo 18 Website
Celine George
 
PDF
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
PPTX
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
PPTX
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
PPTX
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
DOCX
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
PPTX
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
DOCX
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
PPTX
CDH. pptx
AneetaSharma15
 
PPTX
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
PPTX
Care of patients with elImination deviation.pptx
AneetaSharma15
 
PPTX
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
PDF
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
PPTX
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
How to Track Skills & Contracts Using Odoo 18 Employee
Celine George
 
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
How to Apply for a Job From Odoo 18 Website
Celine George
 
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
CDH. pptx
AneetaSharma15
 
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
Care of patients with elImination deviation.pptx
AneetaSharma15
 
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
An introduction to Dialogue writing.pptx
drsiddhantnagine
 

C# for beginners

  • 1. Introduction to C# The New Language for . H.Mössenböck University of Linz, Austria [email protected]
  • 2. Contents Introduction to C# Advanced C# 1. Overview 7. Inheritance 2. Types 8. Interfaces 3. Expressions 9. Delegates 4. Declarations 10. Exceptions 5. Statements 11. Namespaces and Assemblies 6. Classes and Structs 12. Attributes 13. Threads 14. XML Comments References: • B.Albahari, P.Drayton, B.Merrill: C# Essentials. O'Reilly, 2001 • S.Robinson et al: Professional C#, Wrox Press, 2001 • Online documentation on the .NET SDK CD 2
  • 3. Features of C# Very similar to Java 70% Java, 10% C++, 5% Visual Basic, 15% new As in Java As in C++ • Object-orientation (single inheritance) • (Operator) Overloading • Interfaces • Pointer arithmetic in unsafe code • Exceptions • Some syntactic details • Threads • Namespaces (like Packages) • Strong typing • Garbage Collection • Reflection • Dynamic loading of code • ... 3
  • 4. New Features in C# Really new (compared to Java) "Syntactic Sugar" • Reference and output parameters • Component-based programming • Objects on the stack (structs) - Properties • Rectangular arrays - Events • Enumerations • Delegates • Unified type system • Indexers • goto • Operator overloading • Versioning • foreach statements • Boxing/unboxing • Attributes • ... 4
  • 5. Hello World File Hello.cs using System; • uses the namespace System • entry point must be called Main class Hello { • output goes to the console • file name and class name static void Main() { need not be identical Console.WriteLine("Hello World"); } } Compilation (in the Console window) csc Hello.cs Execution Hello 5
  • 6. Structure of C# Programs Programm File F1.cs File F2.cs File F3.cs namespace A {...} namespace B {...} namespace C {...} class X {...} class Y {...} class Z {...} • If no namespace is specified => anonymous default namespace • Namespaces may also contain structs, interfaces, delegates and enums • Namespace may be "reopened" in other files • Simplest case: single class, single file, default namespace 6
  • 7. A Program Consisting of 2 Files Counter.cs class Counter { Compilation int val = 0; public void Add (int x) { val = val + x; } csc Counter.cs Prog.cs public int Val () { return val; } => generates Prog.exe } Execution Prog.cs Prog using System; class Prog { Working with DLLs static void Main() { Counter c = new Counter(); csc /target:library Counter.cs c.Add(3); c.Add(5); => generates Counter.dll Console.WriteLine("val = " + c.Val()); } csc /reference:Counter.dll Prog.cs } => generates Prog.exe 7
  • 9. Unified Type System Types Value Types Reference Types Pointers Simple Types Enums Structs Classes Interfaces Arrays Delegates bool sbyte byte float char short ushort double int uint decimal long ulong User-defined Types All types are compatible with object - can be assigned to variables of type object - all operations of type object are applicable to them 9
  • 10. Value Types versus Reference Types Value Types Reference Types variable contains value reference stored on stack heap initialisation 0, false, '0' null assignment copies the value copies the reference example int i = 17; string s = "Hello"; int j = i; string s1 = s; i 17 s Hello j 17 s1 10
  • 11. Simple Types Long Form in Java Range sbyte System.SByte byte -128 .. 127 byte System.Byte --- 0 .. 255 short System.Int16 short -32768 .. 32767 ushort System.UInt16 --- 0 .. 65535 int System.Int32 int -2147483648 .. 2147483647 uint System.UInt32 --- 0 .. 4294967295 long System.Int64 long -263 .. 263-1 ulong System.UInt64 --- 0 .. 264-1 float System.Single float ±1.5E-45 .. ±3.4E38 (32 Bit) double System.Double double ±5E-324 .. ±1.7E308 (64 Bit) decimal System.Decimal --- ±1E-28 .. ±7.9E28 (128 Bit) bool System.Boolean boolean true, false char System.Char char Unicode character 11
  • 12. Compatibility Between Simple Types decimal double float long int short sbyte only with ulong uint ushort byte type cast char 12
  • 13. Enumerations List of named constants Declaration (directly in a namespace) enum Color {red, blue, green} // values: 0, 1, 2 enum Access {personal=1, group=2, all=4} enum Access1 : byte {personal=1, group=2, all=4} Use Color c = Color.blue; // enumeration constants must be qualified Access a = Access.personal | Access.group; if ((Access.personal & a) != 0) Console.WriteLine("access granted"); 13
  • 14. Operations on Enumerations Compare if (c == Color.red) ... if (c > Color.red && c <= Color.green) ... +, - c = c + 2; ++, -- c++; & if ((c & Color.red) == 0) ... | c = c | Color.blue; ~ c = ~ Color.red; The compiler does not check if the result is a valid enumeration value. Note - Enumerations cannot be assigned to int (except after a type cast). - Enumeration types inherit from object (Equals, ToString, ...). - Class System.Enum provides operations on enumerations (GetName, Format, GetValues, ...). 14
  • 15. Arrays One-dimensional Arrays int[] a = new int[3]; int[] b = new int[] {3, 4, 5}; int[] c = {3, 4, 5}; SomeClass[] d = new SomeClass[10]; // Array of references SomeStruct[] e = new SomeStruct[10]; // Array of values (directly in the array) int len = a.Length; // number of elements in a 15
  • 16. Multidimensional Arrays Jagged (like in Java) a a[0][1] int[][] a = new int[2][]; a[0] a[0] = new int[3]; a[1] a[1] = new int[4]; int x = a[0][1]; int len = a.Length; // 2 len = a[0].Length; // 3 Rectangular (more compact, more efficient access) int[,] a = new int[2, 3]; a a[0, 1] int x = a[0, 1]; int len = a.Length; // 6 len = a.GetLength(0); // 2 len = a.GetLength(1); // 3 16
  • 17. Class System.String Can be used as standard type string string s = "Alfonso"; Note • Strings are immutable (use StringBuilder if you want to modify strings) • Can be concatenated with +: "Don " + s • Can be indexed: s[i] • String length: s.Length • Strings are reference types => reference semantics in assignments • but their values can be compared with == and != : if (s == "Alfonso") ... • Class String defines many useful operations: CompareTo, IndexOf, StartsWith, Substring, ... 17
  • 18. Structs Declaration struct Point { public int x, y; // fields public Point (int x, int y) { this.x = x; this.y = y; } // constructor public void MoveTo (int a, int b) { x = a; y = b; } // methods } Use Point p = new Point(3, 4); // constructor initializes object on the stack p.MoveTo(10, 20); // method call 18
  • 19. Classes Declaration class Rectangle { Point origin; public int width, height; public Rectangle() { origin = new Point(0,0); width = height = 0; } public Rectangle (Point p, int w, int h) { origin = p; width = w; height = h; } public void MoveTo (Point p) { origin = p; } } Use Rectangle r = new Rectangle(new Point(10, 20), 5, 5); int area = r.width * r.height; r.MoveTo(new Point(3, 3)); 19
  • 20. Differences Between Classes and Structs Classes Structs Reference Types Value Types (objects stored on the heap) (objects stored on the stack) support inheritance no inheritance (all classes are derived from object) (but compatible with object) can implement interfaces can implement interfaces may have a destructor no destructors allowed 20
  • 21. Boxing and Unboxing Value types (int, struct, enum) are also compatible with object! Boxing The assignment object obj = 3; wraps up the value 3 into a heap object obj 3 Unboxing The assignment int x = (int) obj; unwraps the value again 21
  • 22. Boxing/Unboxing Allows the implementation of generic container types class Queue { ... public void Enqueue(object x) {...} public object Dequeue() {...} ... } This Queue can then be used for reference types and value types Queue q = new Queue(); q.Enqueue(new Rectangle()); q.Enqueue(3); Rectangle r = (Rectangle) q.Dequeue(); int x = (int) q.Dequeue(); 22
  • 24. Operators and their Priority Primary (x) x.y f(x) a[x] x++ x-- new typeof sizeof 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 c?x:y Assignment = += -= *= /= %= <<= >>= &= ^= |= Operators on the same level are evaluated from left to right 24
  • 25. Overflow Check Overflow is not checked by default int x = 1000000; x = x * x; // -727379968, no error Overflow check can be turned on x = checked(x * x); // System.OverflowException checked { ... x = x * x; // System.OverflowException ... } Overflow check can also be turned on with a compiler switch csc /checked Test.cs 25
  • 26. typeof and sizeof typeof • Returns the Type descriptor for a given type (the Type descriptor of an object o can be retrieved with o.GetType()). Type t = typeof(int); Console.WriteLine(t.Name); // Int32 sizeof • Returns the size of a type in bytes. • Can only be applied to value types. • Can only be used in an unsafe block (the size of structs may be system dependent). Must be compiled with csc /unsafe xxx.cs unsafe { Console.WriteLine(sizeof(int)); Console.WriteLine(sizeof(MyEnumType)); Console.WriteLine(sizeof(MyStructType)); } 26
  • 28. Declaration Space The program area to which a declaration belongs Entities can be declared in a ... - namespace: Declaration of classes, interfaces, structs, enums, delegates - class, interface, struct: Declaration of fields, methods, properties, events, indexers, ... - enum: Declaration of enumeration constants - block: Declaration of local variables Scoping rules - A name must not be declared twice in the same declaration space. - Declarations may occur in arbitrary order. Exception: local variables must be declared before they are used Visibility rules - A name is only visible within its declaration space (local variables are only visible after their point of declaration). - The visibility can be restricted by modifiers (private, protected, ...) 28
  • 29. Namespaces File: X.cs namespace A { ... Classes ... ... Interfaces ... ... Structs ... ... Enums ... ... Delegates ... namespace B { // full name: A.B ... } } File: Y.cs namespace A { ... namespace B {...} } namespace C {...} Equally named namespaces in different files constitute a single declaration space. Nested namespaces constitute a declaration space on their own. 29
  • 30. Using Other Namespaces Color.cs Figures.cs Triangle.cs namespace Util { namespace Util.Figures { namespace Util.Figures { public enum Color {...} public class Rect {...} public class Triangle {...} } public class Circle {...} } } using Util.Figures; class Test { Rect r; // without qualification (because of using Util.Figures) Triangle t; Util.Color c; // with qualification } Foreign namespaces • must either be imported (e.g. using Util;) • or specified in a qualified name (e.g. Util.Color) Most programs need the namespace System => using System; 30
  • 31. Blocks Various kinds of blocks void foo (int x) { // method block ... local variables ... { // nested block ... local variables ... } for (int i = 0; ...) { // structured statement block ... local variables ... } } Note • The declaration space of a block includes the declaration spaces of nested blocks. • Formal parameters belong to the declaration space of the method block. • The loop variable in a for statement belongs to the block of the for statement. • The declaration of a local variable must precede its use. 31
  • 32. Declaration of Local Variables void foo(int a) { int b; if (...) { int b; // error: b already declared in outer block int c; // ok so far, but wait ... int d; ... } else { int a; // error: a already declared in outer block int d; // ok: no conflict with d from previous block } for (int i = 0; ...) {...} for (int i = 0; ...) {...} // ok: no conflict with i from previous loop int c; // error: c already declared in this declaration space } 32
  • 34. Simple Statements Empty statement ; // ; is a terminator, not a separator Assigment x = 3 * y + 1; Method call string s = "a,b,c"; string[] parts = s.Split(','); // invocation of an object method (non-static) s = String.Join(" + ", parts); // invocation of a class method (static) 34
  • 35. if Statement if ('0' <= ch && ch <= '9') val = ch - '0'; else if ('A' <= ch && ch <= 'Z') val = 10 + ch - 'A'; else { val = 0; Console.WriteLine("invalid character {0}", ch); } 35
  • 36. switch Statement switch (country) { case "Germany": case "Austria": case "Switzerland": language = "German"; break; case "England": case "USA": language = "English"; break; case null: Console.WriteLine("no country specified"); break; default: Console.WriteLine("don't know language of {0}", country); break; } Type of switch expression numeric, char, enum or string (null ok as a case label). No fall-through! Every statement sequence in a case must be terminated with break (or return, goto, throw). If no case label matches default If no default specified continuation after the switch statement 36
  • 37. switch with Gotos E.g. for the implementation of automata b a c 0 1 2 c int state = 0; int ch = Console.Read(); switch (state) { case 0: if (ch == 'a') { ch = Console.Read(); goto case 1; } else if (ch == 'c') goto case 2; else goto default; case 1: if (ch == 'b') { ch = Console.Read(); goto case 1; } else if (ch == 'c') goto case 2; else goto default; case 2: Console.WriteLine("input valid"); break; default: Console.WriteLine("illegal character {0}", ch); break; } 37
  • 38. Loops while while (i < n) { sum += i; i++; } do while do { sum += a[i]; i--; } while (i > 0); for Short form for for (int i = 0; i < n; i++) int i = 0; sum += i; while (i < n) { sum += i; i++; } 38
  • 39. foreach Statement For iterating over collections and arrays int[] a = {3, 17, 4, 8, 2, 29}; foreach (int x in a) sum += x; string s = "Hello"; foreach (char ch in s) Console.WriteLine(ch); Queue q = new Queue(); q.Enqueue("John"); q.Enqueue("Alice"); ... foreach (string s in q) Console.WriteLine(s); 39
  • 40. Jumps break; For exiting a loop or a switch statement. There is no break with a label like in Java (use goto instead). continue; Continues with the next loop iteration. goto case 3: Can be used in a switch statement to jump to a case label. myLab: ... goto myLab; Jumps to the label myLab. Restrictions: - no jumps into a block - no jumps out of a finally block of a try statement 40
  • 41. return Statement Returning from a void method void f(int x) { if (x == 0) return; ... } Returning a value from a function method int max(int a, int b) { if (a > b) return a; else return b; } class C { static int Main() { ... return errorCode; // The Main method can be declared as a function; } // the returned error code can be checked with the // DOS variable errorlevel } 41
  • 43. Contents of Classes or Structs class C { ... fields, constants ... // for object-oriented programming ... methods ... ... constructors, destructors ... ... properties ... // for component-based programming ... events ... ... indexers ... // for amenity ... overloaded operators ... ... nested types (classes, interfaces, structs, enums, delegates) ... } 43
  • 44. Classes class Stack { int[] values; int top = 0; public Stack(int size) { ... } public void Push(int x) {...} public int Pop() {...} } • Objects are allocated on the heap (classes are reference types) • Objects must be created with new Stack s = new Stack(100); • Classes can inherit from one other class (single code inheritance) • Classes can implement multiple interfaces (multiple type inheritance) 44
  • 45. Structs struct Point { int x, y; public Point(int x, int y) { this.x = x; this.y = y; } public MoveTo(int x, int y) {...} } • Objects are allocated on the stack not on the heap (structs are value types) + efficient, low memory consumption, no burden for the garbage collector. - live only as long as their container (not suitable for dynamic data structures) • Can be allocated with new Point p; // fields of p are not yet initialized Point q = new Point(); • Fields must not be initialized at their declaration struct Point { int x = 0; // compilation error } • Parameterless construcors cannot be declared • Can neither inherit nor be inherited, but can implement interfaces 45
  • 46. Visibility Modifiers (excerpt) public visible where the declaring namespace is known - Members of interfaces and enumerations are public by default. - Types in a namespace (classes, structs, interfaces, enums, delegates) have default visibility internal (visible in the declaring assembly) private only visible in declaring class or struct - Members of classes and structs are private by default (fields, methods, properties, ..., nested types) Example public class Stack { private int[] val; // private is also default private int top; // private is also default public Stack() {...} public void Push(int x) {...} public int Pop() {...} } 46
  • 47. Fields and Constants class C { int value = 0; Field - Initialization is optional - Initialization must not access other fields or methods of the same type - Fields of a struct must not be initialized const long size = ((long)int.MaxValue + 1) / 4; Constant - Value must be computable at compile time readonly DateTime date; Read Only Field - Must be initialized in their declaration or in a constructor - Value needs not be computable at compile time - Consumes a memory location (like a field) } Access within C Access from other classes ... value ... size ... date ... C c = new C(); ... c.value ... c.size ... c.date ... 47
  • 48. Static Fields and Constants Belong to a class, not to an object class Rectangle { static Color defaultColor; // once per class static readonly int scale; // -- " – // static constants are not allowed int x, y, width,height; // once per object ... } Access within the class Access from other classes ... defaultColor ... scale ... ... Rectangle.defaultColor ... Rectangle.scale ... 48
  • 49. Methods Examples class C { int sum = 0, n = 0; public void Add (int x) { // procedure sum = sum + x; n++; } public float Mean() { // function (must return a value) return (float)sum / n; } } Access within the class Access from other classes this.Add(3); C c = new C(); float x = Mean(); c.Add(3); float x = c.Mean(); 49
  • 50. Static Methods Operations on class data (static fields) class Rectangle { static Color defaultColor; public static void ResetColor() { defaultColor = Color.white; } } Access within the class Access from other classes ResetColor(); Rectangle.ResetColor(); 50
  • 51. Parameters Value Parameters (input values) - "call by value" void Inc(int x) {x = x + 1;} - formal parameter is a copy of the void f() { actual parameter int val = 3; - actual parameter is an expression Inc(val); // val == 3 } ref Parameters (transition values) - "call by reference" void Inc(ref int x) { x = x + 1; } - formal parameter is an alias for the void f() { actual parameter int val = 3; (address of actual parameter is passed) Inc(ref val); // val == 4 - actual parameter must be a variable } out Parameters (output values) - similar to ref parameters void Read (out int first, out int next) { first = Console.Read(); next = Console.Read(); but no value is passed by the caller. } - must not be used in the method before void f() { it got a value. int first, next; Read(out first, out next); } 51
  • 52. Variable Number of Parameters Last n parameters may be a sequence of values of a certain type. keyword array type params void Add (out int sum, params int[] val) { sum = 0; foreach (int i in val) sum = sum + i; } params cannot be used for ref and out parameters Use Add(out sum, 3, 5, 2, 9); // sum == 19 52
  • 53. Method Overloading Methods of a class may have the same name - if they have different numbers of parameters, or - if they have different parameter types, or - if they have different parameter kinds (value, ref/out) Examples void F (int x) {...} void F (char x) {...} void F (int x, long y) {...} void F (long x, int y) {...} void F (ref int x) {...} Calls int i; long n; short s; F(i); // F(int x) F('a'); // F(char x) F(i, n); // F(int x, long y) F(n, s); // F(long x, int y); F(i, s); // cannot distinguish F(int x, long y) and F(long x, int y); => compilation error F(i, i); // cannot distinguish F(int x, long y) and F(long x, int y); => compilation error Overloaded methods must not differ only in their function types, in the presence of params or in ref versus out! 53
  • 54. Constructors for Classes Example class Rectangle { int x, y, width, height; public Rectangle (int x, int y, int w, int h) {this.x = x; this.y = y; width = x; height = h; } public Rectangle (int w, int h) : this(0, 0, w, h) {} public Rectangle () : this(0, 0, 0, 0) {} ... } Rectangle r1 = new Rectangle(); Rectangle r2 = new Rectangle(2, 5); Rectangle r3 = new Rectangle(2, 2, 10, 5); • Constructors can be overloaded. • A construcor may call another constructor with this (specified in the constructor head, not in its body as in Java!). • Before a construcor is called, fields are possibly initialized. 54
  • 55. Default Constructor If no constructor was declared in a class, the compiler generates a parameterless default constructor: class C { int x; } C c = new C(); // ok The default constructor initializes all fields as follows: numeric 0 enum 0 bool false char '0' reference null If a constructor was declared, no default constructor is generated: class C { int x; public C(int y) { x = y; } } C c1 = new C(); // compilation error C c2 = new C(3); // ok 55
  • 56. Constructors for Structs Example struct Complex { double re, im; public Complex(double re, double im) { this.re = re; this.im = im; } public Complex(double re) : this (re, 0) {} ... } Complex c0; // c0.re and c0.im are still uninitialized Complex c1 = new Complex(); // c1.re == 0, c1.im == 0 Complex c2 = new Complex(5); // c2.re == 5, c2.im == 0 Complex c3 = new Complex(10, 3); // c3.re == 10, c3.im == 3 • For every struct the compiler generates a parameterless default constructor (even if there are other constructors). The default constructor zeroes all fields. • Programmers must not declare a parameterless constructor for structs (for implementation reasons of the CLR). 56
  • 57. Static Constructors Both for classes and for structs class Rectangle { ... static Rectangle() { Console.WriteLine("Rectangle initialized"); } } struct Point { ... static Point() { Console.WriteLine("Point initialized"); } } • Must be parameterless (also for structs) and have no public or private modifier. • There must be just one static constructor per class/struct. • Is invoked once before this type is used for the first time. 57
  • 58. Destructors class Test { ~Test() { ... finalization work ... // automatically calls the destructor of the base class } } • Correspond to finalizers in Java. • Called for an object before it is removed by the garbage collector. • No public or private. • Is dangerous (object resurrection) and should be avoided. 58
  • 59. Properties Syntactic sugar for get/set methods class Data { property type property name FileStream s; public string FileName { set { s = new FileStream(value, FileMode.Create); } get { "input parameter" return s.Name; of the set method } } } Used as "smart fields" Data d = new Data(); d.FileName = "myFile.txt"; // invokes set("myFile.txt") string s = d.FileName; // invokes get() JIT compilers often inline get/set methods no efficiency penalty 59
  • 60. Properties (continued) get or set can be omitted class Account { long balance; public long Balance { get { return balance; } } } x = account.Balance; // ok account.Balance = ...; // compilation error Why are properties a good idea? • Interface and implementation of data may differ. • Allows read-only and write-only fields. • Can validate a field when it is assigned. • Substitute for fields in interfaces. 60
  • 61. Indexers Programmable operator for indexing a collection class File { type of the name type and name FileStream s; indexed expression (always this) of the index value public int this [int index] { get { s.Seek(index, SeekOrigin.Begin); return s.ReadByte(); } set { s.Seek(index, SeekOrigin.Begin); s.WriteByte((byte)value); } } } Use File f = ...; int x = f[10]; // calls f.get(10) f[10] = 'A'; // calls f.set(10, 'A') • get or set method can be omitted (write-only / read-only) • Indexers can be overloaded with different index types 61
  • 62. Indexers (other example) class MonthlySales { int[] product1 = new int[12]; int[] product2 = new int[12]; ... public int this[int i] { // set method omitted => read-only get { return product1[i-1] + product2[i-1]; } } public int this[string month] { // overloaded read-only indexer get { switch (month) { case "Jan": return product1[0] + product2[0]; case "Feb": return product1[1] + product2[1]; ... } } } } MonthlySales sales = new MonthlySales(); ... Console.WriteLine(sales[1] + sales["Feb"]); 62
  • 63. Overloaded Operators Static method for implementing a certain operator struct Fraction { int x, y; public Fraction (int x, int y) {this.x = x; this.y = y; } public static Fraction operator + (Fraction a, Fraction b) { return new Fraction(a.x * b.y + b.x * a.y, a.y * b.y); } } Use Fraction a = new Fraction(1, 2); Fraction b = new Fraction(3, 4); Fraction c = a + b; // c.x == 10, c.y == 8 • The following operators can be overloaded: – arithmetic: +, - (unary and binary), *, /, %, ++, -- – relational: ==, !=, <, >, <=, >= – bit operators: &, |, ^ – others: !, ~, >>, <<, true, false • Must return a value 63
  • 64. Conversion Operators Implicit conversion - If the conversion is always possible without loss of precision - e.g. long = int; Explicit conversion - If a run time check is necessary or truncation is possible - e.g. int = (int) long; Conversion operators for custom types class Fraction { int x, y; ... public static implicit operator Fraction (int x) { return new Fraction(x, 1); } public static explicit operator int (Fraction f) { return f.x / f.y; } } Use Fraction f = 3; // implicit conversion, f.x == 3, f.y == 1 int i = (int) f; // explicit conversion, i == 3 64
  • 65. Nested Types class A { int x; B b = new B(this); public void f() { b.f(); } public class B { A a; public B(A a) { this.a = a; } public void f() { a.x = ...; ... a.f(); } } } class C { A a = new A(); A.B b = new A.B(a); } For auxiliary classes that should be hidden - Inner class can access all members of the outer class (even private members). - Outer class can access only public members of the inner class. - Other classes can access an inner class only if it is public. Nested types can also be structs, enums, interfaces and delegates. 65