C# - TPL2141 logo C# - TPL2141

C# only support static scoping (also known as lexical scoping), where the scope of a variable is determined when the code is compiled (Rouse, 2011).

public class Program
{
  int num;

  static void Func1()
  {
    int num = 123;
  }

  public static void Main()
  {
    int num = 567;
    Func1();
    Console.WriteLine($"num1: {num}");
  }
}

The output is

num1: 567

instead of 123 as defined in Func1() (if C# uses dynamic scoping).

Live-code exmaple

References

  1. Rouse, M. (2011, March 1). lexical scoping (static scoping). Retrieved from https://whatis.techtarget.com/definition/lexical-scoping-static-scoping