6

What is the difference, if any, between

public int x;

and

public int x { get; set; }

?

valentin
  • 2,596
  • 6
  • 28
  • 48
  • 1
    the first one is a public variable while the second is a public property. – John Woo Nov 09 '13 at 20:20
  • 3
    if there was just a site where you could search such things like i dont know, Google? https://www.google.co.il/search?q=difference+between+properties+and+variables+c%23&oq=difference+between+properties+and+variabl&aqs=chrome.2.69i57j0l5.11837j0j8&sourceid=chrome&espv=210&es_sm=122&ie=UTF-8#es_sm=122&espv=210&q=properties+vs+variables+c%23 – Tomer W Nov 09 '13 at 20:22
  • 4
    In this case he didn't have anything to google. He didn't know it was a "property". Therefore what exactly could he have put in a google search? I think it's a fair question... – Trevor Elliott Nov 09 '13 at 20:25
  • 1
    possible duplicate of [What is the difference between a field and a property in C#?](http://stackoverflow.com/questions/295104/what-is-the-difference-between-a-field-and-a-property-in-c) – Joel Rondeau Nov 09 '13 at 20:26
  • @TomerW: yep didn't find this because I didn't have the correct key terms :/ But thank you :) – valentin Nov 09 '13 at 20:27
  • possible duplicate of [Auto-implemented getters and setters vs. public fields](http://stackoverflow.com/questions/111461/auto-implemented-getters-and-setters-vs-public-fields) – nawfal Jul 17 '14 at 20:51

3 Answers3

5

The first one is called a field. The second one is a property, in this case an auto-implemented property.

Properties act like fields but use a getter and a setter function to retrive and set the value. Another way of writing the above property is as follows:

private int _x;
public int X
{
    get
    {
        return _x;
    }
    set
    {
        _x = value;
    }
}

The variable _x in this case is called a backing field. With an auto-implemented property you can't access the backing field or customize code in the getter/setter, but if you don't need to than it's shorter and more succinct.

As a rule in C# most of the time any public member should be exposed as a property instead of a field.

Trevor Elliott
  • 11,292
  • 11
  • 63
  • 102
0

The first one is public variable which can be accessed from anywhere.

The second one is public property

Check Properties tutorial for details.

Properties have many uses: they can validate data before allowing a change; they can transparently expose data on a class where that data is actually retrieved from some other source, such as a database; they can take an action when data is changed, such as raising an event, or changing the value of other fields.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0

The difference between thise two is that a property can do something more than just get / set a variable.
take this example:

private int _x;
public int x 
{ 
    get 
    {
      //do something
      return _x;
    } 
    set
    {
      if(_x != value)
      PropertyChanged("x");
      _X = value;
    }
}

when we set the property - we notify something ( PropertyChanged()) that the value has changed. It would be very hard to do with just the field

Jens Kloster
  • 11,099
  • 5
  • 40
  • 54
  • 1
    Yes, but given the exact code, without any added functionality like events or value checks, is there any practical difference at all? In other words, why type { get set } after a public property if all you ever do is set and get the value of X directly? – Kokodoko Apr 07 '15 at 12:56
  • I never type 'get; set;' :-) i type 'prop' and press Tab. And them i am ready if, later, i need to add functionallity. – Jens Kloster Apr 07 '15 at 17:22