Back to Basics – The Default Keyword in Generic Code

In the following posts I’m going to describe basic tools which every developer needs to know. In today’s post I’m going to explain what is the default keyword in generic code and how to use it

What is the Default Keyword?

The default keyword has dual meaning since .Net framework 2.0 was published. The first meaning is inside the switch construct to inform the switch that if non of the cases is valid then the switch will go to the default block. The second meaning was new in .Net framework 2.0 and it was to set a type parameter to its default value. The need for that came from generics which enables us to make a type parameter which is not known until runtime. This behavior raised a question – how to assign a default value to a parameterized type T when you do  not know in advance whether that type is value type, struct or a reference type.

To our rescue came the default keyword. The default keyword when operated on a parameterized type T will return null for reference types and 0 for value types. For structs, each of the struct’s members will
be initialized according to its type – value types to 0 and reference types to null.

How to Use the Default Keyword?

Lets look at a simple example of how to use the default keyword. In the example I use the following class:

class MyClass<T>
{

#region Properties

/// <summary>
/// The value that MyClass holds
/// </summary>

public T Value { get; private set; }

#endregion

#region Ctor

/// <summary>
/// Construct a MyClass object and set all
/// properties to their default state
/// </summary>

public MyClass()
{

Value = default(T);
}


#endregion

}

The class is insert the default value for its Value property in the constructor. Then run the following code:

class Program
{
static void Main(string[] args)
{
MyClass<int> myClassInt = new MyClass<int>();

MyClass<DateTime> myClassDT = new MyClass<DateTime>();

MyClass<object> myClassObj = new MyClass<object>();

Console.WriteLine("myClassInt Value property: {0}", myClassInt.Value);

Console.WriteLine("myClassDT Value property: {0}", myClassDT.Value);

Console.WriteLine("myClassObj Value property: {0}", myClassObj.Value);

Console.ReadLine();
}
}

and the output will be:

Pay attention, if I try to make operations on myClassObj.Value I’ll get an exception
because it is being set to null.

Summary

Lets sum up the post, the default keyword is very useful in order to initialize a type parameter to it’s default value. The keyword is very helpful when you need to reset a class with generic parameters such as a custom generic Enumerator for example. I use the keyword a lot in converter classes which have generic parameters.

References
0
Average: 5 (1 vote)

Gil Fink is working at Sela Group as a .NET consultant. Gil graduated his computer science BA degree in the Interdisciplinary Center in Israel with honours. Also, Gil holds a Microsoft's MCPD enterprise application developer title. Gil's fun is to develop infrastructure for projects, solve logic and design problems and to return home to his baby boy Oron and his loving wife Liora. Gil's interests includes variety of things including Enterprise Library, WCF, LINQ, Entity Framework or whatever Microsoft is throwing at him. Gil is a DZone MVB and is not an employee of DZone and has posted 14 posts at DZone. You can read more from them at their website.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)