Notes
Slide Show
Outline
1
Introduction to C#
  • Andrew Scott
    Computing Department
    Lancaster University, UK
2
C#
  • Modern Object-Oriented language


  • Based on Microsoft .net framework
    • Also, a Component-Oriented language

  • Based on C
    • Inevitably looks like Java
      • Many subtle
        …and some not so subtle differences

3
C# Keywords
4
Statements Must Do Work
  • Unlike in C
    • statements cannot just be expressions…
5
Conditionals: if
  • Conditions must be boolean
    • Must always evaluate to true or false
6
Conditionals: if
  • Conditions must be boolean
    • Must always evaluate to true or false
7
Operators
8
Switch
  • All but the last case
    must break or goto
    • Unless no work done

  • Order un-important
9
Switch
  • All but the last case
    must break or goto
    • Unless no work done

  • Order un-important
10
Loops: do and while
11
Loops: for
12
.NET Primitive Types
13
.NET Primitive Types
14
.NET Primitive Types
15
.NET Primitive Types
  • All languages adopt framework types
    • So… a string is a string
      whatever the language

  • Unified type system
    • Everything is an object
16
Type
Aliases
17
Quoted Strings: @ verbatim
  • string s = “C:\\Program Files\\”;


  • string s = @“C:\Program Files\”;
  • string s = @“
    This is still
    and ““Hello!”” to you”;


  • public void @public { . . . }
18
.NET Type System
  • Reference types
    • Classes
    • Interfaces
    • Arrays
    • Delegates
  • Value types
    • Primitives
    • Enumerators
    • Structures
19
Boxing and Un-boxing
20
Boxing and Un-boxing
21
Boxing and Un-boxing
22
Classes and Instances
23
Variable and Constants
24
Class and Instance Methods
25
Class and Instance Methods
26
The System.Object Class
  • Basis for all other objects


  • System.Object (object)
    • And therefore every object supports:
      • Equals()
      • GetHashCode()
      • GetType()
      • ToString()
27
Properties
28
Exceptions (try and catch)
29
Exceptions (try and catch)
30
Using Exceptions
31
Using Exceptions
32
Using Exceptions
33
Using Exceptions
34
Creating New Exceptions
35
Creating New Exceptions
36
…and Finally
37
C# Keywords