Rami Alshareef
Rami Alshareef

Reputation: 7140

What's the difference between encapsulating a private member as a property and defining a property without a private member?

What's the difference (Performance, memory...etc) between encapsulating a private member like this

private int age;
public int Age
{
  get { return age; }
  set { age = value; }
}

and define a property like this

public int Age
{
  get ;
  set ;
}

Upvotes: 13

Views: 2143

Answers (5)

Ani
Ani

Reputation: 113402

The code that the C# compiler generates for auto-implemented properties is almost identical to your first example (it uses a private, backing field), so I wouldn't worry about it too much.

The only real difference is that it decorates the property getter and setter with the [CompilerGenerated] attribute. This shouldn't have any impact on the performance of getting and setting the property. (As a minor nitpick, that should increase the size of the assembly's binary ever so slightly).

What I like about auto-implemented properties, other than brevity of course, is that it prevents even the declaring type from accessing the backing-field instead of the property (the backing-field is anonymous). This brings clarity to the code, and generally makes refactoring / changing the property implementation easier too.

Upvotes: 6

TalentTuner
TalentTuner

Reputation: 17556

no difference as compared to performance in the second case is synthetic sugar for writing properties called Automatic Properties.

if you want to put some logic in the set or get part , you won't be able to do it automatic properties.

Upvotes: 1

Pieter van Ginkel
Pieter van Ginkel

Reputation: 29632

The difference is that you have control over the getters and setters.

With the automatic implementation, you cannot do something like:

private int age;

public int Age
{
    get { return age; }
    set
    {
        if (age != value)
        {
            age = value;
            OnAgeChanged(EventArgs.Empty);
        }
    }
}

public event EventHandler AgeChanged;

protected virtual void OnAgeChanged(EventArgs e)
{
    var handler = AgeChanged;

    if (handler != null)
        handler(this, e);
}

If you don't need this, the automatic implementation should be enough.

The main advantage over using an automatic property implementation in comparison to a field is that when you use an automatic property implementation and later on you want to change the implementation into e.g. the above, the interface of your class does not change.

Upvotes: 1

Nobody
Nobody

Reputation: 4841

I asked this question a while ago:

see Correct use of C# properties

Quoting the answer:

They are equivalent in the internal compiled form, except that you cannot access the compiler generated private variable in the second form.

From an code efficiency point of view, they are equivalent as well, the just in time compiler normally directly accesses the private variable without the overhead of calling an access function (after the runtime environment has checked accessibility etc.).

From a coding perspective, I prefer the second version which is more compact (less to write, less to read).

The second syntax was introduced in C# 3.0. So the first variant would be more compatible to old compilers.

Upvotes: 1

cdhowie
cdhowie

Reputation: 169018

In the second case, the C# compiler will generate a field for you and generate a getter and setter to access it. In other words, there is no functional difference between the two code samples you posted. The only difference will be the name of the private field, which will be compiler-generated.

Upvotes: 10

Related Questions