tags:

views:

87

answers:

3

Take for example following code from a class:

public class Employee : IEntity
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int EmployeeID { get; set; }
}

public class Company : IEntity
{
    public string Name { get; set; }
    public string TaxID { get; set }
}

I always used get; and set; with something in braces. I never left them like this.

Writing just:

get; set;

What it means?

+7  A: 

Auto-Implemented Properties

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.

CD
+3  A: 

These are called Auto-Implemented Properties:

http://msdn.microsoft.com/en-us/library/bb384054.aspx

The compiler will generate a backing field, similar to this code:

public class Company : IEntity
{
    public string Name
    {
      get { return _Name; }
      set { _Name = value; }
    }
    private string _Name;
}

It was decided that this syntax could be made much shorter, but still keep all the same utility, hence Auto-Implemented Properties were born :)

Merlyn Morgan-Graham
@Merlyn: I think this is the first answer that really cleared my doubt. Glad to know that it was decided later to shorten the code, otherwise I was messed up with the two variants of code.
RPK
+1  A: 

Just look at it as an quick and easy C# way of giving you a read write permission over a variable.

One of the good things of C# if you ask me.

The other answers pretty much tell you everything else there is to know about auto get set. Even though these two quotes seem somewhat conflicting:

CD said:

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.

While Merlyn Morgan-Graham said:

These are called Auto-Implemented Properties:

http://msdn.microsoft.com/en-us/library/bb384054.aspx

The compiler will generate a backing field, similar to this code:

public class Company : IEntity {
    public string Name
    {
      get { return _Name; }
      set { _Name = value; }
    }
    private string _Name; }

It was decided that this syntax could be made much shorter, but still keep all the same utility, hence Auto-Implemented Properties were born :)

To me that seems like CD said it does create a condition whilst Merlyn Morgan-Graham said there are none.

I think CD is correct when stating you can longer use , for example, the setters write permission as a response to also change whatever it's writing.

private int x = 3;

public int _x { get; set /*Change x*/; }

You would have to use the normal get set construction for that

Proclyon