In this short series, I outline the notes that I made while reading the Programming in C# (Exam 70-483) book. These are my personal notes and are in no way official notes from Microsoft.

Please bear in mind that I wrote these notes during my maternity leave in 2015. As of January 2021, the Microsoft exam 70-483: Programming in C# has now been retired. My suggestion is that you are using these notes, please use them as learning resources only and not exam resources.

For previous notes within this blog series please see Programming in C# (Exam 70-483) Notes.

Delegates

e.g. public delegate int Name (int x, int y)

public delegate void Action <in T1, in T2> (T1 arg1, T2 arg2)

public delegate void Func <T1, in T2, out Result> (T1 arg 1, T2 arg 2)

  • A delegate:
    • defines a method’s parameters and return type
    • is a type that references a method.
    • Routes data from A -> B
  • Use a delegate when you want to use a class’s method as a parameter
  • Use + – to combine delegate variables
  • Covariance lets a method return a value from a subclass of the result expected by a delegate
  • Contravariance lets a method take parameters that are from a superclass 

Creating anonymous methods using LAMBDA expressions

Action note1 = () => MessageBox.Show(“Hi”);

Action<string> note2 = message => MessageBox.Show(message);

Action<string> note3 = (message) => MessageBox.Show(message);

Action<string> note4 = (string message) => MessageBox.Show(message);

Func <float, float> square = (float x) => x*x;

Use a delegate to define an event:

public event OverdrawnEventHandler Overdrawn;

You can use pre-defined delegates as events:

public event Action Name;