全局变量,静态变量,即时变量的生命期 [英] Life Time of Global Variable, Static Variable , Instant Variable

查看:180
本文介绍了全局变量,静态变量,即时变量的生命期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么区别b / w全局变量,静态变量,即时变量







1.全局变量的生命周期

2.静态变量的生命周期

3.即时变量的生命周期





提前谢谢

解决方案

如果我将其声明为公共全局变量范围,那么我们可以访问通过项目

这是正确与否




是 - 并且没有。

实际上更不。



C#没有全局变量的概念,它们不存在。 C#中的所有内容都以某种方式包含在某种形式的类中。因此,添加 public 不会使变量成为全局变量,因为在C ++中将其声明为类,或者在方法或函数之外声明它,但在模块在VB中完成。

公共变量的变量是:

1)烦人的人必须保持你的代码 - 你应该保留变量(或字段为更为人所知)私有并通过属性公开对它们的任何访问。

2)使变量在类外可用。这与全局变量不同,因为它仍然需要一个类实例来访问它:

  public   class  MyClass 
{
public int MyInt = 666 ;
}
...
MyClass mc = new MyClass();
Console.WriteLine(mc.MyInt);





所以回答你的三个问题:

1)不存在 - C#不支持全局变量。

2)从创建变量到应用程序结束。

3)假设你的意思是瞬发变量表示在方法中声明的一个,它取决于它发生了什么。如果它是一个值类型,那么它的生命周期就是方法本身的生命周期(如果它超出范围,则更少)。如果它是引用类型,那么当它超出范围时它将可用于处理,假设您没有从方法中传递对它的引用。任何引用类型的实际生命周期都是不确定的,因为它完全取决于垃圾收集器执行周期,如果它没有明确地处置。



dunno那里发生了什么 - 所有奇怪的代码块... - OriginalGriff [/ edit]


嗨哥们........





这些链接有助于回答你的问题.......



http://msdn.microsoft.com/en-us/library/35styb3a%28v=vs.71%29.aspx [ ^ ]



http://stackoverflow.com/questions/5986051/c-sharp-static-variables-scope-and-persistence [ ^ ]



http://www.eskimo.com/~scs/cclass/notes/sx4b.html [ ^ ]



乐于助人

What is the difference b/w Global Variable, Static Variable , Instant Variable

and

1. Life Time of Global Variable
2. Life Time of Static Variable
3. Life Time of Instant Variable


Thanks in advance

解决方案

"Global Variable Scope if i declared it as a Public then we can access through out the Project
is this correct or not"


Yes - and no.
More "No" in actual fact.

C# has no concept of global variables, they do not exist. Everything in C# is contained within a class of some form, in some way. So adding public does not make a variable a global variable in the sense that declaring it outside of a class does in C++, or declaring it outside a method or function, but in a module does in VB.
What declaring a variable as public does is:
1) Annoy people who have to maintain your code - you should keep variables (or fields as they are better known) private and expose any access to them via properties instead.
2) Make the variable available outside the class. This is not the same as a global variable, because it still requires a class instance to access it:

public class MyClass
   {
   public int MyInt = 666;
   }
...
   MyClass mc = new MyClass();
   Console.WriteLine(mc.MyInt);



So in answer to your three questions:
1) Nonexistent - C# does not support global variables.
2) From creation of the variable to the end of the application.
3) Assuming you mean "Instant Variable" to mean one declared within a method, it depends on what happens to it. If it is a value type, then it''s lifetime is that of the method itself (or less if it goes out of scope). If it is a reference type, then it will become available for disposal when it goes out of scope, assuming you do not pass a reference to it out of the method. The actual lifetime of any reference type is indeterminate as it wholly depends on the Garbage Collector execution cycle if it is not explicitly Disposed.

[edit]dunno what happened there - code block all wierd... - OriginalGriff[/edit]


Hi buddy........


these links are helpful to answer your question.......

http://msdn.microsoft.com/en-us/library/35styb3a%28v=vs.71%29.aspx[^]

http://stackoverflow.com/questions/5986051/c-sharp-static-variables-scope-and-persistence[^]

http://www.eskimo.com/~scs/cclass/notes/sx4b.html[^]

happy to help


这篇关于全局变量,静态变量,即时变量的生命期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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