Hi there,

I'm not too new with C#, but I've had a lot of questions recently (I've learned most of C# through experimentation, so that's probably a reason) that I'd like to help find answers for.

Anyway, sorry for the offtopic start! Let me get back on topic...

Okay, so let's say I have a non-static class (let's call this class Dogs), which calls a certain method (let's call this method Bark) a lot during it's life-time. Would it be more effecient to make this method, Bark, static or non-static?

Let me provide an example if I'm unclear:

Class with a non-static method:

//Class with non-static method
public class Dogs
{
    private bool Barked = false;
    Dogs() { }

    public void BreakIn() { this.Barked = this.Bark(); }
    void See() { this.Barked = this.Bark(); }
    public void Cat() { this.Barked = this.Bark(); }

    //Method that is called a lot:  
    public bool Bark() {
        /*insert barking code here*/
        return true;
    }
}

Class with a static method:

//Class with a static method
public class Dogs
{
    public bool Barked = false;
    Dogs() { }

    public void BreakIn() { this.Barked = Dogs.Bark(); }
    void See() { this.Barked = Dogs.Bark(); }
    public void Cat() { this.Barked = Dogs.Bark(); }

    //Method that is called a lot:  
    public static bool Bark() {
        /*insert barking code here*/
        return true;
    }
}

Which would be more effective? The former (class without a static method) or the latter (the class with the static method?)

I thought that the latter would be more effective, because then there would be one less method to initialize when Dogs initializes, because static classes are always there (cannot find a better word.) But, now that I think about it, if Bark has a lot things it needs to process, could there a problem of some sort?

Correct me if I'm wrong, please. And sorry if this is a silly question!

Thanks in advance,
~Sky

You really aren't saving that much memory (about 4 bytes) so I wouldn't worry about it unless you are building for embeded systems. As for issues, yes it can cause some. What if you decide you need to know how many times your dog barks. If you put a counter in there, it will count how many times all dogs bark, not just the one.

Don't worry about opimizing things, worry about making them work and in writing clear code.

Thanks, Momerath!

Based on how you wrote it, it appears you are creating a non-static class (for either example).

Personal opinion, if you are just using it to access resources (like number crunching for example where you pass in and wait for a return) I would go static, but if you want to store variables of any sort to return (whether now or later on) go non-static.

I personally write Static methods alot because I like to break my program up in classes, usually with one class performing a function (again like a math formula, or something like that where variables are passed in and only need to return once)

In your examples, you call the function from within the class. I think the static/nonstatic decision only matters if calling from outside the class. Then it really depends whether it makes sense for the function to be independent of any particular object. (i.e. an instance of the object.)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.