Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Our system has several utility classes.

Some people on our team use (A) a class with all-static methods and a private constructor.

Others use (B) a class with all-static methods (these the juniors).

On code analysis, (A) and (B) raise warning CA1052, which recommends marking the class as sealed.

Included in the MSDN documentation there is the following advice:

If you are targeting .NET Framework 2.0 or earlier, a better approach is to mark the type as static.

Why does this make any sense? I would have thought the opposite; AFAIK, previous to 2.0 there was no way to mark a class as static.

share|improve this question
add comment

3 Answers

up vote 7 down vote accepted

The MSDN page is wrong; it should say "later" not "earlier". static classes were introduced in .NET 2.0.

share|improve this answer
    
Thanks. Now I just need to convince my boss. –  sq33G Nov 12 '11 at 18:06
add comment

I've seen a good number of utility classes in the pre-2.0 era which have all static methods and a private default constructor to make it non-instantiable. Even back then though, that class should have been sealed.

2.0 and later, simply mark the class as static and be done with it.

share|improve this answer
add comment

The CA1052 warning is a little off-target. You should declare classes that contain only static members as static rather than sealed. Classes declared static are implicitly sealed.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.