Sunday, February 10, 2008

.NET Delegates, Indexers and The Lambda Operator (.NET Framework 3.5)

The following 3 topics are useful advanced C# Features and have matured in .NET Framework 3.5.


Traditionally in other development environment such as VB, C or C++ in order to access the Win32 API you would use callback functions or call backs. The problem with this style and method is that you access a raw memory address. In the .Net Framework, Call back functions are known as delegates and provide a much better type safety and object oriented way of accessing the functions.

So the proper definition of a Delegate in .net a type safe object that points to another method or a list of methods and can be invoked or called at a later time. .Net Delegates can point to either static or instance methods.

When you want to creat a delegate in C# you use the keyword delegate.

You can read up more on C# Delegates at MSDN.

The Lambda Operator =>

Lambda expressions allow you to author anonymouse methods in a more consise way that ultimately simplifies the uses of .NET Delegate types.

You can read up more on C# Lambdas at

Indexer Methods

Indexer methods allow you to design custom classes and structures that can be indexed like arrays which is the most useful when dealing with generic or non generic collection types.

You can read up more on C# Indexers at CSharp Help.

No comments: