是否可以以某种方式缩短此声明?
Is it possible to somehow shorten this statement?
if (obj != null)
obj.SomeMethod();
因为我碰巧写了很多,这很烦人.我唯一能想到的就是实现Null Object模式,但这不是我每次都能做到的,当然也不是缩短语法的解决方案.
because I happen to write this a lot and it gets pretty annoying. The only thing I can think of is to implement Null Object pattern, but that's not what I can do every time and it's certainly not a solution to shorten syntax.
和事件类似的问题,其中
And similar problem with events, where
public event Func<string> MyEvent;
然后调用
if (MyEvent != null)
MyEvent.Invoke();
从 C# 6 开始,您只需使用:
From C# 6 onwards, you can just use:
MyEvent?.Invoke();
或:
obj?.SomeMethod();
?.
是空值传播操作符,当操作数为null
时会导致.Invoke()
短路代码>.操作数只被访问一次,因此不存在检查和调用之间的值变化"问题的风险.
The ?.
is the null-propagating operator, and will cause the .Invoke()
to be short-circuited when the operand is null
. The operand is only accessed once, so there is no risk of the "value changes between check and invoke" problem.
===
在 C# 6 之前,否:没有空安全魔法,只有一个例外;扩展方法 - 例如:
Prior to C# 6, no: there is no null-safe magic, with one exception; extension methods - for example:
public static void SafeInvoke(this Action action) {
if(action != null) action();
}
现在这是有效的:
Action act = null;
act.SafeInvoke(); // does nothing
act = delegate {Console.WriteLine("hi");}
act.SafeInvoke(); // writes "hi"
在事件的情况下,这还具有消除竞争条件的优点,即您不需要临时变量.所以通常你需要:
In the case of events, this has the advantage of also removing the race-condition, i.e. you don't need a temporary variable. So normally you'd need:
var handler = SomeEvent;
if(handler != null) handler(this, EventArgs.Empty);
但带有:
public static void SafeInvoke(this EventHandler handler, object sender) {
if(handler != null) handler(sender, EventArgs.Empty);
}
我们可以简单地使用:
SomeEvent.SafeInvoke(this); // no race condition, no null risk
这篇关于在 C# 中如果不为 null 的方法调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!