Click here to Skip to main content
15,867,885 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi guys.

I'm a beginner, and as far as I know the Code Entry Point has to be static. But you can only reference static variables and static methods within theese static Main class.

I didnt made big multithreading yet, so I basically made absolutely everything static. Now I'm starting with Unit testing and it seems like that static variables and static methods are a pain in the a** for unit testing.

What do you recommend me, should I make everything non-static and referende it?

I'm kind of lost here.

Thanks, for all your help.

Sincerely:
~Jonas
Posted
Comments
Richard MacCutchan 16-Feb-15 7:08am    
No they certainly should not be static. You should study the language structure in more detail, especially classes and objects.
John C Rayan 16-Feb-15 7:11am    
Hi
There must be a valid reason behind every class design to make it as a static or non-static. It depends on your requirement. What are you trying to do?. Static means there will be always only one instance of class available. is that you are trying to implement?

1 solution

As little as possible should be static - excepting extension methods, which have to be, and some methods which are "generic" to an application rather than specific to a class.

C# is an OOPs language - and the first "O" is for "Object"! If you make everything static, then you are ignoring what makes the language work in the first place.
It's just a different way of looking at things: instead of the procedure being the important thing as in a "traditional" application, the object becomes capable of dealing with itself, and thus it's a lot easier to handle multiple instances of the same type of object.

Think of a mother with half a dozen children: if she is to get them all to school on time, it all works better if the children know how to dress themselves, so the mother can get on with other things, like sorting out breakfast. So Dress becomes a method of the class Child, and the mother just issues instructions:
C#
mike.Dress();
joe.Dress();
sandra.Dress();
...
Or
C#
foreach (Child child in children)
   {
   child.Dress();
   }

Instead of having to have the mother work out how to do it:
C#
Dress(mike);
Dress(joe);
...
In the OOPs (non-static) version, the child knows how to dress itself (and you can have different methods for BoyChild and GirlChild - both of which are derived from Child - to put a dress on the girls, and trousers on the boys for example).
In the older "traditional" method, the mother knows how to dress a child and has to check in her code what type of child she is dressing to get the right clothes on.

There is a heck of a lot more to it all that this, but that's about it for a small test box! :laugh:
 
Share this answer
 
Comments
[no name] 16-Feb-15 7:42am    
Thanks, this helps me a lot.

Thankse for the childcrem example.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900