tags:

views:

1900

answers:

7

So here is a bit of syntax that I have never seen before, can someone tell me what this means? Not sure if this is supposed to be some shorthand for an abstract property declaration or something or what.

public Class1 myVar { get; set; }

For what its worth, Class1 is an abstract class.

A: 

Automatic getters and setters.

TheTXI
+6  A: 

It's called an automatic or auto-implemented property, and it's not abstract. You can use that property as-is. The compiler will create a backing store for it behind the scenes.

It's very much like just using a field instead of a property, but you get property semantics and you can use it for things like databinding that require properties.

Joel Coehoorn
+27  A: 

In C# 3.0 and later, auto-implemented properties make property-declaration more concise when no additional logic is required in the property accessors. They also enable client code to create objects When you declare a property as shown in the following example, the compiler creates a private, anonymous backing field that can only be accessed through the property's get and set accessors.

// Auto-Impl Properties for trivial get and set
    public double TotalPurchases { get; set; }
    public string Name { get; set; }
    public int CustomerID { get; set; }
alex
I'd vote you up for having a more thorough explanation but I am all out for the day.
TheTXI
+7  A: 

This is the syntax to let the compiler create a (hidden) field for you.

Also very useful is:

public Class1 myVar{ get; private set; }
chris
+5  A: 

It is shorthand for a property that does nothing more that storing the value in a field. So it would be equivalent to:

private Class1 _myVar;

public Class1 myVar
{
  get{ return _myVar; }
  set{ _myVar = value;}
}

Its just a simple property that yoy could replace with a more complex evantuation at a later time without changing the interface.

DefLog
+2  A: 

These are Auto-Implemented properties. They allow you to make you code more concise. You can read more about them here:

Auto-Implement

You can also make the accessors have different access levels:

public int MyVar { get; private set; }

This allows you to expose the property external to the class and still set it internally.

Daniel
+7  A: 

It may look like an abstract property or a property from an interface but it is far from it. In order to encourage developers to use properties (as they are a best-practice for many reasons) Microsoft decided to include this feature in C# 3 to allow you to declare properties with greater ease.

Here is the standard way of creating a property:

String foo;

public String Foo
{
    get { return this.foo }
    set { this.foo = value; }
}

Now this requires quite a bit of typing and as developers are lazy to the core sometimes we are tempted to create public fields just to save some time.

Now with the C# 3 compiler we can do this:

public String Foo { get; set; }

While this looks a bit strange, consider the work that the compiler is doing on your behalf. The previous code gets compiled to this:

[CompilerGenerated]    
private string <Foo>k__BackingField;

public string Foo
{
    [CompilerGenerated]
    get
    {
        return this.<Foo>k__BackingField;
    }
    [CompilerGenerated]
    set
    {
        this.<Foo>k__BackingField = value;
    }
}

So even though the syntax looks a bit strange you are still creating a property exactly the way you are used to.

Andrew Hare