C# – How do parameters and their usage in methods effect the static/instance design decision

cstaticstatic methods

Just a simple question:

I have read that a class should be made static when it does not modify its instance. So if I have a class which is called Account and it has properties such as Id, Duration, etc and these do not get modified by the class, then this can be made static otherwise it should remain static.

How does this (whether the instance itself, via its properties, mutates) effect the static/instance decision?

Furthermore, if a class takes loads of parameters (say this Account class, sticking to our analogy), but does not modify the instance (so no Account variable changes – nothing like Account.x = y // where y is from another class), I assume this can be still made static? So it's not the parameters which are an issue or where they come from, it's what they do?

If it is a property, the same principles apply as when deciding to make a field static or not (such as if the data the field holds will be expensive to get, then have one field holding it – static – correct me if I am wrong).

I've noticed there are over 100 threads on static methods (this falls into a static method as it is dealing with parameters) on C# and I will read all of these as there are good questions and good answers.

Thanks

Best Answer

It sounds like you're looking to make the class immutable, rather than static?

Here's the definition of a Static Class from C# 3 in a Nutshell: "A class can be marked static, indicating that it must be comprised solely of static members and cannot be subclassed. The System.Console and System.Math classes are good examples of static classes."

In the case of an Account, I assume you want an individual Account instance for each account in your system. So that I don't think you want it to be static.