Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline
Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Free Trial Issue of Visual Studio Magazine

email article
printer friendly
more resources

Hidden Gems in C# 2.0
Lesser-known features in C# 2.0 will help you write less code that does more.
by Bill Wagner

August 22, 2005

You've likely heard about the four major features added to the C# language in version 2.0: Generics, Iterators, Partial Types, and Anonymous Delegates. But many other features were added to this new version of C#. These other features will help you express your designs more clearly, in less code. You should become familiar with these enhancements to your favorite language now. That knowledge will help you create code today that can be easily enhanced tomorrow. You want your code ready for C# 2.0 when you are.

Static Classes
You've probably already created classes that contain only static methods and fields. These methods are essentially global utilities for your application domain. It's not a sign of a bad design; the .NET Framework System.Math class is an example of this construct. C# 2.0 adds a new use for the static keyword for just this purpose. Static classes have a number of restrictions, which communicate your design intent to other developers:

  1. Static classes cannot have instance constructors. This restriction means that developers cannot create an instance of any object of a static type. You are communicating that your static class is a means of organizing similar functionality. A static class is not an abstraction that will be used to create other types or instances of objects. From this first restriction, others follow:
  2. Static classes are implicitly sealed. No developer can create a class that is derived from a static class.
  3. Static classes cannot have instance members. If you can't create an instance of the object, it makes no sense to define instance members. (Obviously, this also means that static classes cannot have virtual members.)
ADVERTISEMENT

To achieve similar behavior in C# 1.x, you define a private constructor in your class, and you define your class as sealed and abstract. Defining it as sealed prevents users from creating derived classes; defining it as abstract, combined with the private constructor, prevents users from creating an instance of this class. As you move to C# 2.0, use static classes for this idiom, because the same constructs implement the singleton pattern.

Accessor Accessibility
It's common to create a class where you have a public read-only property, and yet you want to allow derived classes or other classes in your assembly to modify the value of the property. In C# 1.x, you wrote this:

public int MyValue
{
  get { return val; }
}

protected void SetMyValue( 
  int theNewValue ) 
{
  This.val = theNewValue; 
}



Back to top














Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTP Home