In addition to instance and static variables, other classes (inner classes), and methods, a C# class can include many other different members. Class members can be divided into two categories:
data members
function [1] members.
[1] This is a terminology thing – the word 'function' (as in function member) does not mean just 'methods'. In OO languages, a method is simply another name for 'function', and some OO developers who graduated from traditional structured programming schools still use the term function to refer to method. In C#, function members include methods and properties, events, indexers, operators, instance constructors, destructors, and static constructors.
Members that can contain executable code are known as function members of the class.
All methods and variables must be declared within a class. [2] The only C# statements that can be outside a class's curly braces are:
[2] Like Java, C# has got rid of global methods or variables – a feature in C/C++.
C# preprocessor directives (see Chapter 24);
using statements (similar to Java's import statement);
namespace statements (similar to Java's package statement);
code comments (of course).
There are several new class members not heard of in Java. Examples include properties, events, indexers, operators, delegates, destructors, and static constructors. Table 6.2 gives a brief description of C# class members. Note the new members which do not have equivalents in Java.
The following C# class contains all the possible members mentioned above: [3]
[3] You might notice (probably with unease) that I have named all my identifiers – except for local variable names – with an initial capital letter. In accordance to C#'s naming convention, only local variable names start with a lower case letter. This might take some getting used to.
1: using System; 2: 3: public class MyClass{ 4: 5: // constant 6: public const string MyConstant = "C Sharp is fun!"; 7: 8: // instance field 9: public int MyField = 2; 10: 11: // another instance field 12: private string[] MyArray = new string[10]; 13: 14: // instance constructor 15: public MyClass(){ 16: Console.WriteLine("1st instance constructor running"); 17: } 18: 19: // overloaded instance constructor 20: public MyClass(int newMyField){ 21: Console.WriteLine("2nd instance constructor running"); 22: MyField = newMyField; 23: } 24: 25: // static constructor 26: static MyClass(){ 27: Console.WriteLine("static constructor running"); 28: } 29: 30: // destructor 31: ~MyClass(){ 32: Console.WriteLine("destructor running"); 33: } 34: 35: // instance method 36: public void DoSomething(){ 37: Console.WriteLine("instance method running"); 38: } 39: 40: // static Main method 41: public static void Main(){ 42: Console.WriteLine("Main method running"); 43: MyClass mc = new MyClass(); 44: mc = null; 45: } 46: 47: // property 48: public int MyProperty{ 49: get{ 50: return MyField; 51: } 52: set{ 53: MyField = value; 54: } 55: } 56: 57: // indexer 58: public string this [int index]{ 59: get{ 60: return MyArray[index]; 61: } 62: set{ 63: MyArray[index] = value; 64: } 65: } 66: 67: // event 68: public event EventHandler MyEvent; 69: 70: // operator method 71: public static MyClass operator + (MyClass a, MyClass b){ 72: return new MyClass(a.MyField + b.MyField); 73: } 74: 75: // nested class 76: class MyNestedClass{ 77: } 78: } // end class
C# Class Member | Nearest equivalent in Java | Comments |
---|---|---|
Data Members | ||
Constant | Final variable | You can declare a C# constant using the const modifier (see section 8.3) |
Read-only variable | Final variable | A read-only variable is somewhat similar to a constant, except that its value can be set in a constructor (see section 8.3.3) |
Event | No direct equivalent | An event is a special delegate instance used for event handling in C# (see Chapter 15) |
Field | Class/instance variable | A field is similar to non-local variables in Java – fields can be static (belongs to the class) or non-static (belongs to the object/instance of the class) |
Function Members | ||
Destructor | Java finalizer (finalize method) | A destructor is a special method in a class which is invoked by the garbage collector before the object is garbage collected – in this aspect, a C# destructor is similar to the Java finalizer (see section 7.5) |
Indexer | No direct equivalent | An indexer is a convenient way to treat an object encapsulating an array field as an array itself (see Chapter 21) |
Instance constructor | Class constructor | The instance constructor is a special method invoked during instantiation of a class (see section 7.3) |
Static constructor | Static initializer | The static constructor is a special method invoked before any static methods/fields are utilized (see section 7.4) |
Method | Method | C# methods are similar to Java methods. A method can be static (belongs to the class) or non-static (belongs to the object/instance of the class) – there are significant differences between how C# methods take in parameters though (see section 7.2) |
Nested type declarations | Inner classes | Both C# and Java support inner/nested classes (see section 6.9) |
Operator (or operator method) | No equivalent | C# supports operator overloading but Java does not – operator overloading is accomplished by having special operator methods in the C# class (see Chapter 22) |
Property | Accessor/mutator methods | A property can be treated as a field which has optional accessor/mutator methods (see Chapter 20) |
18.224.44.53