C# default Generic Value

In this chapter you will learn:

  1. What is default Generic Value
  2. Example for default Generic Value

Description

The default keyword can be used to get the default value given 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:

Example

Example for default Generic Value


static void Zap<T> (T[] array)
{//from www  . j a  va2s  .c  o  m
  for (int i = 0; i < array.Length; i++)
    array[i] = default(T);
}

Next chapter...

What you will learn in the next chapter:

  1. What are Generic Constraints
  2. Type of constraints
  3. Example for Generic Constraints
  4. Note for Generic Constraints
  5. Example for base class constraint or interface constraint
  6. Example for class constraint and struct constraint
  7. Example for parameterless constructor constraint
  8. Example for naked type constraint
Home »
  C# Tutorial »
    C# Types »
      C# Generics
C# Generic Types
C# Generic Methods
C# default Generic Value
C# Generic Constraints
C# Subclassing Generic Types
C# Generic Delegate
C# Type Parameters