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