Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static variables in C#

In C#, is there a way to put a static variable in a method like VB.Net?

Static myCollection As Collection
like image 946
norlando Avatar asked May 08 '09 15:05

norlando


1 Answers

Why doesn't C# support static method variables?

Q: In C++, it's possible to write a static method variable, and have a variable that can only be accessed from inside the method. C# doesn't provide this feature. Why?

A: There are two reasons C# doesn't have this feature.

First, it is possible to get nearly the same effect by having a class-level static, and adding method statics would require increased complexity.

Second, method level statics are somewhat notorious for causing problems when code is called repeatedly or from multiple threads, and since the definitions are in the methods, it's harder to find the definitions.

-- msdn c# faq

like image 99
chills42 Avatar answered Oct 19 '22 07:10

chills42