What is the default (Object) for in C#?

Asked

Viewed 139 times

8

Sometimes I come across the keyword default in the C#:

object obj = default(object);
string str = default(string);
int number = default(int);
  • What good is?

  • In which scenario can I apply this?

2 answers

8


In this example it doesn’t seem necessary. That’s the same as:

object obj = null;

The default generates the default value of a type, in the case of reference types the default value is null.

The same goes for string which is a type by reference.

But for the int the default value is 0. You have to refer to the types by value which is the default value adopted, it is almost always 0, but there are some cases where 0 does not make sense, so it may be another value. Therefore to int we know it’s the same as:

int number = 0;

It’s weird enough to create an object like object, This is rarely necessary and almost always because of some legacy. This type has no content, so it is a worthless object, something that concretely doesn’t make much sense. It could be a placeholder for another real type, but if this is necessary should use a generic type and not object.

It gets even weirder when you know the type that is trying to create the default value. The most common is to use this with generic types, where you do not know what the default value should be. Even so in some cases it is not even necessary. That would make some sense:

var obj = default(T);

whereas T is a placeholder for the real type. This is a case that makes a lot of sense, especially if you can have types by value.

In some cases where there is application of Pattern matching may also be useful.

Without a generic scenario or Pattern matching it is difficult to justify the use. It can be said that it is to make it clearer that it is the default value, but then it starts to taste, it does not help much in fact. If you know what the type is, you know its default value and it cannot change under normal conditions, then you have no reason to use in this scenario, only when the type will be set later.

In thesis on a type by value created by someone the default value could change over the lifetime of the solution and this is a case that it would be useful even knowing what the specific type is, but there’s something very wrong with a type that starts with a default value and then changes, will probably break some codes or because they didn’t use default, or because they used but expected a stable value. Don’t do this.

If the type has been specified it does not need to be specified again, something like this is normal:

T obj = default;
int number = default;

If the mechanism of annulable references is connected and I think today should always work like this the use of default It gets really weird 'cause he’d be the same as:

string str = null;

Turns out that way str does not accept null, does not make sense. This would make sense:

string? str = default;

because thus the null is an acceptable value, but in string not, so it already initializes wrong. It’s just another reason not to use when you know the type you’re using. The most common would be to use something like:

string str = "";

5

In this particular case it is the operator pattern or literal which produces a standard value of a certain type, such as the standard value of a int is the number 0, the default value the literal of a DateTime is 1/1/0001 12:00:00 AM and so on, each type of data in the C# language has its initial or default value.

The value of a complex data, class and String (which is also a class) is null.

There are two ways to declare the default value that is with type argument in specific, example:

int i = default(int);

and the other way since version 7.1 is literal that it does not need to pass any argument, leaving the responsibility with the compiler can infer the type of the expression, example:

int i = default;

It can be used when it is necessary to have a default value in some variable, it depends on its code, context that this is useful, generally used for arguments. As stated by the documentation itself:


ref. https://docs.microsoft.com/pt-br/dotnet/csharp/language-reference/operators/default#default-literal

Reference

Browser other questions tagged

You are not signed in. Login or sign up in order to post.