I have a method name "LogClick" that is called 2 or more times per second. I call it in the tradicional way:
Call New MyClass().LogClick()
Everty time I call "LogClick", the "MyClass" is instanciated. So I think: "why not transform this method to shared?"
Publ开发者_如何学运维ic Shared Sub LogClick()
...
End Sub
MyClass.LogClick()
Will I have performance, concurrence ou memory problems transforming this method to shared?
]Hey,
No performance issues per se, but any variables that are static as well will be retained in memory, even in ASP.NET. So you have to account for that. But generally, if there aren't any static variables, performance will be the same as an instance... depending on what's in the instance it may be better with a static method.
HTH.
Shared (static in C#) is inherently non-thread safe, as all your page instances will be accessing the same object.
"Problems" entirely depends on what LogClick() is doing. You'll have to be very careful to analyze the code, assuming that execution will transfer to a different thread after every line is executed, and come back afterwards.
精彩评论