13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Static classes191}public static str<strong>in</strong>g Reverse(str<strong>in</strong>g <strong>in</strong>put){char[] chars = <strong>in</strong>put.ToCharArray();Array.Reverse(chars);return new str<strong>in</strong>g(chars);}All methodsare staticThe private constructor B may seem odd—why have it at all if it’s private and nevergo<strong>in</strong>g to be used? The reason is that if you don’t supply any constructors for a class, the<strong>C#</strong> 1 compiler will always provide a default constructor that is public and parameterless. Inthis case, we don’t want any visible constructors, so we have to provide a private one.This pattern works reasonably well, but <strong>C#</strong> 2 makes it explicit and actively preventsthe type from be<strong>in</strong>g misused. First we’ll see what changes are needed to turn list<strong>in</strong>g 7.3<strong>in</strong>to a “proper” static class as def<strong>in</strong>ed <strong>in</strong> <strong>C#</strong> 2. As you can see from list<strong>in</strong>g 7.4, very littleaction is required.List<strong>in</strong>g 7.4The same utility class as <strong>in</strong> list<strong>in</strong>g 7.3 but converted <strong>in</strong>to a <strong>C#</strong> 2 static classus<strong>in</strong>g System;public static class Str<strong>in</strong>gHelper{public static str<strong>in</strong>g Reverse(str<strong>in</strong>g <strong>in</strong>put){char[] chars = <strong>in</strong>put.ToCharArray();Array.Reverse(chars);return new str<strong>in</strong>g(chars);}}We’ve used the static modifier <strong>in</strong> the class declaration this time <strong>in</strong>stead of sealed,and we haven’t <strong>in</strong>cluded a constructor at all—those are the only code differences. The<strong>C#</strong> 2 compiler knows that a static class shouldn’t have any constructors, so it doesn’tprovide a default one. In fact, the compiler enforces a number of constra<strong>in</strong>ts on theclass def<strong>in</strong>ition:■ It can’t be declared as abstract or sealed, although it’s implicitly both.■ It can’t specify any implemented <strong>in</strong>terfaces.■ It can’t specify a base type.■ It can’t <strong>in</strong>clude any nonstatic members, <strong>in</strong>clud<strong>in</strong>g constructors.■ It can’t <strong>in</strong>clude any operators.■ It can’t <strong>in</strong>clude any protected or protected <strong>in</strong>ternal members.It’s worth not<strong>in</strong>g that although all the members have to be static, you’ve got to explicitlymake them static except for nested types and constants. Although nested types areimplicitly static members of the enclos<strong>in</strong>g class, the nested type itself can be a nonstatictype if that’s required.The compiler not only puts constra<strong>in</strong>ts on the def<strong>in</strong>ition of static classes, though—it also guards aga<strong>in</strong>st their misuse. As it knows that there can never be any <strong>in</strong>stances ofLicensed to Rhona Hadida

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!