在调用委托之前,将我的null检查代码显示为灰色 [英] Greyed out my codes of null checking before invoking a delegate

查看:153
本文介绍了在调用委托之前,将我的null检查代码显示为灰色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在VS 2015中编写了统一代码,这意味着它在c#4中,并且空条件运算符不可用.

I write unity codes in VS 2015, which means it's in c#4 and the null condition operator is not available.

在调用委托之前,我进行了空检查.但是,代码为灰色.这是否意味着格雷码不像注释一样有效?如果是这样,我应该怎么做才能防止调用空委托?

Before invoking a delegate, I did a null check. However the codes are greyed out. Does it mean the grey codes are not effective just like comments? If so what should I do to prevent invoking a null delegate?

public delegate void PtjAnimCtrlDelegateColiderSetEnabledTo(bool value);
public PjtAnimCtrlDelegateColliderSetEnabledTo colliderSetEnabledToHandler;

public delegate void PjtAnimCtrlDelegate();
public PjtAnimCtrlDelegate explosionAnimationFinishedHandler;
public PjtAnimCtrlDelegate hitTargetHandler;

public void ExplosionAnimationFinished()
{
  //pjtCtrl.ExplosionAnimationFinished();
  explosionAnimationFinishedHandler();
}

public void SetColliderEnabled()
{
  colliderSEtEnabledToHandler(true);
}

public void SetColliderDisabled() 
{
  colliderSetEnabledToHandler(false);
}

public void HitTarget()
{
  // this IF statement is gray in Visual Studio
  if (hitTargetHandler != null)
  { 
    hitTargetHandler();
  }
}

推荐答案

在调用委托之前,我进行了空检查.但是,代码为灰色.这是否意味着格雷码不像注释一样有效?

Before invoking a delegate, I did a null check. However the codes are greyed out. Does it mean the grey codes are not effective just like comments?

这意味着Visual Studio预编译器/设计时编译器检测到很有可能不需要该语句.

It means that the Visual Studio pre-compiler/design-time-compiler detects the statement is most likely not needed.

如果是的话,我应该怎么做才能防止调用空委托?

If so what should I do to prevent invoking a null delegate?

没什么,编译器仍然会在编译时(以及实际上也在运行时)确定是否需要该语句.

Nothing, the compiler will still determine at compile time (and actually at run-time as well) if the statement is needed.

这篇关于在调用委托之前,将我的null检查代码显示为灰色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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