Ron*_*rby 2 .net c# variables scope declaration
我只是好奇...
是否可以在c#中释放变量?
我的意思是,你可以这样做:
...
string x = "billy bob";
//release variable x somehow
string x = "some other string";
//release variable x somehow
int x = 329;
...
Run Code Online (Sandbox Code Playgroud)
...所以基本上你在同一范围内多次声明变量.
如果在c#中无法做到这一点,可以使用哪种语言?
Gre*_*ech 18
您可以使用范围(松散术语;正式地认为这将是一个声明空间),例如以下是方法体内完全合法的C#:
{
string x = "billy bob";
}
{
string x = "some other string";
}
{
int x = 329;
}
Run Code Online (Sandbox Code Playgroud)
我不建议编写这样的代码,但它很容易让方法中的变量混淆.最好使用不同的,更具描述性的名称,或将每个块分成自己的方法.