📜  C#8.0中的静态局部函数

📅  最后修改于: 2021-05-29 15:20:52             🧑  作者: Mango

在C#7.0中,引入了局部函数。局部函数使您可以在已定义方法的主体内部声明一个方法。或者换句话说,我们可以说,本地函数,其范围仅限于其所创建函数的函数的私有函数。局部函数的类型类似于定义它的函数的类型。您只能从其容器成员中调用本地函数。

例子:

// Simple C# program to
// illustrate local function
using System;
  
class Program {
  
    // Main method
    public static void Main()
    {
        // Here SubValue is the local
        // function of the main function
        void SubValue(int a, int b)
        {
            Console.WriteLine("Value of a is: " + a);
            Console.WriteLine("Value of b is: " + b);
            Console.WriteLine("final result: {0}", a - b);
            Console.WriteLine();
        }
  
        // Calling Local function
        SubValue(30, 10);
        SubValue(80, 60);
    }
}

输出:

Value of a is: 30
Value of b is: 10
final result: 20

Value of a is: 80
Value of b is: 60
final result: 20

但是在C#7.0中,不允许将static修饰符与局部函数一起使用,换句话说,不允许您创建静态局部函数。此功能已在C#8.0中添加。在C#8.0中,允许将static修饰符与local函数。这样可以确保静态局部函数不会引用封闭范围或周围范围的任何变量。如果静态局部函数尝试从封闭的范围访问变量,则编译器将引发错误。让我们借助给定的示例来讨论这个概念:

范例1:

// Simple C# program to illustrate 
// the static local function
using System;
  
class Program {
  
    // Main method
    public static void Main()
    {
        // Here AreaofCircle is the local
        // function of the main function
        void AreaofCircle(double a)
        {
            double ar;
            Console.WriteLine("Radius of the circle: " + a);
  
            ar = 3.14 * a * a;
  
            Console.WriteLine("Area of circle: " + ar);
  
            // Calling static local function
            circumference(a);
  
            // Circumference is the Static local function
            static void circumference(double radii)
            {
                double cr;
                cr = 2 * 3.14 * radii;
                Console.WriteLine("Circumference of the circle is: " + cr);
            }
        }
  
        // Calling function
        AreaofCircle(30);
    }
}

输出:

Radius of the circle: 30
Area of circle: 2826
Circumference of the circle is: 188.4

范例2:

// Simple C# program to illustrate
// the static local function
using System;
  
class Program {
  
    // Main method
    public static void Main()
    {
        // Here AreaofCircle is the local
        // the function of the main function
        void AreaofCircle(double a)
        {
            double ar;
            Console.WriteLine("Radiius of the circle: " + a);
  
            ar = 3.14 * a * a;
  
            Console.WriteLine("Area of circle: " + ar);
  
            // Circumference is the Static local function
            // If circumferenc() try to access the enclosing 
            // scope variable, then the compile will give error
            static void circumference()
            {
                double cr;
                cr = 2 * 3.14 * a;
                Console.WriteLine("Circumference of the circle is: " + cr);
            }
        }
  
        // Calling function
        AreaofCircle(30);
    }
}

输出:

Error CS8421: A static local function cannot contain a reference to 'a'. (CS8421) (f)