久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 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 的方法調用的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Why shouldn#39;t I always use nullable types in C#(為什么我不應該總是在 C# 中使用可空類型)
C# HasValue vs !=null(C# HasValue vs !=null)
C# ADO.NET: nulls and DbNull -- is there more efficient syntax?(C# ADO.NET:空值和 DbNull —— 有沒有更高效的語法?)
How to set null value to int in c#?(如何在c#中將空值設置為int?)
How to handle nulls in LINQ when using Min or Max?(使用 Min 或 Max 時如何處理 LINQ 中的空值?)
GetType on Nullable Boolean(可空布爾值上的 GetType)
主站蜘蛛池模板: 国产成人免费视频网站视频社区 | 免费视频一区二区 | 午夜欧美a级理论片915影院 | 一区二区三区四区在线视频 | 99精品99| 国产精品亚洲一区二区三区在线观看 | 久久最新 | 黄色一级大片在线观看 | 91精品国产综合久久久动漫日韩 | 97caoporn国产免费人人 | 久久人人爽人人爽人人片av免费 | 国产精品自拍av | 久久久久久99 | 国产九九九| 精品无码久久久久久久动漫 | 成人精品一区亚洲午夜久久久 | 99国产精品99久久久久久 | 91xx在线观看 | 蜜桃视频在线观看免费视频网站www | 日韩中文在线 | 亚洲高清网 | 中文字幕国产精品 | 成人精品鲁一区一区二区 | wwwww在线观看 | 亚洲精品一区在线观看 | 久久久久久亚洲精品 | 亚洲国产日韩欧美 | 国产成人免费网站 | 二区三区在线观看 | 日韩在线视频一区二区三区 | 亚洲视频免费观看 | 久久久久黑人 | 国产精品污www在线观看 | 男女啪啪高潮无遮挡免费动态 | 在线成人一区 | 亚洲日韩中文字幕一区 | 久草新在线 | 久久免费精品 | 无码日韩精品一区二区免费 | 日韩精品成人 | 亚洲美女天堂网 |