first_page the funky knowledge base
personal notes from way, _way_ back and maybe today

C# 2.0 Design Notes: default Keyword in Generic Code

From the C# Programming Guide:

"Given a variable t of a parameterized type T, the statement t = null is only valid if T is a reference type and t = 0 will only work for numeric value types but not for structs. The solution is to use the default keyword, which will return null for reference types and zero for numeric value types."

public T GetNext()
{
    T temp = default(T);

    Node current = head;
    if (current != null)
    {
        temp = current.Data;
        current = current.Next;
    }
    return temp;
}

For more information, see:

http://msdn2.microsoft.com/en-us/library/xwth0h0d.aspx
mod date: 2006-07-12T23:22:21.000Z