CSharp - Generic Type default Value

Introduction

The default keyword is used to get the default value for a generic type parameter.

The default value for a reference type is null, and the default value for a value type is the result of bitwise-zeroing the value type's fields:

Type Default value
All reference types null
All numeric and enum types 0
char type '\0'
bool type false

The following code uses default keyword the fill default value for array element.

Demo

using System;
class MainClass/*  w w  w  .  ja va  2 s .  c  om*/
{
    public static void Main(string[] args)
    {
        int[] iArr = CreateArray<int>(new int[3]);
        foreach (int i in iArr)
        {
            Console.WriteLine(i);
        }
    }
    static T[]  CreateArray<T>(T[] array)
    {
        for (int i = 0; i < array.Length; i++)
            array[i] = default(T);
        return array;
    }
}

Result