1

Is this.

    private ObservableCollection<String> _Col;
    public ObservableCollection<String> Col
    {
        get
        {
            return _Col;
        }
        set
        {
            _Col = value;
        }
    }

the same as this

public ObservableCollection<String> Col { get; set }

using col.add("stringvaribleorsomething") they would both result in col being a collection of items?

dee-see
  • 23,668
  • 5
  • 58
  • 91
New Bee
  • 991
  • 1
  • 13
  • 24
  • I suppose this was a typo, but in your setter it should be `_Col = value;` – dee-see Jan 24 '14 at 03:40
  • 1
    You seem to be confusing two different issues. The use of a backing variable or auto property doesn't change the semantics of the `.Add` method. – Enigmativity Jan 24 '14 at 03:46

2 Answers2

1

Shortly - yes. But look at this article about auto-implemented properties in C#

Backs
  • 24,430
  • 5
  • 58
  • 85
  • Thanks for sharing this article, im not sure i understand. what i think its trying to tell me is that if at any stage i have to modify the values it would be difficult with the second example so it is best to use the first example. – New Bee Jan 24 '14 at 03:47
  • @New Bee you can also take a look at the following. It would give some sharp explanation. http://stackoverflow.com/questions/1523548/why-we-need-properties-in-c-sharp – Anees Deen Jan 24 '14 at 06:16
0

If you want to both getter and setter methods like that, yes it is the same.But if you want to make your property read only,or set your property value depends on some condition you should use the first one.

Also the second one is called auto-implemented property.You can see the documentation

Selman Genç
  • 100,147
  • 13
  • 119
  • 184