Click here to Skip to main content
15,886,362 members
Articles / Programming Languages / C#
Tip/Trick

Default(T)

Rate me:
Please Sign up or sign in to vote.
3.91/5 (7 votes)
23 Sep 2017CPOL1 min read 12.7K   28   2   2
Use and examples for the keyword Default in generics

Introduction

Default represents default value of T parameter in generics instructions. In several cases, the default keyword is an absolute unknown and we think it’s unnecessary or its functionality is null. There are some development moments with the generics classes where default keyword can be useful.

Default keyword can make our work easier and it may make the code safer.

Default(T)

This keyword returns the default value of type parameter.

These are the default values for the more important types inside of CLR:

  • Classes -> null.
  • Nullable<T> -> null.
  • Numerics structs (int, double, decimal, etc) -> 0.
  • DateTime structs -> 01/01/0001.
  • Char structs -> empty char.
  • Bool structs -> false.

Evaluation Initializes T Parameters

This is a very simple generic method example. This method has two ref parameters of T type and changes your values in the implementation.

C#
public static void ChangeValues<T>(ref T a, ref T b)
{
    T _a = a;
    T _b = b;

    a = _b;
    b = _a;
}

The ChangeValues method hasn’t generic restriction filter so that T will be any type (reference type or value type)

If necessary, validate if the parameters have been initialized (has a valid value different to initialize value), add a validation chunk:

C#
public static void ChangeValues<T>(ref T a, ref T b)
{
    if(a.Equals(null) || b.Equals(null))
    {
        throw new ArgumentException("....");
    }

    T _a = a;
    T _b = b;

    a = _b;
    b = _a;
}

We have a problem, because this implementation doesn’t verify the default value types.

It will be a wrong execution:

C#
static void Main(string[] args)
{
    DateTime a = new DateTime();  // bad value
    DateTime b = DateTime.Today;

    System.Console.WriteLine($"a --> {a}");
    System.Console.WriteLine($"b --> {b}");

    System.Console.WriteLine();

    ChangeValues(ref a, ref b);

    System.Console.WriteLine($"a --> {a}");
    System.Console.WriteLine($"b --> {b}");

    System.Console.Read();
}

Console output:

Image 1Image 2

To correct this bug, we will a new method version with the default keyword inclusion:

C#
public static void NewChangeValues<T>(ref T a, ref T b)
{

    if (a.Equals(default(T)) || b.Equals(default(T)))
    {
        throw new ArgumentException("....");
    }

    T _a = a;
    T _b = b;

    a = _b;
    b = _a;
}

Create Empty Instance

Another example could be the possibility to create empty instance of T types without reflection (Activator).

C#
public T  CreateEmptyInstace<T>(T item)
{
    //var result = Activator.CreateInstance<T>(); à Expression
    var result = default(T);

    return result;
}

Reset Generics Instances

Another use case for Default(T) could be for reset instance of generics objects.

C#
public void ResetValue<T>(ref T item)
{
    item = default(T);
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Cecabank
Spain Spain
MVP C# Corner 2017

MAP Microsoft Active Professional 2014

MCPD - Designing and Developing Windows Applications .NET Framework 4
MCTS - Windows Applications Development .NET Framework 4
MCTS - Accessing Data Development .NET Framework 4
MCTS - WCF Development .NET Framework 4

Comments and Discussions

 
Questiondefault int value Pin
Khayralla25-Sep-17 8:23
Khayralla25-Sep-17 8:23 
AnswerRe: default int value Pin
Juan Francisco Morales Larios25-Sep-17 13:00
Juan Francisco Morales Larios25-Sep-17 13:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.