为什么我不能在 C# 中声明外部变量?
我需要在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我没有看到问题,只需将
o
声明为基类Operand
即可。稍后,如果您需要知道
o
的类型(在基类上使用虚拟/抽象方法应该避免这种情况),则使用:I don't see the issue, just declare
o
as the base classOperand
.Later if you need to know the type of
o
(using virtual/abstract methods on base class should avoid this) then use:据我了解,您想要将此类变量声明为外部的唯一原因是因为您想对它们调用不同的方法。
例如:
如果您查看此代码,并意识到 C# 是一种静态类型语言,您就会明白它永远无法编译。
但是,如果您想调用在 Normal 和 Special 上声明的方法,您应该在 Interface 或基类中声明此方法,并将 stack.Pop() 强制转换为该类型。这样你就可以使用多态性并且它会起作用。
在你的情况下:
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:
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:
通常,您应该在 if 语句之前声明变量,将其声明为
操作数
。在这种情况下,您根本不需要 if 语句,也不需要布尔正常
。只需将其声明为操作数并使用多态性来执行您需要执行的任何其他操作。正如你所说,你不想处理下面的差异,你不需要知道 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 theboolean normal
. Just declare it as anOperand
and use polymorphism to do whatever else it is you need to do.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?
对于初学者来说,您的示例没有任何意义,因为这两个临时对象无论如何都会消失。但让我们忽略这一点。
鉴于这些操作数位于同一个堆栈上,我们可以假设它们都派生自一个公共类。我假设这是
Operand
。现在,我还假设您想用这段代码做一些事情。它是什么?为什么你必须知道?如果您真的只是想知道它是什么类型,您可以说:
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
.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 语句中执行其他操作时,这才有意义。否则,正如 George 建议的那样,您始终可以执行 o = stack.Pop() ,然后使用 as 和 null 检查来检查类型。
You should be able to do:
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 doo = stack.Pop()
and then check type using as and null checking.