在 C# 中如果不为 null 的方法调用

Method call if not null in C#(在 C# 中如果不为 null 的方法调用)
本文介绍了在 C# 中如果不为 null 的方法调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

是否可以以某种方式缩短此声明?

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 的方法调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

ActiveDirectory error 0x8000500c when traversing properties(遍历属性时 ActiveDirectory 错误 0x8000500c)
search by samaccountname with wildcards(使用通配符按 samaccountname 搜索)
Get the list of Groups for the given UserPrincipal(获取给定 UserPrincipal 的组列表)
Can you find an Active Directory User#39;s Primary Group in C#?(你能在 C# 中找到 Active Directory 用户的主要组吗?)
Query From LDAP for User Groups(从 LDAP 查询用户组)
How can I get DOMAINUSER from an AD DirectoryEntry?(如何从 AD DirectoryEntry 获取 DOMAINUSER?)