I was trying to find meaning of this terms but especially due to language barrier I was not able to understand what they are used for. I assume that "field" is variable (object too?) in the class while "property" is just an object that returns specific value and cannot contain methods etc. By "member" I understand any object that is declared on the class level. But these are just my assumptions based on commented code samples where some careful programmers used "property region" etc. I would really appreciate if someone could explain it to me.
Fields are ordinary member variables or member instances of a class. Properties are an abstraction to get and set their values. Properties are also called accessors because they offer a way to change and retrieve a field if you expose a field in the class as private.
Both properties and methods are members of an object. A property describes some aspect of the object while a method accesses or uses the owning object.
Properties are the special type of class members that provides a flexible mechanism to read, write, or compute the value of a private field. Properties can be used as if they are public data members, but they are actually special methods called accessors.
In object-oriented programming, a member variable (sometimes called a member field) is a variable that is associated with a specific object, and accessible for all its methods (member functions).
In C# :
fields : These are variables declared at the class level.
public class SomeClass { private int someInteger; // This is a field public double someDouble; // This is another field protected StringBuidler stringBuidler; // Still another field }
properties : Often used as accessors to a private field of a class, they can provide get and set methods that wrap some logic around the field manipulation.
public class SomeClass { private StringBuilder stringBuilder; // Property declaration public StringBuilder StringBuilder { get { if(this.stringBuilder == null) this.stringBuilder = new StringBuidler(); return this.stringBuilder; } set { if(this.stringBuilder == null) this.stringbuilder = value; } } }
members : Includes fields, properties, methods, events of a class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With