为什么C#中不支持以下语法? [英] Why is following syntax not supported in C#?

查看:118
本文介绍了为什么C#中不支持以下语法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

IIRC在C ++中我们可以在if语句中声明变量。我们为什么不能这样做?/#
在C#中?

让我们看看这个例子:


object obj = GetSomeObject() ;


if((Customer cust = obj as Customer)!= null)

{

cust.DoStuff();

}

else if((供应商支持= obj作为供应商)!= null)

{

supp .DoSomeOtherStuff();

}


我现在可以更好地解决这个具体的例子,但这只是一个

的例子让我知道我的意思。


问题是,目前我必须这样写:


客户服务;

供应商支持;


if(cust = obj as Customer)!= null)

{

cust.DoStuff();

}

else if((supp = obj as Supplier)!= null)

{

supp.DoSomeOtherStuff();

}


这会不必要地将两个变量的可见性扩展到外部

范围。

什么你是否考虑过这个想法?

IIRC in C++ we can declare variables in if statements. Why can''t we do that
in C#?
Lets see that example:

object obj = GetSomeObject();

if ((Customer cust = obj as Customer)!=null)
{
cust.DoStuff();
}
else if ((Supplier supp = obj as Supplier )!=null)
{
supp.DoSomeOtherStuff();
}

I now that that specific example could be solved better but this is just an
example to give you the idea what I mean.

The problem is that at the moment I have to write it like that:

Customer cust;
Supplier supp;

if (cust = obj as Customer)!=null)
{
cust.DoStuff();
}
else if ((supp = obj as Supplier )!=null)
{
supp.DoSomeOtherStuff();
}

this unnecessarily expands the visibility of both variables to the outer
scope.
What do you think about that idea?

推荐答案

尝试(客户)obj将你的obj投射到客户类型。 As表示As。关键字是

VB。这样的事情(未经测试,但它可能会指向你一般的

方向):


if((Customer cust =(Customer)obj)!= null)

{

cust.DoStuff();

}

else if((供应商支持=(供应商)obj)!= null)

{

supp.DoSomeOtherStuff();

}


" cody" <德******** @ gmx.de>写在消息中

新闻:ef ************** @ TK2MSFTNGP10.phx.gbl ...
Try (Customer)obj to cast your obj to Customer type. The "As" keyword is
VB. Something like this (untested, but it might point you in the general
direction):

if ((Customer cust = (Customer)obj)!=null)
{
cust.DoStuff();
}
else if ((Supplier supp = (Supplier)obj)!=null)
{
supp.DoSomeOtherStuff();
}

"cody" <de********@gmx.de> wrote in message
news:ef**************@TK2MSFTNGP10.phx.gbl...
IIRC in C ++我们可以在if语句中声明变量。我们为什么不能在C#中使用

让我们看一下这个例子:

对象obj = GetSomeObject();
if((Customer cust = obj as Customer)!= null)
{
cust.DoStuff();
}
if if((供应商supp = obj as Supplier) != null)
{
supp.DoSomeOtherStuff();
}
我现在可以更好地解决这个具体的例子,但这只是
一个例子让你明白我的意思。

问题是,目前我必须这样写:

客户委托;
供应商支持;

if(cust = obj as Customer)!= null)
{
cust.DoStuff();
}
否则if((supp = obj as Supplier)!= null)

supp.sDoSomeOtherStuff();
}
这会不必要地扩大两个变量的可见性外部
范围。
你对这个想法怎么看?
IIRC in C++ we can declare variables in if statements. Why can''t we do
that
in C#?
Lets see that example:

object obj = GetSomeObject();

if ((Customer cust = obj as Customer)!=null)
{
cust.DoStuff();
}
else if ((Supplier supp = obj as Supplier )!=null)
{
supp.DoSomeOtherStuff();
}

I now that that specific example could be solved better but this is just
an
example to give you the idea what I mean.

The problem is that at the moment I have to write it like that:

Customer cust;
Supplier supp;

if (cust = obj as Customer)!=null)
{
cust.DoStuff();
}
else if ((supp = obj as Supplier )!=null)
{
supp.DoSomeOtherStuff();
}

this unnecessarily expands the visibility of both variables to the outer
scope.
What do you think about that idea?



你好Cody,

你可以使用''是''关键字


object obj = GetSomeObject();


if(obj is Customer)

{

((客户)obj).DoStuff();

}

else if(obj是供应商)

{

((供应商)obj).DoSomeOtherStuff();

}


它不会''但就像你的C ++代码一样。



2005年5月26日星期四15:09:14 +0200,cody< de ******* *@gmx.de>写道:
Hi Cody,

You can use the ''is'' keyword

object obj = GetSomeObject();

if(obj is Customer)
{
((Customer)obj).DoStuff();
}
else if(obj is Supplier)
{
((Supplier)obj).DoSomeOtherStuff();
}

It wouldn''t be exactly as your C++ code though.


On Thu, 26 May 2005 15:09:14 +0200, cody <de********@gmx.de> wrote:
IIRC在C ++中我们可以在if语句中声明变量。为什么我们不能在C#中这样做?
让我们看看那个例子:

对象obj = GetSomeObject();

if((客户cust = obj as Customer)!= null)
{
cust.DoStuff();
}
if if((供应商supp = obj as Supplier)!= null)
{
supp.DoSomeOtherStuff();
}
我现在可以更好地解决这个具体的例子,但这只是一个给出的例子你的想法是什么意思。

问题是,目前我必须这样写:

客户委托;
供应商支持;

if(cust = obj as Customer)!= null)
{
cust.DoStuff();
}
if if((supp = obj as供应商)!= null)
这一点不必要地将两个变量的可见性扩展到外部范围。
你怎么看待这个想法?
IIRC in C++ we can declare variables in if statements. Why can''t we do that
in C#?
Lets see that example:

object obj = GetSomeObject();

if ((Customer cust = obj as Customer)!=null)
{
cust.DoStuff();
}
else if ((Supplier supp = obj as Supplier )!=null)
{
supp.DoSomeOtherStuff();
}

I now that that specific example could be solved better but this is just an
example to give you the idea what I mean.

The problem is that at the moment I have to write it like that:

Customer cust;
Supplier supp;

if (cust = obj as Customer)!=null)
{
cust.DoStuff();
}
else if ((supp = obj as Supplier )!=null)
{
supp.DoSomeOtherStuff();
}

this unnecessarily expands the visibility of both variables to the outer
scope.
What do you think about that idea?




-

快乐的编码!

Morten Wennevik [C# MVP]



--
Happy coding!
Morten Wennevik [C# MVP]


Fist,cast抛出异常而不是我的代码的意图,第二个

在C#中支持as关键字* * 。它没有扔,而是返回

null。

Michael C# <豪*** @ boutdat.com> schrieb im Newsbeitrag

新闻:#D ************** @ TK2MSFTNGP12.phx.gbl ...
Fist, casting throws an exception which is not the intent of my code, second
the as keyword *is* supported in C#. It doesn''t throw but instead returns
null.
"Michael C#" <ho***@boutdat.com> schrieb im Newsbeitrag
news:#D**************@TK2MSFTNGP12.phx.gbl...
试试(客户) obj将您的obj转换为Customer类型。 As表示As。关键字是
VB。这样的事情(未经测试,但它可能指向你一般的方向):

if((Customer cust =(Customer)obj)!= null)
{
cust.DoStuff();
}
否则if((供应商支持=(供应商)obj)!= null)
{
supp.DoSomeOtherStuff();
}

cody <德******** @ gmx.de>在消息中写道
新闻:ef ************** @ TK2MSFTNGP10.phx.gbl ...
Try (Customer)obj to cast your obj to Customer type. The "As" keyword is
VB. Something like this (untested, but it might point you in the general
direction):

if ((Customer cust = (Customer)obj)!=null)
{
cust.DoStuff();
}
else if ((Supplier supp = (Supplier)obj)!=null)
{
supp.DoSomeOtherStuff();
}

"cody" <de********@gmx.de> wrote in message
news:ef**************@TK2MSFTNGP10.phx.gbl...
IIRC在C ++中我们可以在if中声明变量声明。我们为什么不能在C#中使用

让我们看一下这个例子:

对象obj = GetSomeObject();
if((Customer cust = obj as Customer)!= null)
{
cust.DoStuff();
}
if if((供应商supp = obj as Supplier) != null)
{
supp.DoSomeOtherStuff();
}
我现在可以更好地解决这个具体的例子,但这只是
一个例子让你明白我的意思。

问题是,目前我必须这样写:

客户委托;
供应商支持;

if(cust = obj as Customer)!= null)
{
cust.DoStuff();
}
否则if((supp = obj as Supplier)!= null)

supp.sDoSomeOtherStuff();
}
这会不必要地扩大两个变量的可见性外部
范围。
您如何看待这个想法?
IIRC in C++ we can declare variables in if statements. Why can''t we do
that
in C#?
Lets see that example:

object obj = GetSomeObject();

if ((Customer cust = obj as Customer)!=null)
{
cust.DoStuff();
}
else if ((Supplier supp = obj as Supplier )!=null)
{
supp.DoSomeOtherStuff();
}

I now that that specific example could be solved better but this is just
an
example to give you the idea what I mean.

The problem is that at the moment I have to write it like that:

Customer cust;
Supplier supp;

if (cust = obj as Customer)!=null)
{
cust.DoStuff();
}
else if ((supp = obj as Supplier )!=null)
{
supp.DoSomeOtherStuff();
}

this unnecessarily expands the visibility of both variables to the outer
scope.
What do you think about that idea?




这篇关于为什么C#中不支持以下语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆