New C# 6 functionality "Auto-Property initializers" is just a facilitator?

Asked

Viewed 289 times

14

One of the new C#6 features is the possibility to initialize properties at the time of declaration.

public class Customer
{
    public string First { get; set; } = "Jane";
    public string Last { get; set; } = "Doe";
}

The aim is only to facilitate the writing of the class, such as no longer needing to declare a constructor to perform the initializations, or behind something else?

  • ramaral, should we exchange this tag for the tag c#? http://meta.pt.stackoverflow.com/q/4421/18246

  • @jbueno In fact I hesitated to put it when it did not appear in the suggestions. The reading of the post you indicated did not help me to decide, especially on a matter as specific as this.

2 answers

9

The aim is only to facilitate the writing of the class, such as no longer needing to declare a constructor to perform the initializations, or behind something else?

Actually no longer need to declare constructores. Variable initialization was already done in a static context. Now the language opened the possibility to initialize properties as well.

8


Both the automatic properties available since C# 2 and their initialization available since version 6 are only syntactic sugars. It can even create the property more "manual" and initialize it in a constructor.

Of course, if you go deeper the property itself is just a couple of methods with specific characteristics, but let’s stick to this first layer of abstraction.

When you write

class Exemplo {
    public int valor { get; set; } = 1;
}

At heart it’s the same as

internal class Exemplo {
    private int valor;
    public int Valor {
        get { return valor; }
        set { valor = value; }
    }
    public Exemplo() {
        valor = 1;
    }
}

In fact the compiler generates code next to this:

.class private auto ansi beforefieldinit AutoPropertyInitializer.Exemplo
       extends [mscorlib]System.Object {
  .field private int32 '<Property>k__Valor'
  .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 ) 
  .method public hidebysig specialname instance int32 
          get_Valor() cil managed {
              //corpo aqui
          }

  .method public hidebysig specialname instance void
          set_Valor(int32 'value') cil managed {
              //corpo aqui
          }

  .method public hidebysig specialname rtspecialname 
          instance void  .ctor() cil managed {
              // Code size       18 (0x12)
              .maxstack  8
              IL_0000:  ldarg.0
              IL_0001:  ldc.i4     0x1
              IL_0006:  stfld      int32 AutoPropertyInitializer.Exemplo::'<Property>k__Valor'
              IL_000b:  ldarg.0
              IL_000c:  call       instance void [mscorlib]System.Object::.ctor()
              IL_0011:  ret
          }

    .property instance int32 Valor() {
        .get instance int32 AutoPropertyInitializer.Exemplo::get_Valor()
        .set instance void AutoPropertyInitializer.Exemplo::set_Valor(int32)
    }
}

I put in the Github for future reference.

  • 1

    This new feature makes sense because so all "variables" can be initialized in the declaration.

Browser other questions tagged

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