Why doesn't C# support static method variables?

Published 11 May 04 09:41 PM

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.

[Author: Eric Gunnerson]

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Thomas Schittli said on May 12, 2004 4:12 AM:

Thank you for your explanations, but I'm not at all happy with it...

First, there are a lot of C# features which could be done another way, but we like C# because it offers already known features more elegant.
Second, threading as such is also 'somewhat notorious for causing problems' but that does not stop us using it. Additionally, it is not at all elegant to have class-level static variables which in fact belongs to a method. They get degraded to 'class global variables' and it's just a question of time until another developers misuse them and we will earn side effects.
Of course, it's possible to put such a method in it's own class, but I think we don't really have to discuss such 'solutions' here.

I'm sure, that static method variabls must be used carefully, but then, they make sense and are very useful.

kind regards,
Thomas
Schittli
# Jerry Pisk said on May 12, 2004 10:24 AM:
I must agree with Thomas, especially when the C# team decided to add features such as partial types - don't you those are going to make finding definitions of anything a lot harder than method static variables? I hope those three reasons posted are not the actual reasons the C# design team based their decisions on.

As for method static variables being problematic when the method is called from multiple threads - how exactly are they different from class static members? From the implementation point of view they're class static members that are scoped to a single method.
# Jon Skeet said on May 21, 2004 11:02 PM:
I'm with the C# team on this one. A method itself doesn't logically have state outside the period during which it's being called. If you've got a method which has separate state, it should be a separate object - or the state should be part of the class.
# Glen Martin said on June 4, 2004 8:06 AM:
I agree, I don't believe that a method should encapsulate data. That is the purpose of an object.
# Adam Tuliper said on July 6, 2004 10:02 AM:
ironically vb.net supports them. Go figure.
# John Cass said on July 8, 2004 1:53 AM:
It seems to me there are two angles of approach. The language designers are trying to follow a rationale that fits the principles underlying the language. Programmers are trying to do a job and know which features they find useful. The C# team have had a rare opportunity to design a language from scratch and are keen to keep it as ‘clean’ as possible. On the other hand the VB team are re-designing an existing language and, I suspect, are more likely to keep features which are arguably not ideal but serve a very useful purpose. The idea of a variable which is scoped to a method but retains its value between calls is very old and useful in certain circumstances. Enumeration is one which comes to mind. On balance, rather than write a whole class to do a single simple job, I would prefer to be able to do it in a single function.

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required

This Blog

Syndication

Page view tracker