What Is the Use of 'Default' Keyword in C#

What is the use of `default` keyword in C#?

The default keyword is contextual since it has multiple usages. I am guessing that you are referring to its newer C# 2 meaning in which it returns a type's default value. For reference types this is null and for value types this a new instance all zero'd out.

Here are some examples to demonstrate what I mean:

using System;

class Example
{
static void Main()
{
Console.WriteLine(default(Int32)); // Prints "0"
Console.WriteLine(default(Boolean)); // Prints "False"
Console.WriteLine(default(String)); // Prints nothing (because it is null)
}
}

Does default keyword create new instance?

As stated here, it creates a new default value. So its same memory allocation as it happens for value type and reference type. First one on stack second on heap

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/default#default-literal

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

Please tell me what is use of default keyword in c#.net

I believe the default is 0, so you will have an invalid HardwareInterfaceType generated. I personally don't like this sort of coding for enums. IMO it's more clear to define an Enum value of "Undefined" or "None" and then initialize the variable to that instead.

One of the "Gotchas" with enums is that they can be initialized to a value that does not exist in the enum. The original programmer might have thought that the enum would be initialized to some valid value in the Enum definition if he used the "default" keyword. Not true, and IMO this is not a good use of the "default" keyword at all.

var foobar = default(HardwareInterfaceType);
Console.WriteLine(Enum.IsDefined(typeof(HardwareInterfaceType), foobar)); //returns false
Console.WriteLine(foobar); //returns 0

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

C#'s default keyword equivalent in C++?

Assuming the type is default-constructible, you can use value initialization. For example,

template <typename T>
T get()
{
return T(); // returns a value-initialized object of type T
}

If the type is not default-constructible, typically you need to provide a default to use. For example,

template <typename T>
T get(const T& default_value = T())
{
return default_value;
}

This function can be called with no argument if the type is default-constructible. For other types, you can provide a value to be returned.

Different between keyword new and default in create instance of class

In essence default(T) only reserves the minimum memory required to hold a reference to, or instance of the type, it does not call any constructors.

  • For primitive types like int and bool the value returned would be 0 or false respectively.
  • For reference types (classes) the only reservation made is for the reference, which will be null
  • For structs, the memory for the struct will be reserved according to the rules above, so reference fields will be initialized to null, even if they cannot normally be null because of a constructor! Value fields will have their default set (0, false, etc).
  • For enum types, values will be initialized to their first (0 value) field unless you have manually assigned values to the enum labels, in which case the value will still be 0 and might be invalid.

what's the difference between the default and default constructor

This creates a new instance of the type A by calling the default, parameterless constructor:

A a = new A();

This assigns the default value for type A to the variable a and does not call any constructor at all:

A a = default(A);

The main difference is that the default value of a type is null for reference types and a zero-bit value for all value types (so default(int) would be 0, default(bool) would be false, etc.).



Related Topics



Leave a reply



Submit