set {
MyField = value;
}
}
public event EventHandler MyEvent;
public int this[int index] {
get {
return 0;
}
set {
Console.WriteLine("this[{0}] = {1}", index, value);
}
}
public static MyClass operator+(MyClass a, MyClass b) {
return new MyClass(a.MyField + b.MyField);
}
public MyClass() {
Console.WriteLine("Instance constructor");
}
public MyClass(int value) {
MyField = value;
Console.WriteLine("Instance constructor");
}
~MyClass() {
Console.WriteLine("Finalizer");
}
static MyClass() {
MyStaticField *= 2;
Console.WriteLine("Static constructor");
}
internal class MyNestedClass
{}
}
shows a class that contains each kind of member. The example
class Test
{
static void Main() {
// Instance constructor usage
MyClass a = new MyClass();
MyClass b = new MyClass(123);
// Constant usage
Console.WriteLine("MyConst = {0}", MyClass.MyConst);
// Field usage
a.MyField++;
Console.WriteLine("a.MyField = {0}", a.MyField);
// Method usage
a.MyMethod();
// Property usage
a.MyProperty++;
Console.WriteLine("a.MyProperty = {0}", a.MyProperty);
// Indexer usage
a[3] = a[1] = a[2];
Console.WriteLine("a[3] = {0}", a[3]);
// Event usage
a.MyEvent += new EventHandler(MyHandler);
// Overloaded operator usage
MyClass c = a + b;
// Nested type usage
MyClass.MyNestedClass d = new MyClass.MyNestedClass();
}
static void MyHandler(object sender, EventArgs e) {
Console.WriteLine("Test.MyHandler");
}
}
shows uses of these members
No comments:
Post a Comment