3 ms·
Yes. However, C# uses the convention that capitalized "fields" are actually properties, and lowercase ones are true fields. For example, Foo.X might be an x po
by russellsprouts 13y ago
Yes. However, C# uses the convention that capitalized "fields" are actually properties, and lowercase ones are true fields.
For example,
Foo.X might be an x position, returned from a getter method, and set with a setter method. Internally, the class make use the Foo.x field to store it, in which case the getter and setter are trivial. In fact, C# will write them for you
The property could look like:
int X{
get;
set;
}
(Though my syntax might not be completely correct, I haven't used C# in a while). C# will automatically create a private field to store the value for x, and the getter and setter will work as expected.
Classes should, as a general rule, only expose properties and methods to the outside world. All fields should be private. Yes, accessing a property calls a method, but so does a Java getX().
Java uses the same rule, except getters and setters are implemented using setX(int) and getX(). C# properties are the equivalent. The client of a class should not care how the class stores values, but that the getters and setters work.
- Someone 13y ago"However, C# uses the convention that capitalized "fields" are actually properties, and lowercase ones are true fields." If only. In http://msdn.microsoft.com/en-us/library/vstudio/ms229043%28v=vs.110%29.aspx http://msdn.microsoft.com/en-us/library/vstudio/ms229043%28v..., Microsoft advocates: "The PascalCasing convention, used for all identifiers except parameter names, capitalizes the first character of each word"
- cmircea 13y agoThe UI in MonoDevelop / Visual Studio will show the differences during autocomplete. Also, fields should NEVER be public in a class.