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

可空布爾值上的 GetType

GetType on Nullable Boolean(可空布爾值上的 GetType)
本文介紹了可空布爾值上的 GetType的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

當我在 Microsoft MSDN 上發現這篇文章時,我正在研究可空布爾值

I was looking into nullable bools when I found this article on Microsoft MSDN

如何:識別可空類型(C# 編程指南)

您可以使用 C# typeof 運算符來創建表示 Nullable 類型的 Type 對象.

You can use the C# typeof operator to create a Type object that represents a Nullable type.

所以我嘗試使用可為空的布爾值進行檢查:

So I tried checking with a nullable bool:

Console.Write(typeof(bool?)); //System.Nullable`1[System.Boolean]

MSDN上的文章說

您還可以使用 System.Reflection 命名空間的類和方法來生成表示 Nullable 類型的 Type 對象.但是,如果您嘗試在運行時使用 GetType 方法或 is 運算符從 Nullable 變量獲取類型信息,則結果是表示底層類型的 Type 對象,而不是 Nullable 類型本身.

You can also use the classes and methods of the System.Reflection namespace to generate Type objects that represent Nullable types. However, if you try to obtain type information from Nullable variables at runtime by using the GetType method or the is operator, the result is a Type object that represents the underlying type, not the Nullable type itself.

在 Nullable 類型上調用 GetType 會導致在類型隱式轉換為 Object 時執行裝箱操作.因此 GetType 總是返回一個代表底層類型的 Type 對象,而不是 Nullable 類型.

Calling GetType on a Nullable type causes a boxing operation to be performed when the type is implicitly converted to Object. Therefore GetType always returns a Type object that represents the underlying type, not the Nullable type.

如果這是真的,我希望從 .GetType() 獲得相同的結果,無論我使用可空 bool 還是常規 bool.但事實并非如此:

If this is true I expect to get the same result from .GetType() whether I use a nullable bool or a regular bool. But this is not what happens:

    bool a = new bool();
    Console.Write(a.GetType()); //Prints System.Boolean

    bool? b = new bool?();
    Console.Write(b.GetType()); //Exception!

發生的異常:

BoolTest.exe 中發生類型為System.NullReferenceException"的未處理異常

An unhandled exception of type 'System.NullReferenceException' occurred in BoolTest.exe

附加信息:未將對象引用設置為對象的實例.

Additional information: Object reference not set to an instance of an object.

但是對象引用被設置為一個對象的實例.導致此錯誤的原因可能是什么?

But the object reference is set to an instance of an object. What could be the cause of this error?

推薦答案

您正在對 NULL 引用調用 GetType(使用 No值).

You're calling GetType on a NULL Reference (The result of boxing a Nullable Type with No Value).

布爾值?b = new bool?(); 等價于 bool?b = null;

試試這個以獲得正確的結果:

Try this to get the correct result:

bool? b = new bool?(false);
Console.Write(b.GetType()); // System.Boolean

文檔意味著,如果您在具有值(非空)的 Nullable 對象上成功調用 GetType().你得到的底層類型是 System.Boolean.但是您不能使用 NULL 引用調用任何方法,這是適用于任何引用類型的一般規則.

The documentation means that if you call GetType() successfully on a Nullable object that has value (Not Null). You get the Underlying type which is System.Boolean. But you can't call any method using a NULL reference and this is a general rule that applying to any reference type.

要清除 = nullnew bool?() 之間的等價點,請檢查此 小提琴.兩者都生成相同的 IL:

To clear the equivalence point between = null and new bool?(), check this Fiddle. Both generates the same IL:

IL_0001:  ldloca.s   V_0
IL_0003:  initobj    valuetype [mscorlib]System.Nullable`1<bool>

IL_0009:  ldloca.s   V_1
IL_000b:  initobj    valuetype [mscorlib]System.Nullable`1<bool>

這篇關于可空布爾值上的 GetType的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 中的空值?)
Method call if not null in C#(在 C# 中如果不為 null 的方法調用)
主站蜘蛛池模板: 麻豆亚洲 | 热久久久久 | 美女操网站| 精品自拍视频 | 亚洲国产aⅴ成人精品无吗 亚洲精品久久久一区二区三区 | 超碰导航 | 日韩精品久久一区二区三区 | 一区二区在线观看av | 国产高清一二三区 | 黄视频免费观看 | 久久大陆| 欧美日韩一区在线播放 | 亚洲欧美日韩成人在线 | 亚洲综合色视频在线观看 | 韩国精品在线 | 国产精品一区二区av | 蜜臀网站 | 精品亚洲一区二区 | www.9191.com| 色婷婷婷婷色 | 自拍视频精品 | a国产视频 | 6080yy精品一区二区三区 | 国产精品国产三级国产aⅴ中文 | 精品国产乱码一区二区三区a | 欧美色欧美亚洲另类七区 | 欧美日韩精品一区二区三区四区 | 久久精品视频在线免费观看 | 精品国产99 | 久久精品国产清自在天天线 | 久久成人精品视频 | 亚洲精品无 | 黄色亚洲 | 1000部精品久久久久久久久 | 亚洲天堂一区 | 国产一级免费视频 | 91精品国产91久久久久福利 | 精品一区二区三区在线观看 | 日本不卡一区 | 免费国产视频在线观看 | 久热精品视频 |