What is the difference between these two types of standard values?

Asked

Viewed 58 times

0

In C#, by creating a classe new with some properties, I should set some default value for the properties created, 0 in the case of a property of Saldoatual.

What would be the difference between the 2 following ways that represent the above situation? Would you have any future problem using the first or the second? Which would be the best?

First way:

using System;

public class Correntista
{
    public double SaldoAtual { get; set; } = 0;
    public string Nome { get; set; }
}

Second way:

using System;

public class Correntista
{
    public Correntista()
    {
        SaldoAtual = 0;
    }

    public double SaldoAtual { get; set; }
    public string Nome { get; set; }
}
  • 1

    None.

  • One important detail: this is a feature of the language, has no relation to any web framework. Therefore the tags ASP.NET-MVC (framework to work with MVC in ASP.NET), ASP.NET (framework to develop . NET for the web) and ASP (another technoalloy, completely different) are unnecessary to this question.

  • Although there are no differences in practice, I left an interesting answer for those who are curious.

1 answer

3


In practice there is no difference. The default value for the property will be entered in the constructor call.

However, analyzing the two generated IL codes, one can notice a brief difference between the two. See below the code referring to the constructor method generated for both cases.

Case 1 - Value entered in the manufacturer:

Correntista..ctor:
IL_0000:  ldarg.0     
IL_0001:  call        System.Object..ctor
IL_0006:  nop         
IL_0007:  nop         
IL_0008:  ldarg.0     
IL_0009:  ldc.i4.0    
IL_000A:  call        UserQuery+Correntista.set_SaldoAtual
IL_000F:  nop         
IL_0010:  ret   

Case 2 - Default value for property:

Correntista..ctor:
IL_0000:  ldarg.0     
IL_0001:  ldc.i4.0    
IL_0002:  stfld       UserQuery+Correntista.<SaldoAtual>k__BackingField
IL_0007:  ldarg.0     
IL_0008:  call        System.Object..ctor
IL_000D:  nop         
IL_000E:  ret     

You realize there are two small differences between them?

In the first case, the base class call is first (Object) and then the constructor uses the method set_SaldoAtual to change the field value.

In the second case, the value of the field is initially changed, but unused of the method set_SaldoAtual, the value is inserted directly into the "support field" (backing field).

This really shouldn’t change anything in practice because only auto-properties can have a default value. Thus, the method implementation set_SaldoAtual it will always just be to make the backing field receive the value passed to the method.

  • How do I verify the generated IL codes?

  • 1

    @Victorlaio You have to use some tool to see. I use the Linqpad, but has the Ildasm and several others.

  • Obgd for the tip!

Browser other questions tagged

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