简体   繁体   中英

Do I need to make my class static to use a static method?

I have the following:

public class Sort
{

    public static void ShuffleGenericList<T>(IList<T> list)
    {
        //generate a Random instance
        Random rnd = new Random();
        //get the count of items in the list
        int i = list.Count();

        ...
        ...
    }
}

Should my class also be static or is it okay with just the method being static?

Marking classes as static is purely optional (if they don't contain extension methods) .

In other words, a static class ' members must also be static , but generally static members can be declared in any class :

// doesn't compile:
static class StaticClass { 
    void M() { }
}

// compiles:
class JustClass { 
    static void M1() { }
    void M2() { }
}

From the common sense, you should (but don't have to) mark class as static when:

  • it has no instance members;
  • is never supposed to be instantiated;
  • it is not intended to be used as variable, parameter or member type;
  • all of the above is true and you want to express it with a compiler-enforced constraint.

For example, new Sort() might or might not make any sense in your code, depending on what's in the class, etc. From the sample you provided, I think marking it static is the way to go.

Note, though, that you have to mark class as static when it contains extension methods you intend to use in other parts of your code. Classes that contain extension methods must be static.

The answer is the class doesn't need to be static, but let's look at the logic here.

If we have a sort class (a utility class), it doesn't make sense to instantiate it. So then why not make the class static? Programming isn't just about the end-result -- it's also about making the code readable.

On the other side, why might you have a static method in a non-static class? I haven't come across this too many times, but a good use might be a utility function that you intend to use only from that class.

The method being static is sufficient.

To test, simply try calling it:

Sort.ShuffleGenericList([some list])

It is fine to expose individual static members from a class.

Check these MSDN links for details:

Note that if you mark the class itself as static then you can no longer instantiate it and use multiple copies referenced throughout your application - you restrict it to a one-stop shop, so to speak.

Per Microsoft, static methods don't have to be in static classes.

http://msdn.microsoft.com/en-us/library/79b3xss3(v=vs.80).aspx

If your class does not have any instance member and only static methods, you should make it static.

You are in no way not obliged to do so, the code will compile and execute just fine without doing so, but marking it static has some advantages, the most important being you won't be able to create an instance by mistake.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM