question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow local variables to be static and/or readonly (deeper scoping)

See original GitHub issue

Proposal

Allow local variables (within method blocks) to be marked as static, readonly, or static readonly.

Purpose

This would allow a developer to have the advantage of static and readonly fields, but limit their usage to a fine-grained scope.

Example:

class Foo
{
    public void Method()
    {
        static readonly var codes = new[] { 1, 7, 10 };
    }
}

This would help alleviate situations such as reallocating a new array (and assigning the value to fibonaccis) upon each execution of Method():

class Foo
{
    public void Method()
    {
        // This should be static readonly, but only accessible in this method.
        var fibonaccis = new int[] { 0, 1, 2, 5, 8, 13, ...,  701408733 };

        ...
    }
}

Or, it could help avoid potential issues with static field being accessible outside of its intended scope, purposefully or accidentally:

class Foo
{
    // This variable should only be used in Method().
    // Please do not change its value on me somewhere else!
    private static int _methodCount = 0;

    public void Method()
    {
        var count = ++_methodCount;

        ...
    }
}

Implementation

The Roslyn compiler could recognize the syntax and generate the necessary IL code, just as it does for automatic properties, async/await, foreach, etc.

Originally, the discussion from codeplex talked about the potential issues and confusion, and the final thought I had was to lift the local variable as a Lazy<T> and for this feature to be purely syntactic sugar. After researching how VB.NET implements local static variables, the compiler does a bit more work to generate the correct IL code. See this article: http://weblogs.asp.net/psteele/7717. I think the same thought should be put into C#.

While the article only talks about static local variables, I think that there would be real value to also allow the readonly keyword to be used as well. The complexity would grow, of course, because we have four possible situations (instead of just two):

  • static without initialization (already supported by VB.NET).
  • static with initialization (already supported by VB.NET).
  • readonly with initialization (not supported by any language).
  • static readonly with initialization (not supported by any language).

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:19
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
sharwellcommented, Jan 21, 2015

💡 If you are going to allow static (without the readonly modifier), you should probably update this to allow static volatile as well.

2reactions
HaloFourcommented, Jan 23, 2015

@shunsukeaida VB.NET already has Static variables which behave as described by this feature request. The keyword was inherited from pre-.NET versions of VB.

Implementing ReadOnly variables would be a new feature for VB.NET, though.

For readonly variables I’m personally a fan of the Apple Swift syntax which uses let as an alternative to var, e.g.:

var x = 1;
let y = 2;
x = 3; // legal
y = 4; // compiler error, y is readonly
Read more comments on GitHub >

github_iconTop Results From Across the Web

c++ initialization static variable in "local scope"
A variable with static storage duration where the initial value is known at compile time, zero initialized or constant initialized is Static ......
Read more >
C# | Types of Variables
Local variables ; Instance variables or Non – Static Variables; Static Variables or Class Variables; Constant Variables; Readonly Variables ...
Read more >
Why use a local variable over a global variable? [duplicate]
Static variables live for the lifetime of the program; allow for restricted scope (to the class) and potentially allow different read vs. write ......
Read more >
Static - Visual Basic
You can use Static only on local variables. This means the declaration context for a Static variable must be a procedure or a...
Read more >
Accessing Variables at Runtime
It can only be determined at run-time. There are two ways to implement access to non-locals under dynamic scope: "deep access" and "shallow...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found