The term "member" is a collective generic catch all name for anything that is part of a class or interface. For instance: Method, Field, Property, Constructor, Desctructor. The literal definition (according to csharp-online.net) is:
"Class element that defines a behavior or property—constructors, events, member variables, methods, properties, etc. Also known as type members."
A method is a function definition - in semantic terms a process that can be executed. i.e. DoSomething(); In loose terms, a constructor and destructor would technically fall under this definition even though semantically they're slightly different in that they're automatically initiated at class creation and class destruction time and cannot be called directly.
A property is kind of like a field, but it is in two parts, a setter and a getter. A property's setter and getter can function in different manners, but semantically they have the purpose of setting a value and retrieving the value [from a backing store which is usually a private field]. They may get more complex in what happens prior to the value being set or returned, but essentially what it boils down to is that the process of setting the data and retrieving the data is different and may have different levels of access. For instance, a property's getter may be public [can be accessed from outside the class], protected [can only be accessed by derivative classes or from within the containing class], could be private [and thus can only be accessed from within the class] without affecting how the setter is accessed. You could for instance have a public getter, but only a private setter.
A field has a single accessor, it doesn't have a setter or a getter (well, not that you have access to, underneath the hood of the programming language, it may have, it may differ from language to language). This means that whatever accessor is specified means that it can be get or set by the same objects [If it's public, it can be set or get from outside the class; protected, only by deriving classes or inside the class; private only inside the class].
A variable (I *think*, look this up to make sure I'm right, I usually use the terms variable and field interchangeably in my own life, but I think the technical definition separates them) is the same as a field, however, it's defined within a class member, i.e. inside the boundary of a property or a method declaration. A short code example to demonstrate each:
- public class MyClass
-
{
-
public int MyField; //A field - setter and getter are both public
-
//it can be referenced by any code inside or
-
//outside this class instance.
-
-
private int _backingStoreForMyProperty; //A field - setter and getter are
-
//both private. It can only be
-
//referenced by code inside this
-
//class instance.
-
-
public int MyProperty //A property with a separate getter/setter
-
{
-
get //because no accessor is specified on the get, it takes the accessor
-
//of the property definition - in this case, public. It is
-
//referenced by calling int x = MyProperty; which will get the
-
//value from the backing store and apply it to x.
-
{
-
return _backingStoreForMyProperty;
-
}
-
private set //can only be accessed from inside this class instance.
-
//by specifying MyProperty = 2; for instance.
-
{
-
if (value <= 0) throw new exception("Must be > 0");
-
_backingStoreForMyProperty = value;
-
}
-
}
-
public MyClass()
-
{
-
//This is a constructor, which could be called a method too...
-
//technically, but it's automatically called when you "new up"
-
//the class, and can't be called the same way other methods can
-
//be. You can tell it's a constructor, because it has no return
-
//type and it takes the same name as the class. All other methods
-
//excluding the destructor have a return type - even if there is no
-
//return type, in which case void is returned.
-
}
-
~MyClass()
-
{
-
//The desctructor, this is called when garbage collection destroys
-
//your object. It could be called a method, but you can't call it
-
//by conventional means.
-
}
-
public void DoSomething()
-
{
-
//A method that can be executed. Notice how the return type is void.
-
//in the case the method returned something useful, you could specify
-
// public int DoSomething() { /* details... */ }
-
-
DateTime MyVal = DateTime.Now; //A variable - it's the same as a
-
//field but it's inside a method and
-
//can only be referenced by code
-
//inside this method that occurs after
-
//the variable declaration. Code that
-
//appeared before this variable
-
//declaration, even within this method
-
//can't access it.
-
}
-
}
All of the elements defined in the class are called members, collectively.
I haven't discussed the internal accessor, just FYI, there is one and it can be combined with protected to make "protected internal". You should look those up if you're not already familiar with them.