C#中可以释放变量吗?

发布于 2024-07-30 02:14:52 字数 297 浏览 6 评论 0原文

我只是好奇...

是否可以在 C# 中释放变量?

我的意思是,你能做这样的事情吗:

...
string x = "billy bob";

//release variable x somehow

string x = "some other string";

//release variable x somehow

int x = 329;
...

...所以基本上你是在同一范围内多次声明变量。

如果这在 C# 中不可能,那么什么语言可以?

I'm just curious...

Is it possible to release a variable in c#?

What I mean is, can you do something like this:

...
string x = "billy bob";

//release variable x somehow

string x = "some other string";

//release variable x somehow

int x = 329;
...

... so basically you are declaring the variable multiple times in the same scope.

If this isn't possible in c#, what languages is it possible in?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

末が日狂欢 2024-08-06 02:14:52

不,这在同一范围内的 C# 中是不可能的 - 对此我非常感激。 这听起来像是导致可读性灾难的良方。 正如 Greg 所示,您可以仅出于此目的在方法中引入自己的范围。

我不知道哪些语言允许你这样做 - 弱类型语言当然通常会让你分配任意值,但我不知道有一种语言具有带有类型信息的显式变量声明并且还允许在同一范围内重新声明。

您可能想阅读 Eric Lippert 的最新博客文章,即 所有关于范围。 (如果您还没有阅读 Eric 的博客,您可能需要在日历中腾出几天时间并阅读所有后面的帖子……或者至少是自他加入 C# 团队以来的那些帖子。准备好让您大吃一惊吧反复。)

No, this isn't possible in C# within the same scope - for which I'm very grateful. It sounds like a recipe for readability disaster. As Greg has shown, you can introduce your own scopes within a method solely for this purpose though.

I don't know which languages allow you to do this - weakly typed languages will often let you assign arbitrary values of course, but I don't know of a language which has explicit variable declarations with type information and also allows a redeclaration in the same scope.

You might want to read Eric Lippert's most recent blog post, which is all about scope. (If you don't already read Eric's blog, you might want to clear out a few days in your calendar and read all the back posts... or at least the ones since he joined the C# team. Prepare to have your mind blown repeatedly.)

凉月流沐 2024-08-06 02:14:52

你可以重用一个字符串:

string x = "billy bob";
x = "some other string";

你为什么要这样做?

也许你可以这样做:

object x = "billy bob"
x = "some other string";
x = 329;

然后按你喜欢的方式投射它?

you can reuse a string:

string x = "billy bob";
x = "some other string";

Why would you want to do this?

Maybe you can do this:

object x = "billy bob"
x = "some other string";
x = 329;

Then cast it as you like?

弱骨蛰伏 2024-08-06 02:14:52

当然,您可以重新分配变量:

string s = "billy bob";
s = "something else";

您不能在同一范围内重新声明变量:

// This is illegal:
string s = "billy bob";
string s = "foo"

由于 Object 是 .NET 中类型系统的根,因此您可以执行以下操作:

object o = "billy bob";
o = "foo";
o = 5;

You can reassign variables, of course:

string s = "billy bob";
s = "something else";

You can't redeclare variables in the same scope:

// This is illegal:
string s = "billy bob";
string s = "foo"

Since Object is the root of the type system in .NET, you can do something like this:

object o = "billy bob";
o = "foo";
o = 5;
舞袖。长 2024-08-06 02:14:52

您可以使用作用域(宽松的术语;正式地我相信这将是一个声明空间)来实现,例如,以下内容在方法体内是完全合法的 C#:

{
    string x = "billy bob";    
}
{
    string x = "some other string";
}
{
    int x = 329;
}

不过,我不建议编写这样的代码,它使它变得太容易了让方法中的变量变得混乱。 最好使用不同的、更具描述性的名称,或者将每个块分成自己的方法。

You can do it with scopes (loose term; formally I believe this would be a declaration space), e.g. the following is perfectly legal C# within a method body:

{
    string x = "billy bob";    
}
{
    string x = "some other string";
}
{
    int x = 329;
}

I wouldn't advise writing code like this though, it makes it far too easy to get variables confused in a method. Better to use different, more descriptive names, or separate each block out into its own method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文