7

In VB.Net, I can declare a variable in a function as Static, like this:

Function EncodeForXml(ByVal data As String) As String
    Static badAmpersand As Regex = new Regex("&(?![a-zA-Z]{2,6};|#[0-9]{2,4};)")

    data = badAmpersand.Replace(data, "&")

    ''// more processing

    return data
End Function

Note that I need to use the keyword Static, rather than Shared, which is the normal way to express this in VB.Net. How can I do this in C#? I can't find its equivalent.

4 Answers 4

14

Ha! In posting the question, I found the answer! Rather than googling for C# I should have been looking for details on how VB.Net implements it, and typing up the question made that apparent to me. After applying that insight, I found this:
http://weblogs.asp.net/psteele/articles/7717.aspx

That article explains that it's not really supported by the CLR, and the VB compiler creates a static (shared) variable "under the hood" in the method's class. To do the same in C#, I have to create the variable myself.

More than that, it uses the Monitor class to make sure the static member is thread-safe as well. Nice.

As a side note: I'd expect to see this in C# sometime soon. The general tactic I've observed from MS is that it doesn't like VB.Net and C# to get too far apart feature-wise. If one language has a feature not supported by the other it tends to become a priority for the language team for the next version.

Sign up to request clarification or add additional context in comments.

2 Comments

And to think, I was going to pimp my own blog (where I addressed this a couple months ago) ... But you've saved me the trouble, and that entry is considerably more clear than my own. Thanks for the link!
You can also check out the IL/C# code generated from VB using reflector, I notice that sometime ago. It's also fun to see what some VB tricks are possible done in C#
6

Personally I'm glad that C# doesn't have this. Logically, methods don't have state: types and instances do. C# makes that logical model clearer, IMO.

4 Comments

Agreed that using this to save state with a method is a bad idea. Note that in this case the purpose is to make sure the creation/compilation of the regex happen only once. You could say this is a form of state, but I think here it's really about performance, not keeping data between method calls.
In that case it's fine just to be a static readonly field. It's a piece of immutable state associated with the type, initialized when the type is initialized. Make sense as a static to me :)
I take it you haven't worked with Closures yet. They literally are mutable state being stored in a method.
Yes, I certainly have worked with closures - but I regard those as encapsulating "method + environment" rather than just a method. (The state itself is stored in a separate class where necessary.)
4

There is no equivalent in C# unfortunately.

You will need to use a class level variable.

This is one of the few things that VB has that I wish C# had.

Comments

1

You have to declare this on the class level:

private static readonly RegEx badAmpersand = new RegEx("...");

1 Comment

That should work in this case, but in the general sense it's more complicated than that because there could be concurrency issues in a multi-threaded app. The VB.Net Static member is supposed to be thread-safe.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.