为什么不C#支持本地静态变量像C呢? [英] Why doesn't C# support local static variables like C does?

查看:189
本文介绍了为什么不C#支持本地静态变量像C呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么不C#有像C局部静态变量?我错过了!

Why doesn't C# have local static variables like C? I miss that!!

推荐答案

您可以使用委托模拟它...这里是我的示例代码:

You can simulate it using a delegate... Here is my sample code:

public Func<int> Increment()
{
    int num = 0;
    return new Func<int>(() =>
    {
        return num++;
    });
}

您可以调用它是这样的:

You can call it like this:

 Func<int> inc = Increment();
 inc();

这篇关于为什么不C#支持本地静态变量像C呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆