RKh
RKh

Reputation: 14159

What a simple get; set; in a class means?

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?

Upvotes: 2

Views: 2089

Answers (3)

Proclyon
Proclyon

Reputation: 526

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

Upvotes: 1

Merlyn Morgan-Graham
Merlyn Morgan-Graham

Reputation: 59151

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 :)

Upvotes: 3

CD..
CD..

Reputation: 74176

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.

Upvotes: 8

Related Questions