Wednesday, March 26, 2008
« Online banking using CardSpace | Main | C# 2.0 features not covered in Whirlwind... »

My fourth screencast on modern C# language features is posted on Microsoft's Channel 9 — Whirlwind 4: What's new is C# 2 - Accessors, Static Classes, Nullable Types (9:40). This installment wraps up the major C# 2.0 features, so next time we'll be jumping into the C# 3.0 goodness.

In addition to a few resources on today's topics, I've also provided some code showing a sample of using accessor visibility and a static class. And for the careful listener, I  have some small corrections to the screencast.

Resources

Asymmetric Accessor Accessibility, C# Programming Guide, MSDN Library

Access Modifiers, C# Programming Guide, MSDN Library

Static Classes and Static Class Members, C# Programming Guide, MSDN Library

static, C# Language Reference, MSDN Library

Nullable Types, C# Programming Guide, MSDN Library

?? Operator, C# Language Reference, MSDN Library

Nullable<T> Generic Structure, .NET Framework Class Library, MSDN Library

C# Whidbey Featurette #3: Static classes, blog post by Eric Gunnerson, C# program manager. Eric provides some justification for the static class in C# 2.0.

Get a Charge From Statics with Seven Essential Programming Tips, K. Scott Allen, MSDN Magazine, June 2005.

Nullable types in C#, blog post by Eric Gunnerson, C# program manager.

Create Elegant Code With Anonymous Methods, Iterators, And Partial Classes, Juval Löwy, MSDN Magazine, Visual Studio 2005 Guided Tour issue, 2006 (Vol. 21, No. 3)

Code sample

Here is a sample that illustrates use of accessor visibility and static classes. 

using System;
using System.Collections.Generic;

namespace CSharpWhirlwind4
{
 
public class Animal
  {
   
private string name;
   
public string Name
    {
     
get { return name; }
     
private set { name = value; }  // restrict accessor visibility
    }

   
// private constructor
    private Animal( string name ) { Name = name; }

   
// nested static class, manages Animal instances
    public static class ClassFactory
    {
     
private static Dictionary<string, Animal> animals
        =
new Dictionary<string, Animal>();

     
public static Animal Create( string name )
      {
       
if ( !animals.ContainsKey( name ) )
          animals[ name ] =
new Animal( name );
       
return animals[ name ];
      }
    }
  }

 
// ...
}

The Animal class has a Name property which is marked with public visibility. Within the Name property, the set accessor is declared with private visibility, so that only members of the Animal class can set the Name property.

The Animal constructor is also declared with private visibility, so Animal cannot be instantiated except by members of the Animal class. The constructor uses the private set accessor on the Name property.

Inside of the Animal class, there is a nested class named ClassFactory which is responsible for managing Animal instances. The developer's intention is that Animal.ClassFactory should only contain static members, and therefore never be instantiated. That is indicated by declaring the class to be static.

imageTake a look at the resulting assembly in ildasm. The ClassFactory class (.class) is marked as abstract   and sealed. As an abstract class, it cannot be instantiated. And because it is sealed, it cannot be inherited by another class. Also note that the static ClassFactory has a default class constructor (.cctor), which is in fact permitted on static classes since that is a static member.

Since ClassFactory is a nested class in the Animal class, it is a member of the Animal class, and so has access to Animal's private constructor.

In this example, the ClassFactory ensures that no more than one instance of an Animal of a given name is created, as illustrated here.

Animal w1 = Animal.ClassFactory.Create( "wombat" );
Animal w2 = Animal.ClassFactory.Create( "wombat" );
Debug.Assert( w1 == w2 );   // two references to the same object

Errata

The devil is always in the details.

At 1:31, in a bout of overly excessive exuberance, on accessor visibility I said  you can change the visibility of "one, the other, or both" accessors.  Every word of that is true... except for the "or both" part.

At the end of the section on static classes (4:17) I misspoke when I said the static class "exists only because it has private members." Pretend you really heard me say it "exists only because it has static members."

Previous episodes

Comments are closed.