Senator Guerra Souty original series calendar,replica hublot blue steel peach pointer collocation of rolex replica Rome digital scale, track type minute replica watches scale shows that the classical model is swiss replica watches incomparable, wearing elegant dress highlights.
mr-ponna.com

 

YOU ARE HERE: HOME Questions How to initialize a default value when you know its data type



Use of C# 'default' keyword

View(s): 13055

How to initialize a default value when you know its data type?

Answer 1)

Using default keyword we can initialize default value for its type like as shown below:

public int amount = default(System.Int32);

The default key word is normally used to initialise a member in a generic classes. In generic classes and methods, one issue that arises is how to assign a default value to a parameterized type T when you do not know the following in advance: 
  1. Whether T will be a reference type or a value type.
  2. If T is a value type, whether it will be a numeric value or a struct.

For example, here’s an example from MSDN help

public class GenericList<T>
{
  private class Node
  {
      //
      public Node Next;
      public T Data;
  }
 
  private Node head;
  public T GetNext()
  {
      T temp = default(T);
  }
}

The type 'T' is parameterised type for the template. With the line in bold the type 'T' could be a numeric value (so the variable should be initialised to '0') or a reference (and so should be initialised to NULL). Using default here makes sure the correct initialisation takes place.

  Asked in:  Inooga Solutions   Expertise Level:  Intermediate
  Last updated on Thursday, 05 April 2012
3/5 stars (5 vote(s))

Register Login Ask Us Write to Us Help