// Define type as abstract to prevent
// creation
abstract class TimeUtilClass
{
public static void PrintTime()
{ Console.WriteLine(DateTime.Now.ToShortTimeString()); }
public static void PrintDate()
{ Console.WriteLine(DateTime.Today.ToShortDateString()); }
}
While these constructs are still permissible, the use of static classes is a cleaner solution and
more type-safe, given that the previous two techniques allowed nonstatic members to appear
within the class definition without error.
On a related note, a project’s application object (e.g., the class defining the Main() method) is
often defined as a static class, to ensure it only contains static members and cannot be directly
created. For example:
Do'stlaringiz bilan baham: |