-
benefits of static
If I have a class that has some functions in it. Is there any benefit to using static to declare the functions? I'm doing something like this:
http://msdn.microsoft.com/library/de...ientsocket.asp
and I was curious as to why they use static functions within the class. Thanks,
Jacob438
-
They use statis methods, so that the class does'nt have to be instantiated to use the methods.
eg
Code:
class MyClass
{
public static MyMethod()
{
Console.Writeline("Class does'nt have to be instantiated to use this method");
}
}
MyClass.MyMethod();
}
You'll see these in utility classes. When you have a bunch of methods, you just stick them in a class and declare them static.
Also if you are going to be using static methods, the variables in that class have to be static too.
-
ah, cool, thanks, I read the description in MSDN about static classes and even looked through the examples and I understood what it meant, but not what the point was, thanks a lot DevGrp, sometimes it helps to have something explained by somebody else.
Thanks,
Jacob438
-
-
Also, if you are going to use it as a straight utility class, you may want to implement a private default constructor and/or static constructor. The private default constructor will block users from instantiating your class directly, and a static constructor can be used to initialize members (static) when the class is first loaded in memory.
-
While you're at it, you can also used the sealed keyword to prevent the class from being inherited.