views:

3090

answers:

5

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

Static myCollection As Collection
+2  A: 

The closest thing to VB.NET's Static is to create a field in the current type. Other than that C# has no equivalent.

Andrew Hare
+2  A: 

No, The CLR does not support this, and VB.NET resorts to compiler tricks to allow it. Ugh.

Otávio Décio
If you don't like compiler tricks, do you use the "yield" keyword in C#?
Joel Coehoorn
@Joel - no, never.
Otávio Décio
+12  A: 

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

chills42
> "notorious for causing problems when code is called repeatedly or from multiple threads" -- Funny, because the VB.Net implementation is considered to be thread-safe.
Joel Coehoorn
Thread-safe and doing what you exspect are two different things...
Daniel Brückner
How did this get Checked as the best answer. The answer is simply NO. It is said, but still a no. The Answer provided here which are a quote from Eric Gunnerson are Mircosoft's cop-out. It is useful simple as that.BTW, Java has had this ability since at least 1.2 and C++ for as long as I can remember. You would think a language that mixes C++, Java and VB would support for the things that all three of them had in common. If three languages support this there must be a good reason, and dropping it with those two reasons is lame and the they obviously are hiding something.
Rodney Foley
Regardless of the excuse, this is still the canonical answer that is given by Microsoft.
chills42
+3  A: 

No there isn't but how is this different then having a static variable at the class level?

Actually if you look into how shared is implemented, it is a compiler trick that creates a static field on the class.

JoshBerke
Yep, it's exactly the same as having one at class level. It's only allowed to be declared at method level because legacy VB's static keyword meant a local's value would persist after function/sub return.
x0n
It's different because VB's "compiler trick" also uses the monitor class to make sure it's thread safe, and because it's scoped to method so access elsewhere will fail (better semantics).
Joel Coehoorn
Its only scoped because the method name is used to name the variable. And I'd assume if you wanted it threadsafe in C# you'd implement a monitor as well. I'd rather see C# implement a static thread safe that wraps access to the variable as they do in VB, but not worry about scoping to a method.
JoshBerke
in VB6 was a static method variable shared accross all instances of a class or scoped to the actual class?
JoshBerke
A: 

I'm pretty sure the C# equivalent is const: therefore:

public const Collection myCollection = new Collection();

I'm not too familiar with VB.NET, so I could be off base, but that will allow you set a variable which cannot be changed.

AllenG
static variables are not constant. A static variable is one where each instance of the class shares the same variable instance. The variable is mutable, and a change to the value in one class will change the value in all other instances of that class.
NerdFury