c#-4.0 - C# 外部整数?如何跨类和命名空间创建全局变量?

标签 c#-4.0 extern

作为一名老 C/C++ 程序员,我想在整个过程中保留一个全局 int 计数器 我的所有命名空间和类。

public static extern int EventCount;

不工作; VS2010 编译器不允许我使用 extern int。 即使使用DLLImport

[DllImport ( "SilverlightApplication37.dll" )]
public static extern int EventCount;

VS2010 提示,

Error   1   The modifier 'extern' is not valid for this item    

那么如何在我的所有代码中拥有全局 int 呢?

干杯!

K博士

最佳答案

没有这样的事情。为了保存数据,C# 有字段、属性和局部变量。您可以创建一个静态类,然后创建一个属性,如下所示:

public static class GlobalData
{    
    public static int EventCount { get; set; }
}

关于c#-4.0 - C# 外部整数?如何跨类和命名空间创建全局变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4584939/

相关文章:

c - 为什么 'extern'存储类的功能不同?

c - C语言在头文件中定义外部函数(模块)

C++ : extern variable inside namespace and printf vs cout

c#-4.0 - 查找 List<T> 倒数第二个元素

c# - Word自动化打印输出: wait for its finished?

wpf - 比较剪贴板中的 IDataObject

c++ - extern 在 C++ 中如何工作?

javascript - 如何将 javascript 函数的输出传递给 asp 按钮 CommandArgument

c#-4.0 - List<T> 的意外内存使用

c++ - 默认情况下全局变量是extern还是相当于在全局中用extern声明变量?