|
-
Nov 19th, 2002, 03:58 PM
#1
Thread Starter
Lively Member
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
-
Nov 21st, 2002, 11:04 AM
#2
Frenzied Member
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.
Dont gain the world and lose your soul
-
Nov 21st, 2002, 01:00 PM
#3
Thread Starter
Lively Member
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
-
Nov 21st, 2002, 06:55 PM
#4
Frenzied Member
No prob
Dont gain the world and lose your soul
-
Nov 21st, 2002, 07:31 PM
#5
PowerPoster
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.
-
Nov 21st, 2002, 09:29 PM
#6
Frenzied Member
While you're at it, you can also used the sealed keyword to prevent the class from being inherited.
Dont gain the world and lose your soul
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|