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.
None.
– Jéf Bueno
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.
– Jéf Bueno
Although there are no differences in practice, I left an interesting answer for those who are curious.
– Jéf Bueno