-2

Why would I ever want to do this:

public T X { get; set; }

Instead of this:

public T X;

People (including some answers on SO) have told me that "What if you wanna change the behavior later on?" is an answer to this, but that's not true, since I can just convert my public T X; into

public T X {
    get { blabla; }
    set { blybly; }
}

Edit: Before posting anymore links about "Read this" and "Read that", you can check the links. 99% of them contain mostly other people referring to other links, and nobody actually gives a proper reason to use properties over fields.

Here's one example of a popular answer: "You can make the setter private

public T X { get; private set; }

Well obviously if I want a private setter, I will create a property. But if I don't, why not just use a field? And if I later decide I want a private setter, why couldn't I change my field from public T X; to public T X {get; private set; }?

Skamah One
  • 2,456
  • 6
  • 21
  • 31
  • 2
    See http://csharpindepth.com/Articles/Chapter8/PropertiesMatter.aspx – Jon Skeet Apr 30 '15 at 16:11
  • 4
    "but that's not true" - it depends on whether you care about binary compatibility, source compatibility, reflection compatibility... – Jon Skeet Apr 30 '15 at 16:11
  • 2
    Just one difference from the top of my head: reflection. You'd break code that checks for a field when you suddenly change it to a property. – Jeroen Vannevel Apr 30 '15 at 16:11
  • 1
    Don't forget any code that uses `ref` will break. – ChaosPandion Apr 30 '15 at 16:12
  • 2
    @CharlesMager There's no proper answer to this on the link you posted. – Skamah One Apr 30 '15 at 16:12
  • 2
    @Skamah One There are tons of answers in that link, I just read them in they seem like excellent reasons why you would use a property over a field – Scriven Apr 30 '15 at 16:18
  • @Scriven Only reason I've found so far is not to have to recompile the program. The rest of the answers are something along "You can make the setter private" which, if I decide I later want to do, I can change my code to match to. – Skamah One Apr 30 '15 at 16:22
  • 1
    If there is no proper answer then you should put a bounty on it to attract attention, not create a new question with the same premise. – Jeroen Vannevel Apr 30 '15 at 16:24

1 Answers1

0

One thing I can think of off the top of my head is this.

  1. Create an assembly with your class in containing a field named X.
  2. Create an application that uses that class and sets the value of X.
  3. Deploy your app.
  4. Update the class to be a property because you need logic in the getter/setter.
  5. Build the DLL and deploy it as an update.

When you run it you will get System.MissingFieldException - because the consumer app was originally written to look for a field rather than a get_X method.

Whether or not you should deploy your applications in this way or not is a different matter, but if you are a 3rd party code provider and a client is using your library in this way then it will crash their deployed apps.

Peter Morris
  • 20,174
  • 9
  • 81
  • 146