15

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

Static myCollection As Collection
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Nick O
  • 3,716
  • 6
  • 38
  • 50

5 Answers5

21

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
  • 14,201
  • 3
  • 42
  • 77
  • 6
    > "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 May 08 '09 at 15:37
  • 4
    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 S. Foley Nov 16 '09 at 06:38
  • 1
    Regardless of the excuse, this is still the canonical answer that is given by Microsoft. – chills42 Nov 16 '09 at 19:01
  • just make a 1 method static class with a private static variable. and if you really want your static method in a class with other stuff then have your static method call this new method. – JDPeckham May 08 '18 at 00:32
5

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
  • 66,142
  • 25
  • 126
  • 164
  • 1
    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 May 08 '09 at 15:35
  • 1
    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 May 08 '09 at 15:39
  • 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 May 08 '09 at 15:51
  • in VB6 was a static method variable shared accross all instances of a class or scoped to the actual class? – JoshBerke May 08 '09 at 15:57
  • In VB a "static" variable is scoped like the method. So if the method is shared (C# static) so is the variable. If it is inside an instance method, then it is owned by a specific object. – Jonathan Allen Feb 12 '11 at 00:37
3

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
  • 344,730
  • 71
  • 640
  • 635
1

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

Otávio Décio
  • 73,752
  • 17
  • 161
  • 228
-6

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
  • 8,112
  • 29
  • 40
  • 1
    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 May 08 '09 at 15:39
  • Slight correction. A static variable in C# is shared. In VB a static variable is only shared if the containing function is shared. – Jonathan Allen Feb 12 '11 at 00:39