Chris Ongsuco's Weblog
Information Technology, business, life, food...

C# 2.0 static classes

Wednesday, June 08, 2005 8:51 AM

After playing with C# 2.0 I noticed something funny about static classes.  According to the updated C# 2.0 specification (March 2005), static classes are classes that are not intended to be instantiated and which contain only static blah…blah…blah.  If so then suppose to be if I make my class as static then automatically all its methods and properties will also be static BUT that's not the case here. Making a class static will look something like this:

 

using System;

 

namespace DotNet

{

    public static class Settings

    {

    public static string GetName()

    {

    return "MyName";

    }

    }

}

 

Notice that I still have to declare the GetName() method as static.  Why can’t I make it something like this?

 

using System;

 

namespace DotNet

{

    public static class Settings

    {

    public string GetName()

    {

    return "MyName";

    }

    }

}

 

Declaring a class static is already understood that it can’t be instantiated anyway. Now assuming that we have a class with ONLY ONE method, when calling GetName() method what is the difference between my two examples below?

 

Sample 1:

using System;

 

namespace DotNet

{

    public static class Settings

    {

    public static string GetName()

    {

    return "MyName";

    }

    }

}

 

And

 

Sample 2:

using System;

 

namespace DotNet

{

    public class Settings

    {

    public static string GetName()

    {

    return "MyName";

    }

    }

}

 

Calling the GetName() method in both samples would look like this:

 

Sample 1 – DotNet.Settings.GetName();

Sample 2 – DotNet.Settings.GetName();

 

If maybe Microsoft can change it to something like this it would be great:

 

using System;

 

namespace DotNet

{

    public static class Settings

    {

    public string GetName()

    {

    return "MyName";

    }

    }

}


Feedback

# re: C# 2.0 static classes

I see your perspective. However, the compiler would then preface the "static" keyword in front of any "non-static" members, this would happen at compile time and in a sense is both helpful and harmful.

Helpful because the compiler would take care of assigning all members of a static class to be static. Harmful because the compiler is "changing the intention" of coder.

IMO, the current way is the best, a simple compiler error helps you write the correct code, so there's no ambiguity. 6/8/2005 4:16 PM | Eric Newton

# re: C# 2.0 static classes

Hmm...so the issue is with the compiler...well then if thats the case then there is nothing special about static classes. Its just a class with "static" keyword. :-) 6/9/2005 7:09 AM | Chris Ongsuco

# re: C# 2.0 static classes

As per your code, following are the difference

Sample 1 code is purely static class, it doesnt allow instance (non static ) member. Moreover you would not allow to create instance of this class.

Sample 2 code is instance class contain static member.

If you define class with static keyword, its purely static class it doesnt allow other than static member and constructor.
12/19/2007 2:01 AM | Omar

# re: C# 2.0 static classes

In certian scenario, you have static methods in non-static class which is fine but you don't let that class to inherit. You can make it from non-static to static. 10/9/2008 2:12 AM | Rahul

# re: C# 2.0 static classes

Microsoft included best practices and they says that if you have static members then why you are putting them in normal classes? this is overhead. all the static methods should be grouped in a class and of course that would/should be static so that we dont need to create any instance.
perfect example is System.Math ( all methods are static) 10/23/2009 4:04 AM | Tarun

Post a comment