Did you know? DZone has great portals for Python, Cloud, NoSQL, and HTML5!

Gil Fink is an expert in ASP.NET and Microsoft data platform and serves as a Senior .Net Consultant and Architect at Sela Group. He is a Microsoft data platform MVP and a certified MCPD Enterprise Application Developer. Gil has worked in the past in variety of positions and projects as a leading developer, team leader, consultant and more. His interests include Entity Framework, Enterprise Library, WCF, LINQ, ADO.NET and many other new technologies from Microsoft. Gil is a DZone MVB and is not an employee of DZone and has posted 98 posts at DZone. You can read more from them at their website. View Full User Profile

Back to Basics – The Default Keyword in Generic Code

10.20.2008
Email
Views: 3193
  • submit to reddit

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
Published at DZone with permission of Gil Fink, author and DZone MVB. (source)

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