Non static class with only static methods
-
I have noticed looking at MS source that they regularly do this for a class with only static methods instead of creating a static class:
public class ClassName
{
private ClassName()
{ }public static void Method() { // ... }
}
Is there any benifit to this that I'm missing over a static class?
Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)COM Interop? COM requires a public constructor without parameters, and there it is. And isn't it possible to access class methods through an instance with COM? When I remember those times when I was VB programmer correctly, that was possible, but I am not sure.
-
I have noticed looking at MS source that they regularly do this for a class with only static methods instead of creating a static class:
public class ClassName
{
private ClassName()
{ }public static void Method() { // ... }
}
Is there any benifit to this that I'm missing over a static class?
Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)I think this is laziness on the part of MS. If (when?) the class definition changes in the Framework, the documentarians won't have to root around in the code to make sure that every method is still static.
-
Pre version 2? Backward compatibility?
Which sounds plausable, but why allow the class to be instanced (by making it non-static) when it can never be instantiated (because the constructor is private)? Backwards compatibility is going to break anyway, just in a much more awkward spot.
-
I have noticed looking at MS source that they regularly do this for a class with only static methods instead of creating a static class:
public class ClassName
{
private ClassName()
{ }public static void Method() { // ... }
}
Is there any benifit to this that I'm missing over a static class?
Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)You could use static methods if the functionality of the method is not dependent on the instance properties/methods. Consider the following class as an example. There is a static "Parse" method that takes in a string and return a new instance of "Data". This static function "does not" depend on any of the instance properties/methods of the Data class. But it just creates an instance of the Data class for you to use. So, any other class that wishes to create an instance of Data (which has a ',' seperated int's) can do so by calling
Data.Parse(_stringValue)
.public class Data
{
private List a;public Data() { a = new List(); } public Data(int\[\] nums) { a = new List(); // add to a } public static Data Parse(string nums) { // say nums = "1,2,3 string\[\] \_nums = nums.Split(",".ToCharArray()); List iList = new List(); foreach(string s in nums) iList.Add(int32.Parse(s)); return new Data(iList.ToArray()); }
}
PS - This is a just a trivial example of the usage of static methods in a non-static class. Edit - If there are only static methods in a non-static, it might be useless, but it could also be for futuristic reasons. They could add to this class more instance properties / methods in the future that would use these static methods.
Cheers, Karthik
-
I think this is laziness on the part of MS. If (when?) the class definition changes in the Framework, the documentarians won't have to root around in the code to make sure that every method is still static.
-
I don't think your argument is valid. Static methods in non-static classes have their own uses. I have given one such (trivial) example.
Cheers, Karthik
I agree: I use static methods in non-static classes all the time. But the question was about non-static classes with nothing BUT static methods.
-
I agree: I use static methods in non-static classes all the time. But the question was about non-static classes with nothing BUT static methods.
-
Which sounds plausable, but why allow the class to be instanced (by making it non-static) when it can never be instantiated (because the constructor is private)? Backwards compatibility is going to break anyway, just in a much more awkward spot.
Private, schmivate; I can call a private constructor or anything else -- which is why they introduced static classes in .net 2.
-
Perhaps so the class can be inherited? just a guess
Don't vote my posts down just because you don't understand them - if you lack the superior intelligence that I possess then simply walk away
A class with a single private default constructor cannot be inherited. It will always need some way to instantiate the parent...
-
A class with a single private default constructor cannot be inherited. It will always need some way to instantiate the parent...