-5

What is the difference between a Field and a Property in C#?

I have read through this topic above but it is full of confusing answers with blah blah.

I want to know, in plain english, is this code below a field or property? . If it's a field, what is a property? If it's a property, what is a field?

class Door
{
     public int width { get; set; }
}

Thank you very much.

Community
  • 1
  • 1

2 Answers2

1

That's a property. It's the shorthand for creating a property with a getter, a setter, and a backing variable.

class Door
{
     public int width { get; set; }
}

The backing variable is anonymous, but basically the code that the compiler generates for that is the same as:

class Door {

  private int _width;

  public int width {
    get {
      return _width;
    }
    set {
      _width = value;
    }
  }

}

A field is just a public variable in a class or a struct, and would look like this:

class Door {

  public int width;

}

In this case the compiler doesn't create any code to handle the field, it's just a plain variable.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • 1
    don't see much c# code with same-line curly brackets. I like it. – Jonesopolis Sep 24 '13 at 17:51
  • practical question: do you use public fields in real-world projects? Now I can see quite religious feelings around their prohibition. – Ilya Ivanov Sep 24 '13 at 17:51
  • @IlyaIvanov my teacher said fields are for kindergarten level. in real life you use only properties. – Ömer Özkan Sep 24 '13 at 17:52
  • @ÖmerÖzkan: Yeah, I remember fields from kindergarten-level C#. Good old days. (Seriously, though — I use fields, and I hope this is real life.) – Ry- Sep 24 '13 at 17:54
  • @minitech If you posted your first comment below my question as an answer, I Would pick you as best answer. Because it has been most helpful to me. Why dont you do it? – Ömer Özkan Sep 24 '13 at 17:55
  • @ÖmerÖzkan: Your question is closed, so I can’t answer it. Besides, Guffa’s says the same thing now, at the bottom. – Ry- Sep 24 '13 at 17:56
  • @minitech when I told you this, the question was open. – Ömer Özkan Sep 24 '13 at 17:56
  • @ÖmerÖzkan: I don’t check for new comments continuously. – Ry- Sep 24 '13 at 17:57
  • @minitech do you have problems regarding public fields with other team members? There is quite a lot of holy wars on the area, like: Properties doesn't "encapsulate" your implementation details, if your use auto-properties of generate properties based of fields. – Ilya Ivanov Sep 24 '13 at 18:00
  • @minitech It's sad because I liked your answer. – Ömer Özkan Sep 24 '13 at 18:00
  • @ÖmerÖzkan: Do you dislike my answer for some reason? I noticed that you rather selected an answer that doesn't actually answer any of the questions... – Guffa Sep 24 '13 at 18:17
  • @Guffa yes, because you rejected my small edit to make it bold on your answer. – Ömer Özkan Sep 25 '13 at 01:52
  • @ÖmerÖzkan: I haven't rejected any edit. So you revenge-accepted the other answer because you thought that I rejected the edit? – Guffa Sep 25 '13 at 09:41
  • @ÖmerÖzkan: So how do you feel now, that you know that I didn't reject any edit? Do you still want to keep the answer that doesn't answer your question, and doesn't say a single word about what you thought was so important? – Guffa Sep 28 '13 at 17:42
  • @Guffa I made the edit again. If you accept it, I will select you as chosen answer :) Thank you. – Ömer Özkan Oct 05 '13 at 14:07
1

a property is just a syntax for defining getters and setters for a field.

class Door
{
     public int width { get; set; }
}

is similar to

class Door
{
    private int width;

    public int getWidth()
    {
        return width;
    }
    public void setWidth(int i)
    {
        width = i;
    }
}