Programming C#

Florian Rappl, Department of Theoretical Physics, University of Regensburg

Programming C#

Day 2: Object oriented programming (OOP), Generics and Windows Forms projects

Content

  • Again: Difference class and struct
  • Constructor and inheritance
  • Keywords and syntax
  • Polymorphism
  • Modifiers
  • Generic classes and methods
  • Implementing interfaces
  • Example of OOP: Windows Forms

Structures and classes

  • Structures are value types and will be copied
  • They are non-nullable (no instantiation required)
  • Classes are reference types and will be referenced
  • They are null if not instantiated
  • Polymorphism is possible with both
  • Structures can only inherit from interfaces

→ Example - Structures and classes

Structures.cs

Constructor

  • The constructor is always called
  • If no constructor is specified, a default one is used
  • Constructors can call other constructors of the same class
  • Or a constructor of the base class
  • By default the default constr. of the base class is called
  • Calling order: Call the other constructor, then invoke local code

Inheritance

  • In C# a class can only inherit from one class
  • Structures and classes can implement n interfaces
  • Access the classes members with the this pointer
  • Exclusively access base class members with base
  • Modifiers are very important
  • Interfaces do not need modifiers, all public

Inheritance flow

Modifiers

  • Access only from current class: private
  • Access also from inherited classes: protected
  • Access also from whole library: internal
  • Access to everyone: public
  • Additionally: internal protected, i.e. internal or protected
  • Default: class / struct / interface / delegate / enum is internal, a member of a class or structure is private

Polymorphism

  • Hiding is always possible
  • Recommended technique: mark method as new
  • New implementation only if marked as abstract (no basic implementation available) or virtual
  • Mark new implementation as override
  • Childs need to specify base constructor if no def. constructor available

→ Example - Inheritance and polymorphism

Polymorphism.cs

Generics

  • Generics is another way of reusing code
  • The goal is to generate (similar) classes that differ in one or more types
  • Best example: List<T> - strongly typed collection (list)
  • Compiler generates the classes for all types that are used in our code
  • If we use List<int>, List<double>, two classes will be generated by the compiler
  • Requirement: Specifying the type identifiers (like method arguments) and using them (e.g. name T with usage T myvariable)

Compiler generated classes

Generic methods

  • Not only classes can be generated, also methods
  • Similar syntax, but (mostly) different (easier) usage
  • For methods the compiler can (mostly) infer the type
public static void Swap<T>(ref T l, ref T r) {
	T temp = r; r = l; l = temp;
}
int a = 3; int b = 4; Swap(ref a, ref b);

Generic constraints

  • Generics alone are powerful, yet very limited
  • By setting constraints some limitations can be overcome
  • The magic keyword is where
  • Several possibilities e.g. new() means has def. constructor
  • One example: void Test<T>(T obj) where T : new()
  • More constraints possible, e.g. T : Stopwatch, new()
  • Constraints with multiple types? Separate with where

→ Example - Generics

Generics.cs

Interfaces

  • Interfaces are like contracts
  • They say nothing about internal structures
  • Usages: ability to have multi-inheritance and specifying abilities
  • Can be implemented implicit (like members) or explicit (access only if casted to the specific interface type)
  • Interfaces can inherit from other interfaces

Abstract classes

  • Abstract class is like an interface with implementations
  • Just use abstract before class
  • This enables the usage of abstract methods and properties
  • Such members have to be implemented
  • No instances of abstract classes can be created
  • Use it if you want something stronger than a contract
  • Keep in mind: A class can only inherit from one other class

→ Example - Shaping classes

Interfaces.cs

Windows Forms

  • One of several GUI possibilities
  • First one implemented in .NET
  • Start a project: File, New, Project, C#, Windows Forms Application
  • The integrated designer will help us
  • There is a big OOP tree behind this
  • For us most important is Control
  • A really important derivative is Form
  • Form represents a window

Understanding GUI

  • We need a loop that performs updates
  • That loop is actually integrated in Windows, we just need to start it
  • Windows will then register our app and post messages
  • Such messages arrive in our app as events
  • An event can be something like mouse over, click, key down, ...
  • The state of controls is based on such events (like hover)
  • Additionally we can register our own handlers
  • Right now its enough to do it with the designer

The message loop

→ Example - Windows Forms

SimpleForm.cs

All available presentations

More questions? Just mail!

MVP

Florian Rappl, MVP Visual C#