« July 2004 | Main | September 2004 »

August 30, 2004

Whidbey, Visual Basic event handlers

In Visual Basic, event handlers are defined and registered declaratively in one step by adding a “Handles <instance>.<event>” clause to a Sub or Function declaration whose signature matches the event’s delegate.

Because the declarative style is used, as opposed to C#’s imperative style (event += new <delegate>), there is no control over the lifetime of the event registration. This can require additional code and effort when the lifetime of the event source extends beyond the lifetime of a resource required by the handler.

A second limitation of Visual Basic event handler support appears to be that a generic delegate defined in C# can not be used to define an event if you want Visual Basic clients to be able to bind to it.

 

August 03, 2004

C# changes from 1.1 to 2.0

As of Beta 1.

C# 1.1 à 2.0

Generic classes, interfaces, and methods. Introduces the <type,argument,list> syntax.

Generic constraints. Introduces the syntax: “where T : (class_type | “class” | “struct” ,)? (interface_type | type_parameter ,)* (“new()”)?

Anonymous methods. Re-uses “delegate” and curly braces to declare and define inline methods.

Iterators. Methods that return IEnumerator or IEnumerable (or generic variants) may use “yield return” and “yield break” syntax within a loop to yield control on each item.

Partial types. Introduces “partial” syntax.

Nullable types. Introduces “int?” syntax for all value types and “??” operator.

 

 

Use using to alias long generic type names

Assigning a type name alias with a “using” directive in C# is a handy way to shorten a long type name and simplifies using generic types much as you would normally do with a typedef in C++.

namespace Foo {

            using Stack1 = Stack<Bar<int, string>>;

            … use Stack1 as a type alias for Stack<Bar<int, string>> …

}