为什么我不能在 C# 中声明外部变量?

发布于 2024-12-12 00:40:59 字数 428 浏览 0 评论 0原文

我需要在 if 语句中声明一个变量。然后我会在外面使用它。但据我所知,C#中没有外部变量。但我需要这样做。

我有两个类都派生自一个类。

基类:操作数 派生类: NormalOperand SpecialOperand

bool normal

在某处声明,

if(normal)
    NormalOperand o = stack.Pop() as NormalOperand;
else
    SpecialOperand o = stack.Pop() as SpecialOperand;

我不想在下面处理这些差异。有什么黑客可以做到这一点吗?或者我是否必须在任何与此相关的地方处理它?

I need to declare a variable inside an if statement. Then I will use it on the outside. But as far as I know, there are no external variables in C#. But I need to do this.

I have two classes both derived from one class.

Base class: Operand
Derived classes: NormalOperand SpecialOperand

bool normal

Declared somewhere

if(normal)
    NormalOperand o = stack.Pop() as NormalOperand;
else
    SpecialOperand o = stack.Pop() as SpecialOperand;

I don't want to deal this differences below. Is there any hack to do that? Or do I have to deal with it everywhere I do something related to this?

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

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

发布评论

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

评论(5

他夏了夏天 2024-12-19 00:40:59

我没有看到问题,只需将 o 声明为基类 Operand 即可。

Operand o = stack.Pop(); // add as Operand if needed

稍后,如果您需要知道 o 的类型(在基类上使用虚拟/抽象方法应该避免这种情况),则使用:

if (o is NormalOperand)
{
    // TODO: maybe check for null
    (o as NormalOperand).NormalSpecificMethod();
}

I don't see the issue, just declare o as the base class Operand.

Operand o = stack.Pop(); // add as Operand if needed

Later if you need to know the type of o (using virtual/abstract methods on base class should avoid this) then use:

if (o is NormalOperand)
{
    // TODO: maybe check for null
    (o as NormalOperand).NormalSpecificMethod();
}
阳光下慵懒的猫 2024-12-19 00:40:59

据我了解,您想要将此类变量声明为外部的唯一原因是因为您想对它们调用不同的方法。

例如:

 normal = true;

 ...

 o.DoSomethingNormal()

 normal = false;
 o.DoSomethingSpecial();

如果您查看此代码,并意识到 C# 是一种静态类型语言,您就会明白它永远无法编译。

但是,如果您想调用在 Normal 和 Special 上声明的方法,您应该在 Interface 或基类中声明此方法,并将 stack.Pop() 强制转换为该类型。这样你就可以使用多态性并且它会起作用。

在你的情况下:

Operand o = stack.Pop() as Operand;
o.DoSomething();

As I understand the only reason you want to declare such a varaible as external is because you want to call different methods on them.

For example:

 normal = true;

 ...

 o.DoSomethingNormal()

 normal = false;
 o.DoSomethingSpecial();

If you have a look at this code, and realize that C# is a statically typed language you will understand that this can never compile.

But if you want to call a method that is declared on both Normal and Special you could should declare this method in an Interface or base class and cast the stack.Pop() to that type. That way you will use polyformism and it will work.

In your case:

Operand o = stack.Pop() as Operand;
o.DoSomething();
橘寄 2024-12-19 00:40:59

通常,您应该在 if 语句之前声明变量,将其声明为操作数。在这种情况下,您根本不需要 if 语句,也不需要布尔正常。只需将其声明为操作数并使用多态性来执行您需要执行的任何其他操作。

Operand o = stack.Pop() as Operand;

正如你所说,你不想处理下面的差异,你不需要知道 o 是否特殊。或者你呢?

Usually, you should be declaring the variable before the if statement, declaring it as an Operand. In this situation, you don't need an if statement at all, nor do you need the boolean normal. Just declare it as an Operand and use polymorphism to do whatever else it is you need to do.

Operand o = stack.Pop() as Operand;

As you say, you don't want to deal with the differences below, you shouldn't need to know whether o is special or not. Or do you?

柏林苍穹下 2024-12-19 00:40:59

对于初学者来说,您的示例没有任何意义,因为这两个临时对象无论如何都会消失。但让我们忽略这一点。

鉴于这些操作数位于同一个堆栈上,我们可以假设它们都派生自一个公共类。我假设这是Operand

Operand o = Stack.Pop();

现在,我还假设您想用这段代码一些事情。它是什么?为什么你必须知道?如果您真的只是想知道它是什么类型,您可以说:

if(o.GetType() == typeof(NormalOperand))
{
}
else if(o.GetType() == typeof(SpecialOpernad))
{
}

For starters, your example doesn't make any sense as both those temporaries disappear anyway. But let's ignore that little bit.

Given that these operands are on the same stack, we can assume they are both derived from a common class. I'll assume that is Operand.

Operand o = Stack.Pop();

Now, I also assume you want to do something with this code. What is it? Why do you have to know? If you really just want to know what type it is, you can say:

if(o.GetType() == typeof(NormalOperand))
{
}
else if(o.GetType() == typeof(SpecialOpernad))
{
}
执手闯天涯 2024-12-19 00:40:59

你应该能够做到:

bool normal = ...

Operand o = null;
if (normal) o = stack.Pop() as NormalOperand; Enter code here`else o = stack.Pop() as SpecialOperand;

但我不确定我是否理解它。我的意思是,只有当您需要在 if 语句中执行其他操作时,这才有意义。否则,正如 George 建议的那样,您始终可以执行 o = stack.Pop() ,然后使用 as 和 null 检查来检查类型。

You should be able to do:

bool normal = ...

Operand o = null;
if (normal) o = stack.Pop() as NormalOperand; Enter code here`else o = stack.Pop() as SpecialOperand;

I am not sure I understand it though. I mean, this makes sense only if you need to do other stuff within the if-statement. otherwise, as George suggests, you can always do o = stack.Pop() and then check type using as and null checking.

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