-4

有没有可能是这样的:

var userAggregazione;
if (userAggregazione = YouTube.Actions.IsAccessTokenValid() != null)
{
}

IsAccessTokenValid()返回一个userAggregazione实例或null. 所以,在 if 里面,我想设置var userAggregazione,并检查它是否null存在。

可能吗?

4

3 回答 3

3

不确定我是否理解,但让我尝试:

var userAggregazione = YouTube.Actions.IsAccessTokenValid();
if (userAggregazione == null)
{
   // userAggregazione is null - do something
}
else
{
   // it is not null - do something
}
于 2013-11-12T14:33:37.590 回答
2

如果分配周围有额外的括号,它将起作用,以便在!=比较之前对其进行评估。

MyClass userAggregazione;
if ((userAggregazione = YouTube.Actions.IsAccessTokenValid()) != null)
{}

编辑2:我假设这个例子是简化的,因为通常你应该这样做,为了清楚起见:

//my style preference is also to not use 'var' anyway
//when getting value from a function
//because it's not clear what the type is.
MyClass userAggregazione = YouTube.Actions.IsAccessTokenValid();
if (userAggregazione != null)
{}
于 2013-11-12T14:35:22.820 回答
0

是的,您需要添加一些大括号:

if ((userAggregazione = YouTube.Actions.IsAccessTokenValid()) != null)

只要确保IsAccessTokeValid不抛出任何异常

于 2013-11-12T14:33:29.993 回答