Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does default(object); do in C#?

Tags:

c#

People also ask

What is the default object?

The default(object) is a way to null. This is asking the C# compiler to add the default value of the object type, which is null.

What is the purpose of default keyword in C?

You can use the default keyword in the following contexts: To specify the default case in the switch statement. As the default operator or literal to produce the default value of a type. As the default type constraint on a generic method override or explicit interface implementation.

What is the default for objects C#?

The object and string types have a default value of null, representing a null reference that literally is one that does not refer to any object.

What is the default value for an object of type object?

Objects. Variables of any "Object" type (which includes all the classes you will write) have a default value of null.


  • For a reference-type, it returns null
  • For a value-type other than Nullable<T> it returns a zero-initialized value
  • For Nullable<T> it returns the empty (pseudo-null) value (actually, this is a re-statement of the second bullet, but it is worth making it explicit)

The biggest use of default(T) is in generics, and things like the Try... pattern:

bool TryGetValue(out T value) {
    if(NoDataIsAvailable) {
        value = default(T); // because I have to set it to *something*
        return false;
    }
    value = GetData();
    return true;
}

As it happens, I also use it in some code-generation, where it is a pain to initialize fields / variables - but if you know the type:

bool someField = default(bool);
int someOtherField = default(int)
global::My.Namespace.SomeType another = default(global::My.Namespace.SomeType);

default keyword will return null for reference types and zero for numeric value types.

For structs, it will return each member of the struct initialized to zero or null depending on whether they are value or reference types.

from MSDN

Simple Sample code :<br>
    class Foo
    {
        public string Bar { get; set; }
    }

    struct Bar
    {
        public int FooBar { get; set; }
        public Foo BarFoo { get; set; }
    }

    public class AddPrinterConnection
    {
        public static void Main()
        {

            int n = default(int);
            Foo f = default(Foo);
            Bar b = default(Bar);

            Console.WriteLine(n);

            if (f == null) Console.WriteLine("f is null");

            Console.WriteLine("b.FooBar = {0}",b.FooBar);

            if (b.BarFoo == null) Console.WriteLine("b.BarFoo is null");

        }
    }

OUTPUT:

0
f is null
b.FooBar = 0
b.BarFoo is null

Default value of MyObject. See default Keyword in Generic Code (C# Programming Guide) (MSDN):

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:

  • Whether T will be a reference type or a value type.
  • If T is a value type, whether it will be a numeric value or a struct.

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. For structs, it will return each member of the struct initialized to zero or null depending on whether they are value or reference types. The following example from the GenericList class shows how to use the default keyword. For more information, see Generics Overview.

public class GenericList<T>
{
    private class Node
    {
        //...

        public Node Next;
        public T Data;
    }

    private Node head;

    //...

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

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

Specifies the default value of the type parameter.This will be null for reference types and zero for value types.

See default


The default keyword returns the "default" or "empty" value for a variable of the requested type.

For all reference types (defined with class, delegate, etc), this is null. For value types (defined with struct, enum, etc) it's an all-zeroes value (for example, int 0, DateTime 0001-01-01 00:00:00, etc).

It's mostly used with generic code that can be applied to both reference and value types, because you can't assign null to a value type variable.