如何:在 C# 中取消设置变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/197302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to: unset variable in C#?
提问by Sejanus
It's really funny but I didn't manage to find an answer on my own. How can I unset variable? E.g. php has unset($var) function.
这真的很有趣,但我没有设法自己找到答案。如何取消设置变量?例如 php 有 unset($var) 函数。
采纳答案by Nico
There is not really an equivalent to "unset".
没有真正等同于“未设置”。
The closest match I know is the use of the defaultkeyword.
我所知道的最接近的匹配是使用default关键字。
For example:
例如:
MyType myvar = default(MyType);
string a = default(string);
The variable will still be "set", but it will have its default value.
该变量仍将被“设置”,但它将具有其默认值。
回答by Jon Skeet
You can't. There's no notion of "unsetting" a variable. You can set it to a different value - 0, null, whatever's appropriate. Instance/static variables don't even have a concept of whether the variable is set/unset, and local variables only have "definitely assigned" or "not definitely assigned".
你不能。没有“取消设置”变量的概念。您可以将其设置为不同的值 - 0、空值,任何合适的值。实例/静态变量甚至没有变量是否设置/未设置的概念,局部变量只有“确定分配”或“未确定分配”。
What is it you're trying to achieve?
你想要达到什么目标?
回答by gimel
Maybe you'd like to free the objectthat the variable is referencing:
也许你想释放变量引用的对象:
MyVar = null;
回答by Arry
Generally setting it to null does the job (for variable of types like int you would have to make it a nullable version int?).
通常将它设置为 null 可以完成这项工作(对于像 int 这样类型的变量,您必须将其设置为可空版本 int?)。
If you only want to use the variable for a short period of time in a bigger function you can scope it, like this:
如果您只想在更大的函数中短时间使用该变量,您可以对它进行作用域,如下所示:
{
int i = 2;
}
The variable will only last until the closing brace.
该变量只会持续到右大括号。
If these do not cover your circumstance then can you elaborate on where you need to do this.
如果这些不能涵盖您的情况,那么您能否详细说明您需要在何处执行此操作。
回答by rslite
Value-type variables don't need unset. They are permanently allocated.
值类型变量不需要取消设置。它们是永久分配的。
For reference-type variables you just set them to nulland the garbage collector will destroy the associated object (and free the memory). But note that the variable itself will continue to exist throughout its scope (code block, method, object life, ...)
对于引用类型的变量,您只需将它们设置为null,垃圾收集器将销毁关联的对象(并释放内存)。但请注意,变量本身将继续存在于其整个范围内(代码块、方法、对象生命周期……)
If you want to use this to free memory then just set all not-needed objects to null and wait for the garbage collector to do its job.
如果你想用它来释放内存,那么只需将所有不需要的对象设置为 null 并等待垃圾收集器完成它的工作。
Edit: As noted in comments I ommited to say that the garbage collector won't start the collection immediately. This will happen usually when the framework tries to allocated memory and can't find enough free. It's possible to start "manually" a garbage collection, but it's not advisable and might worsen the behavior of the program. For most purposes the default behavior of the GC should be enough.
编辑:正如我在评论中指出的那样,垃圾收集器不会立即开始收集。这通常会在框架尝试分配内存但找不到足够的空闲空间时发生。可以“手动”启动垃圾回收,但不建议这样做,并且可能会恶化程序的行为。对于大多数用途,GC 的默认行为应该足够了。
回答by benPearce
For an object you can set it to null, a string is best set to String.Empty, or you can declare a variable as nullable such as:
对于可以将其设置为 null 的对象,最好将字符串设置为 String.Empty,或者您可以将变量声明为可为空的,例如:
int? i = null;
回答by Rick Minerich
You could define a scope for that variable. When the scope exits, the variable will no longer be defined:
您可以为该变量定义一个范围。当作用域退出时,变量将不再被定义:
System.Console.WriteLine("let's give this a try: ");
{
int j = 0;
System.Console.WriteLine(j);
}
//Won't compile with the following line.
//System.Console.WriteLine(j);
回答by Rick Minerich
personally I just go
我个人就去
variable = Nothing
变量 = 无
that seems to free up the resources especially when working with mobile phones!
这似乎可以释放资源,尤其是在使用手机时!
回答by Laura
To unset environment variable use the same Environment.SetEnvironmentVariable method, but pass null, or string.Empty as a value http://msdn.microsoft.com/en-us/library/96xafkes(v=vs.110).aspx
要取消设置环境变量,请使用相同的 Environment.SetEnvironmentVariable 方法,但传递 null 或 string.Empty 作为值http://msdn.microsoft.com/en-us/library/96xafkes(v=vs.110).aspx
"If value is empty and the environment variable named by variable exists, the environment variable is deleted."
“如果value为空,并且variable命名的环境变量存在,则删除该环境变量。”