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

為什么我不應該總是在 C# 中使用可空類型

Why shouldn#39;t I always use nullable types in C#(為什么我不應該總是在 C# 中使用可空類型)
本文介紹了為什么我不應該總是在 C# 中使用可空類型的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

自從 .net 2.0 中引入這個概念以來,我一直在尋找一些好的指導.

I've been searching for some good guidance on this since the concept was introduced in .net 2.0.

為什么我想在 c# 中使用不可為 null 的數據類型?(一個更好的問題是為什么我不默認選擇可空類型,而僅在明確有意義的情況下才使用不可空類型.)

Why would I ever want to use non-nullable data types in c#? (A better question is why wouldn't I choose nullable types by default, and only use non-nullable types when that explicitly makes sense.)

選擇可空數據類型而不是不可空數據類型是否會對性能產生重大"影響?

Is there a 'significant' performance hit to choosing a nullable data type over its non-nullable peer?

我更喜歡根據 null 而不是 Guid.empty、string.empty、DateTime.MinValue、<= 0 等檢查我的值,并且通常使用可空類型.我不經常選擇可空類型的唯一原因是我后腦勺發癢的感覺,這讓我覺得不僅僅是向后兼容性迫使額外的?"顯式允許空值的字符.

I much prefer to check my values against null instead of Guid.empty, string.empty, DateTime.MinValue,<= 0, etc, and to work with nullable types in general. And the only reason I don't choose nullable types more often is the itchy feeling in the back of my head that makes me feel like it's more than backwards compatibility that forces that extra '?' character to explicitly allow a null value.

是否有人總是(大多數情況下)選擇可空類型而不是不可空類型?

Is there anybody out there that always (most always) chooses nullable types rather than non-nullable types?

感謝您的時間,

推薦答案

你不應該總是使用可空類型的原因是有時你能夠保證一個值被初始化.并且您應該嘗試設計您的代碼,以便盡可能經常出現這種情況.如果一個值不可能被未初始化,那么 null 就沒有理由成為它的合法值.作為一個非常簡單的例子,考慮一下:

The reason why you shouldn't always use nullable types is that sometimes you're able to guarantee that a value will be initialized. And you should try to design your code so that this is the case as often as possible. If there is no way a value can possibly be uninitialized, then there is no reason why null should be a legal value for it. As a very simple example, consider this:

List<int> list = new List<int>()
int c = list.Count;

始終有效.c 不可能被初始化.如果它變成了一個 int?,你實際上是在告訴代碼的讀者這個值可能為空.確保在使用之前檢查它".但是我們知道這永遠不會發生,那么為什么不在代碼中公開這個保證呢?

This is always valid. There is no possible way in which c could be uninitialized. If it was turned into an int?, you would effectively be telling readers of the code "this value might be null. Make sure to check before you use it". But we know that this can never happen, so why not expose this guarantee in the code?

在值是可選的情況下,您是絕對正確的.如果我們有一個可能返回也可能不返回字符串的函數,則返回 null.不要返回 string.Empty().不要返回魔法值".

You are absolutely right in cases where a value is optional. If we have a function that may or may not return a string, then return null. Don't return string.Empty(). Don't return "magic values".

但并非所有值都是可選的.將所有內容都設為可選會使您的其余代碼變得更加復雜(它添加了另一個必須處理的代碼路徑).

But not all values are optional. And making everything optional makes the rest of your code far more complicated (it adds another code path that has to be handled).

如果你能特別保證這個值永遠有效,那為什么要扔掉這些信息呢?這就是您通過使其成為可空類型來做的事情.現在該值可能存在也可能不存在,任何使用該值的人都必須處理這兩種情況.但是您知道,首先這些情況中只有一種是可能的.所以請幫助您的代碼的用戶,并在您的代碼中反映這一事實.然后,您的代碼的任何用戶都可以依賴該值是否有效,而且他們只需處理一種情況而不是兩種情況.

If you can specifically guarantee that this value will always be valid, then why throw away this information? That's what you do by making it a nullable type. Now the value may or may not exist, and anyone using the value will have to handle both cases. But you know that only one of these cases is possible in the first place. So do users of your code a favor, and reflect this fact in your code. Any users of your code can then rely on the value being valid, and they only have to handle a single case rather than two.

這篇關于為什么我不應該總是在 C# 中使用可空類型的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

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 中的空值?)
Method call if not null in C#(在 C# 中如果不為 null 的方法調用)
GetType on Nullable Boolean(可空布爾值上的 GetType)
主站蜘蛛池模板: 国产亚洲精品美女久久久久久久久久 | 国产免费麻豆视频 | 日本精品一区二区 | 一区二区三区在线免费观看视频 | 国产亚洲精品精品国产亚洲综合 | 欧洲一区二区视频 | 婷婷综合网| 91在线观看| 国产人成在线观看 | 日韩有码一区 | 亚洲一区二区黄 | 国产一区二区三区四区五区加勒比 | 亚洲综合在线视频 | 欧美日韩亚洲国产综合 | 国产伦精品一区二区三区精品视频 | 日韩成人精品 | 国产精品99久久久久久久久 | 96国产精品久久久久aⅴ四区 | 国产精品免费av | 欧美午夜一区 | 亚洲欧洲日本国产 | 色综合成人网 | 欧美日韩在线观看视频网站 | 操操日| www.xxxx欧美 | 91精品国产综合久久久久久丝袜 | 北条麻妃一区二区三区在线视频 | 九九在线视频 | 精品毛片在线观看 | 午夜激情影院 | 欧美激情久久久 | 在线观看av中文字幕 | 欧美成人一区二区三区片免费 | 在线一区 | 一区二区三区在线观看视频 | av高清| 午夜a级理论片915影院 | av影音| 中文字幕一区二区三区精彩视频 | 亚洲+变态+欧美+另类+精品 | 一区二区三区四区国产 |