C# .Net Fun Current .Net Version: 1.1.4322

C# Reference
XML Web Services
Contact Us
Feeds
What's New in C# 2.0 - Static Classes
Author:Michael G.
Category(ies):.Net Framework 2.0, C# 2.0
Description:Learn all about the new C# 2.0 Framework feature: Static Classes.

Tested with Microsoft Visual C# 2005 Beta 1, .Net 2.0.40607.42

Well, while not as exciting as many of the other new features of C# 2.0, static classes give us a faster way of implementing classes who's only purpose is to deliver static members. While you can always just create a class with a private constructor, so that it doesn't get instantiated, and then add all static members, static classes enforce those requirements automatically. Static classes are sealed (they're can't be derived from), they can't be instantiated (made an instance object from) and they can't contain a constructor. namespace WhatsNew20StaticClasses { class Program { static void Main(string[] args) { MyStaticClass.PrintMessage("Hello World!"); Console.WriteLine("Press any key to continue..."); Console.ReadKey(); } } public static class MyStaticClass { public static void PrintMessage(String Message) { Console.WriteLine(Message); } } }
 
COPYRIGHT/DISCLAIMER | CONTACT US