Tech Ed 2005 - Monday's Technology Highlights
C# 2.0 by Anders Hejlsberg was my favorite session today. Here are a few points worth noting...
- Generics
- Can be applied at class, struct, interface, delegate, and method level
- When applied at method level, there is no need to specify the type when using. For example:
// method declaration using generic
public void MyMethod<T>(T arg) {// implementation }
// examples of calling method explicitly
x.MyMethod<int>(5);
x.MyMethod<string>("Whassup");
// examples of calling method without type (known as Type Inference)
x.MyMethod(5);
x.MyMethod("Whassup"); - To set a Generic to a default initilized state use default(T)where T is the generic
- If casting from a Generic, cast first to object, then to desired type. For example:
// t is a variable of a generic type
int i = (int)(object)t; - Anonymous Methods
- Here is an example of applying code directly to a click event:
// code block is provided instead of pointing to System.Eventhandler delegate
myButton.Click += delegate { MessageBox.Show("Muhahaha!"); }; - Nullable Types
- When the need arises to treat a value type as a null, here is how it can be done with C#:
// adding the ? to the value type makes it a Nullable Type
int? i = null; - Static Classes
- Apply the static keyword to a class to enforce static only members.