What Does Default(Object); Do in C#

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

  • 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);

what is the difference between the keywords default and new

default is used for zeroing out values. For reference types, thats null. For value types, that is effectively the same as using new without any arguments. default is great for generics.

new creates an instance of that type, invoking the constructor.

In your example, if I do:

Loan loan = default(Loan);

or in newer versions of C#:

Loan loan = default;

which is logically equivalent to

Loan loan = null;

you will get a null reference exception if you don't construct it:

loan.MakePayment(100); // Throws

Default value for user defined class in C#

To chime in with the rest, it will be null, but I should also add that you can get the default value of any type, using default

default(MyClass) // null
default(int) // 0

It can be especially useful when working with generics; you might want to return default(T), if your return type is T and you don't want to assume that it's nullable.

parameter with default object value

You would need to do it inside the method and provide a comment that the method accepts null and uses A(10) as a default value.

void f(A a = null)
{
if(a == null)
a = new A(10);
}

Do all objects have a default value?

Do all objects have a default value?

Absolutely not. For example, the string "abc" is an object, but it does not have a "default value". The number 12 is an object, but it does not have a "default value".

However, all types have a default value. Remember, objects are instances of types; objects exist at runtime. Types are a compile-time concept. Do not confuse types with objects; they are as different as the string "The New York Times" and an actual copy of today's New York Times.

Values that may be stored in a variable of reference type are either references to objects or null. Hence the name "reference type": a value of a variable of reference type is a reference (or null).

Values that may be stored in a variable of value type are objects that are the values of that type. Hence the name "value type" - the value of a variable of value type is a value.

(I omit pointer types from the discussion; for our purposes, assume that all pointer types are logically the same as the value type IntPtr.)

The default value of any reference type is the null reference value.

The default value of any numeric value type - int, decimal, and so on - is the zero of that type. (Types that support multiple representations of zero, like float, choose the positive zero.) The default value of bool is false. The default value of any nullable value type is the null value of that value type.

The default value of any other value type is recursively defined as the value of that type formed by setting all of the fields of the type to their default values.

Is that clear?

What is the default behavior of Equals Method?

The default implementation of Equals
supports reference equality for
reference types
, and bitwise equality
for value types
. Reference equality
means the object references that are
compared refer to the same object.
Bitwise equality means the objects
that are compared have the same binary
representation.

http://msdn.microsoft.com/en-us/library/bsc2ak47.aspx



Related Topics



Leave a reply



Submit