C# Feature Development


This tutorial outlines the major developments and improvements of the C# language through time. This tutorial came about as I was preparing for a technical interview, and wanted to be sure I had been refreshed on all of the major features of the C# language.

Do you need to know all of these features to make C# programs? No. But two things: first, even someone new to C# should know that they exist, and second, if you know about them, you'll find uses for them.

These features are broken down into the version in which they were introduced. Each feature is then listed, along with a brief description of what the feature is, why or when you would use it, and an example of how to do it.

All of these features deserve a larger discussion of them. Some of them even have entire books dedicated to them. This tutorial, however, is designed to be a simple introduction tutorial to these features.

C# 2.0


What: You can create type-safe data structures without committing to an actual data type.

An example of this that you have already probably used is the List class. Before this feature was added, you'd have to create a list class that contained things of the type object. That way, you could put in just about anything into the list. Of course, this has a couple of problems. First, if your list was supposed to be containing strings, you'd need to cast everything in the list to a string before you could use it. Second, it is possible that the list that you have that is supposed to contain strings might actually contain something with a different type. You'd have to also handle casting error problems in all of these cases.

OK, so an alternative is to create a ListOfString type, which contains only strings. This solves the casting problems, but it means you'd have to create a ListOfString, a ListOfInt, a ListOfDouble, and so on, for every conceivable data type, which is not very realistic.

Enter generics. You can create a special class, that can refer to a "generic" type. When you actually go to use the class, you can specify at that point, what the specific type of data you are going to store in it.

It is also worth mentioning that the C# implementation is different from similar features in C++ and Java, in that the underlying bytecode actually supports generics. C++ and Java essentially do what I described earlier, compiling a ListOfString (or whatever the type is) for the different types your software uses.

When: The description above should be a good explanation of when you'd use this, but for the sake of completeness, the time that you'd want to use generics is when you have a class that contains or operates on a specific type of data, but at the moment, you don't know what exactly that type is going to be. A few examples would be a stack, a queue, a heap, a binary search tree… the list goes on and on.

How: Generics is done in two parts. First, you create the class that uses generics, then, elsewhere in the code, you use the generic class, specifying the type you want to use.

This first code snippet shows you how to make a generic class:

// This class uses the generic type T.  T can represent any type.
public class Stack<T>
    private T[] items; // This creates an array of the generic type.
    // This method will return the generic type, whatever it happens to be.
    public T Pop()
        // Some interesting code goes here...
    // This method passes in the generic type as a parameter.
    public void Push(T itemToAdd)
        // Some interesting code goes here...

Then, to use the class, you do this:

Stack<string> stackOfStrings = new Stack<string>(); // This creates a new Stack object containing the type "string".
stackOfStrings.Push("Hello World!"); // This calls the Push method, which C# now knows must pass in strings for this particular Stack.
string topMessage = stackOfStrings.Pop(); // The Pop method will return the type "string" here, so no need to cast anything.

Partial Types

What: A cool mechanism that allows to you define classes or methods spread out over multiple files.

When: A common place that this occurs is with auto-generated code that Visual C# Express/Visual Studio makes for you for things like GUI classes. (The IDE has a part of the class it modifies, while you have a part that you modify.) This can also be helpful if you've got a very large class that you want to break down into more manageable files (though perhaps if it is really that big, you should consider redesigning the class into multiple classes, each with their own file).

How: For classes, add the partial keyword to a class declaration, immediately before the class keyword, like this:

public partial class MyClass

Do this in each place that the partial class will appear. When compiling, C# will basically stick all of the "partial" pieces of code together into one big one.

For methods, you can use the partial keyword to declare a method in one file and an implementation in another file. (This, by the way, is how C++ classes are usually done.)

// In one file, you can do this:
public partial void DoSomething();
// In a second file, you can then do this:
public partial void DoSomething()
    // Insert your code here...

Anonymous Methods


Nullable Types


C# 3.0

Implicitly Typed Local Variables

Object and Collection Initializers

Auto-Implemented Properties

Extension Methods

Lambda Expressions

Expression Trees

C# 4.0

Dynamic Binding

Named and Optional Arguments

Generic Covariance and Contravariance

C# 5.0 - Future Version

Compiler as a Service

Asynchronous Methods