如何在C-sharp中声明Global Variable ..? [英] How to declare Global Variable..in C-sharp ?

查看:184
本文介绍了如何在C-sharp中声明Global Variable ..?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我解决c Sharp中的全局变量以及如何为其分配内存吗
谢谢..?

Can anyone help me on global variables in c sharp and how memory is allocated to it
thanks..?

推荐答案

http://www.dotnetperls.com /global-variable [ ^ ]


从技术上讲,没有任何东西-一切都是基于类的,因此C#中没有全局变量的概念

但是,如果绝对必须使用它们,则声明一个static类,并在其中使用静态变量:
Technically, there aren''t any - everything is class based, so there is no concept of a global variable in C#

However, if you absolutely must use them, then declare a static class, and use static variables in that:
public static class Globals
   {
   public static string MyString = "Hello";
   }
...
   Console.WriteLine(Globals.MyString); 

通常,您应该尝试避免它们.

Generally, you should try to avoid them though.


http://msdn.microsoft.com/zh-CN/library/ms173109.aspx [ ^ ]:在C#中,没有其他某些语言中的全局变量或方法.甚至程序的入口点Main方法也必须在类或结构中声明

全局变量的行为是通过静态函数和静态变量实现的.
http://msdn.microsoft.com/en-us/library/ms173109.aspx[^]: In C#, there are no global variables or methods as there are in some other languages. Even a program''s entry point, the Main method, must be declared within a class or struct

The behavior of globals is achieved through static functions and static variables


这篇关于如何在C-sharp中声明Global Variable ..?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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