2

I know this may be a silly question. I referred some articles. But, I am very interested to know the main difference for the following code,

using System;

namespace Business
{
    public class User
    {
        private int _id;

        public int ID
        {
            get { return _id; }
            set { _id = value; }
        }
    }
}

And

using System;

namespace Business
{
    public class User
    {
        public int ID { get; set; }
    }
}

I want to know some brief details (any reference)...

Thanks

Jesuraja
  • 3,774
  • 4
  • 24
  • 48

5 Answers5

2

In your example there is no difference.

However, encapsulation is an important aspect of object oriented programming. You typically do not want to have a wide open setter.

namespace Business
{
    public class User
    {
        private int _id;

        public int ID
        {
            get { return _id; }
            set { 
              if (value <= 0) 
                   throw new ArgumentOutOfRangeException(value, "value", "Impossible value");

              _id = value; }
        }
    }
}

By using a backing field you can also make sure that a value is specified in the constructor:

namespace Business
{
    public class User
    {
        private AccountState _state;

        public User()
        {
            _state = AccountState.Active;
        }

        public AccountState State
        {
            get { return _state; }
            set { 
                 if (value == AccountState.Active && PasswordExpired)
                    throw new ArgumentException("Can not set active state when the password have expired");
                 _state = value; 
             }
        }
    }
}
jgauffin
  • 99,844
  • 45
  • 235
  • 372
  • But in the constructor you can also set a auto-property, so whats the benfit there? The benefit is, that you can set the backing-field without a constructor IMO... – Christoph Fink May 15 '14 at 07:13
1

There is no any difference for compiler, but there is differences in code.

You don't have a way to intercept set and get on auto-property, which you can do in normal case. If you ever worked with WPF, you will find there massive use of that "normal" properties.

public int Data  {
    get {}  
    set {
       //Do something ehere
    }
}

That means you can not debug them (auto-properties) or put break-point inside.

Tigran
  • 61,654
  • 8
  • 86
  • 123
  • Actually you can put breakpoints into auto-implemented properties, but it's a PITA: `Debug` -> `New Breakpoint`-> `Break at function` and enter `YourClassName.get_YourPropertyName` into the `Function` textbox. – sloth May 15 '14 at 07:19
0

After the compiler they are the same.

public int ID { get; set; }

is just "syntax sugar".

This is called "auto properties" and is very usefull to reduce boiler plate code if you have "empty getter/setter" like in your example.

With auto-property you can no longer access the backing-field directly as it only exists after compilation, which can cause a very minor performance drawback IF the getter/setter is not inlined by the JIT.

Also at the moment there is no way to directly initialize an auto-property, but with the upcoming C# 6 you can do the following:

public int ID { get; set; } = 0;
Christoph Fink
  • 22,727
  • 9
  • 68
  • 113
0

The Auto-Implemented Properties (http://msdn.microsoft.com/en-us/library/bb384054.aspx) were introduced in C# 3.0, prior to that you had to use your first version.

Samuel
  • 6,126
  • 35
  • 70
0

Auto-implemented properties were introduced to facilitate and simplify this:

private int _id;

public int ID
{
    get { return _id; }
    set { _id = value; }
}

into this:

public int ID { get; set; }

These two pieces of code are identical from the compiler point of view.

However, if you want to introduce any logic into your properties, then auto-implemented properties are NOT the way to go.

For example, if you want to verify that provided value of id must always be non-negative, you can implement this logic inside the setter like this:

private int _id;

public int ID
{
    get
    {
        return _id;
    }
    set
    {
        if (value < 0)
        {
            throw new BusinessException("You must provide a non-negative number for ID.");
        }

        _id = value;
    }
}

You cannot achieve this kind of flexibility with auto-implemented properties.

anar khalilov
  • 16,993
  • 9
  • 47
  • 62