What happens when we assign the default value to objects?

Asked

Viewed 116 times

7

Example:

public Carro Metodo(string marca) {

 Carro carro = default;

 if(!string.IsNullOrEmpty(marca))
 {
   carro.Marca = marca
 }

 return carro;
}

Studying on the Internet, I realized that some objects were initialized with default.

What happens when we do this?

2 answers

8


As the name itself says, a default value of that type is assigned to the variable. This may vary but is usually a value equivalent to a zero, a simple initialization.

In types by value would be the minimum value, which is usually 0, but not always, a DateTime for example it is not quite a 0 because this number does not even exist in this type (it starts on the 1st of month 1 and year 1).

Types by reference used to be their standard as a null because this is the 0 address and indicates that there is no object there. This is a problem in C# 8 because it does not recognize as nullable the types by reference as it was before (it is still possible to configure this by project, file or code snippet), so use default is not suitable because it will generate a null that cannot be used for this type. It will not give error, but will produce a Warning, then what will occur there depends on the C# version and how the chunk is configured, or if the type is explicitly declared as voidable (Carro? for example).

Every first-class programmer who Warning is an error in any code other than a prototype.

So the default should not be abused, usually the default value is obvious and it is rare for you to explicitly want the default value, at least when you know the type. In reference types you usually do not want the default value that is null, this induces error and it is better to initialize the object with its constructor.

If a guy doesn’t have one builder probably he was conceptualized in a wrong way. It seems to be the case of this type Carro, assuming that Carro is a type by reference. Ideally you have a constructor that produces a valid object and does not use the default where you don’t need.

In fact the default exists mainly for cases where you do not know the type of beforehand and so do not know what is the default value of it. This occurs in generic programming (eventually with reflection as well). Its abuse leads to unintended or wrong consequences, as seems to be the case with this code, although the biggest problem is the lack of constructor and/or initialization of its members.

Check this code (which only exemplifies the use, it is not good to do any of this):

using static System.Console;
#nullable enable //o .NET Fiddle não liga o anulável por padrão

public class Program {
    public static void Main() {
        Carro carro = default;
        WriteLine(carro.Marca);
        Carro? carro2 = default;
        WriteLine(carro2.Marca);
        Carro carro3 = new Carro();
        WriteLine(carro3.Marca);
    }
}

public class Carro {
    public string Marca;
    public string Modelo = "";
    public string? Tipo;
}

Behold working in the .NET Fiddle. Also put on the Github for future reference. Still other locations do not have C# 8.

Note the warnings, they should be solved, this is modern programming, and it will benefit you

I understand why they did it that way, but it was a mistake to choose this semantics for types by reference. Ideally it should be called the default constructor when using the default, except for cancellable types.

7

Your variable points to the default value of object type variables (null).

Default exists for 2 reasons:

  • To make your intention easier to understand for those reading your code.
  • So you don’t have to search in the documentation, what the default value of each variable type is, when you want to 'reset' it to the initial value (null for objects, 0 for ints, etc...).

Browser other questions tagged

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