-1

Is there any difference between:

public class user
{       
    public string Name { get; set; }  
}

And:

public class user
{       
    public string Name;  
}

??? Thank you

dvIta
  • 137
  • 2
  • 8
  • The difference is get-set. With them, they are properties, without them, fields. In Your example get-set don't do much, but you can use them. – Arghya C Oct 02 '15 at 09:29
  • 1
    in this case there is no difference between field and property really. but properties gives you ability to modify almost everything. – M.kazem Akhgary Oct 02 '15 at 09:33
  • 1
    Who upvoted this one? Seriously? – wingerse Oct 02 '15 at 09:33
  • 1
    There are subtle differences, but those differences are completely swamped by the "elephant in the room" problem with both. Both are bad practice as they offer no encapsulation of data and expose `Name` as publicly mutable. Please read http://blog.ploeh.dk/2011/05/26/CodeSmellAutomaticProperty/ to understand why you should avoid both. – David Arno Oct 02 '15 at 10:10

1 Answers1

1

First one is a property, second is a field.

https://msdn.microsoft.com/en-gb/library/x9fsa0sw.aspx https://msdn.microsoft.com/en-us/library/ms173118.aspx

Ian Newson
  • 7,679
  • 2
  • 47
  • 80