Destroying Objects in C#
(Page 1 of 5 )
In this third part of a four-part series on classes and objects in C#, you'll learn how to invoke static methods, how to use destructors, and more. This article is excerpted from chapter four of the book
Programming C# 3.0, fifth edition, written by Jesse Liberty and Donald Xie (O'Reilly, 2008; ISBN: 0596527438). Copyright © 2008 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.
Invoking Static Methods
The Main() method is static. Static methods are said to operate on the class, rather than on an instance of the class. They don’t have a thisreference, as there is no instance to point to.
Java programmers take note: in C#, calling static methods through instance variables is not permitted.
Static methods can’t directly access nonstatic members. ForMain()to call a nonstatic method, it must instantiate an object. Consider Example 4-2, shown earlier.
SomeMethod()is a nonstatic method ofMyClass. ForMain()to access this method, it must first instantiate an object of typeMyClass, and then invoke the method through that object.
Using Static Constructors
If your class declares a static constructor, you are guaranteed that the static constructor will run before any instance of your class is created.*
You can’t control exactly when a static constructor will run, but you do know that it will be after the start of your program and before the first instance is created. Because of this, you can’t assume (or determine) whether an instance is being created.
For example, you might add the following static constructor to theTime class from Example 4-4:
static Time()
{
Name = "Time";
}
Notice that there is no access modifier (e.g.,public) before the static constructor. Access modifiers aren’t allowed on static constructors. In addition, because this is a static member method, you can’t access nonstatic member variables, and soNamemust be declared a static member variable:
private static string Name;
The final change is to add a line toDisplayCurrentTime(), as in the following:
public void DisplayCurrentTime()
{
System.Console.WriteLine("Name: {0}", Name);
System.Console.WriteLine("{0}/{1}/{2} {3}:{4}:{5}",
Month, Date, Year, Hour, Minute, Second);
}
When all these changes are made, the output is:
Name: Time
11/27/2007 7:52:54
Name: Time
11/18/2007 11:45:30
(Your output will vary depending on the date and time you run this code.)
Although this code works, it isn’t necessary to create a static constructor to accomplish this goal. You can, instead, use an initializer:
private static string Name = "Time";
which accomplishes the same thing. Static constructors are useful, however, for setup work that can’t be accomplished with an initializer and that needs to be done only once.
Java programmers take note: in C#, a static constructor will serve where a static initializer would be used in Java.
For example, assume you have an unmanaged bit of code in a legacy DLL. You want to provide a class wrapper for this code. You can callLoadLibrary in your static constructor and initialize the jump table in the static constructor. I discuss handling legacy code and interoperating with unmanaged code in Chapter 22.
Static Classes
In C#, there are no global methods or constants. You might find yourself creating small utility classes that exist only to hold static members. Setting aside whether this is a good design, if you create such a class, you won’t want any instances created. Mark your classStaticto ensure that no instance of the class may be created. Static classes are sealed, and thus you may not create derived types of aStaticclass. Note, however, that static classes may not contain nonstatic members or have a constructor.
Next: Using Static Fields >>
More C# Articles
More By O'Reilly Media