Main
3rd lecture -- globalization and windows forms
Slides
Source codes
- ComparingExample.cs
- CalendarExample.cs
- ResourceExample.zip
- DelegatesExample.cs
- EventsExample.cs
- FormEventsExample.zip
- PositioningExample.zip
Notes, corrections etc.
- Contravariance of delegates -- In the lecture, I have tried to show you that delegates in C# are contravariant (see MSDN article). First, let me show you a correct example of contravariance, similar to the one from the MSDN article:
class MainClass
You may see that the delegate
{
delegate int MyDel(System.Collections.ArrayList l);
public static int ReturnSomething(Object e)
{
return 1;
}
public static void Main(string[] args)
{
System.Collections.ArrayList a = new System.Collections.ArrayList() {4,5,6};
MyDel m = new MyDel(ReturnSomething);
Console.WriteLine(m(a));
}
}m
is assigned a methodReturnSomething
, although the parameter types do not match exactly (ArrayList is derived from Object). There is an intuitive explanation of why the above example should work: the method assigned tom
should be able to takeArrayList
and return an integer. However, even the methodReturnSomething
can do this, and it is not important that the method can take more general parameters as inputs.
Now, here is the (incorrect) example from the lecture:class MainClass
We get an error in compile time. To see why it does not work, read this discussion (note that they talk about covariance, but the explanation is valid even for contravariance). Just a brief summary of the discussion: from very strict point of view, value types such as
{
delegate int MyDel(int i);
public static int ReturnZero(Object e)
{
return 1;
}
public static void Main(string[] args)
{
MyDel m = new MyDel(ReturnZero);
Console.WriteLine(m(0));
}
}
int
are not derived fromobject
, only boxed versions of value types are. Boxing and unboxing is done implicitly in many cases (e.g. when passing an integer to a method that acceptsObject
), but not in this case.