CSharp - Generic Type default Value


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.


using System;
class MainClass//from   ww  w  . j a  v a  2s  . co m
    public static void Main(string[] args)
        int[] iArr = CreateArray<int>(new int[3]);
        foreach (int i in iArr)
    static T[]  CreateArray<T>(T[] array)
        for (int i = 0; i < array.Length; i++)
            array[i] = default(T);
        return array;