Last post Jun 21, 2010 09:51 AM by toas1
Jun 21, 2010 08:39 AM|stuie|LINK
I hope someone can confirm my if my understanding on this subject is correct or not.
Ok, firstly a static class only ever exists once and is not an instance. Any static members (ie static int NoOfPeople;) is stored in one place and is shared between all sessions (like the old global variables).
Now static methods is where i'm not 100% sure. If I have a static method that doesn't use any other static members could this cause inconstant results, example (this is a fairly pointless method but just a quick example of the top of my head)
public static class MyQuickMaths
public static int SumThenHalf(int a, int b)
int totalToReturn = a + b;
return totalToReturn / 2;
So in this example if two sessions (or threads) were to call this at the same time - would they both get back the expected results, because the method only uses private data (a, b and totalToReturn).
Im sure this sounds a little simple but I will be using static methods to build user objects and various other objects that there will have to be a 100% garentee that the objects will not get mixed up between sessions and the wrong things return to the user.
Thanks in advance.
Jun 21, 2010 08:55 AM|MetalAsp.Net|LINK
Since you're not using any static variables in the method, I think you're OK. The calculation is only based on the parameters passed in. My bet is that you're OK.
Jun 21, 2010 09:00 AM|stuie|LINK
That's what I would have thought - I will be doing some database stuff from a static method and was worried the results returned may get a bit skewed if multiple request were happing at once.
Jun 21, 2010 09:16 AM|toas1|LINK
this is not an easy question. I sounds like it should be, all thats needed is a simple yes or no but since not even Microsoft MVPs can answer it (check this link
http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/5b006850-a262-46ba-a179-98154ba36898) I would put a lock inside my method to be on the safe side.
Jun 21, 2010 09:49 AM|stuie|LINK
This is the hard thing about it... no-one can really give a definite answer. I think I will stick to creating a shared object and locking it.
Unless anyone else has any other suggestion or contribution to this thread.
Also - thanks toas1 for the link, good article explaining the ins and outs.
Jun 21, 2010 09:51 AM|toas1|LINK
I would go for that too