Sunday, March 2, 2014

When should we use static members in a non-static class ?

at el-shitlony



Hello,


Sometimes you want to reuse the code and it doesn't need to access any instance members so it might be better to use a static method.



void Main()
{
ThrowMe throwMe = new ThrowMe();

throwMe.Throw();
}

public class ThrowMe
{
public void Throw()
{
throw CreateInvalidOperationException();
}

public static Exception CreateInvalidOperationException()
{
return new InvalidOperationException("The operation is invalid");
}
}






at el-shitlony


This is pretty bad example.


An exception is not specific to just one class; it is an object that can be created anywhere, either inside an object or in the main path.


Moreover, the simplicity in creating an exception object does not justify creatlng a static method for it .


It's incredible the amount of idiocies you are able to post.





In the beginning, there was nothing. Then the Big Bang! Now, look at me... I'm ritehere.


No comments:

Post a Comment