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

可空布爾值上的 GetType

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

問題描述

當(dāng)我在 Microsoft MSDN 上發(fā)現(xiàn)這篇文章時(shí),我正在研究可空布爾值

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

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

您可以使用 C# typeof 運(yùn)算符來創(chuàng)建表示 Nullable 類型的 Type 對(duì)象.

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

所以我嘗試使用可為空的布爾值進(jìn)行檢查:

So I tried checking with a nullable bool:

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

MSDN上的文章說

您還可以使用 System.Reflection 命名空間的類和方法來生成表示 Nullable 類型的 Type 對(duì)象.但是,如果您嘗試在運(yùn)行時(shí)使用 GetType 方法或 is 運(yùn)算符從 Nullable 變量獲取類型信息,則結(jié)果是表示底層類型的 Type 對(duì)象,而不是 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 類型上調(diào)用 GetType 會(huì)導(dǎo)致在類型隱式轉(zhuǎn)換為 Object 時(shí)執(zhí)行裝箱操作.因此 GetType 總是返回一個(gè)代表底層類型的 Type 對(duì)象,而不是 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() 獲得相同的結(jié)果,無論我使用可空 bool 還是常規(guī) bool.但事實(shí)并非如此:

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!

發(fā)生的異常:

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

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

附加信息:未將對(duì)象引用設(shè)置為對(duì)象的實(shí)例.

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

但是對(duì)象引用被設(shè)置為一個(gè)對(duì)象的實(shí)例.導(dǎo)致此錯(cuò)誤的原因可能是什么?

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

推薦答案

您正在對(duì) NULL 引用調(diào)用 GetType(使用 No值).

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

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

試試這個(gè)以獲得正確的結(jié)果:

Try this to get the correct result:

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

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

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?() 之間的等價(jià)點(diǎn),請(qǐng)檢查此 小提琴.兩者都生成相同的 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>

這篇關(guān)于可空布爾值上的 GetType的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Why shouldn#39;t I always use nullable types in C#(為什么我不應(yīng)該總是在 C# 中使用可空類型)
C# HasValue vs !=null(C# HasValue vs !=null)
C# ADO.NET: nulls and DbNull -- is there more efficient syntax?(C# ADO.NET:空值和 DbNull —— 有沒有更高效的語(yǔ)法?)
How to set null value to int in c#?(如何在c#中將空值設(shè)置為int?)
How to handle nulls in LINQ when using Min or Max?(使用 Min 或 Max 時(shí)如何處理 LINQ 中的空值?)
Method call if not null in C#(在 C# 中如果不為 null 的方法調(diào)用)
主站蜘蛛池模板: 国产一区二区在线看 | 九九精品网 | 国产成人在线视频 | 欧美成人精品一区 | 九九视频免费观看 | 亚洲精品欧美 | 91亚洲精品乱码久久久久久蜜桃 | 免费黄色av| 成人做受黄大片 | 精品黄色 | 一区二区视频在线 | 成人免费黄色大片 | 欧美不卡一区二区三区 | 黄色国产网站 | 97caoporn| 免费在线观看av | 欧美一级做性受免费大片免费 | 性欧美69| 欧美综合视频 | 九九热精品视频 | 大乳女喂男人吃奶 | 国产女人18毛片18精品 | 操女人网站 | 91美女视频 | 欧美日韩在线一区二区三区 | 中文有码在线 | 国产黄色av网站 | 日韩精品三区 | 1级黄色大片 | 欧美精品久| 成人av免费看 | 欧美专区第一页 | 国产一级片网站 | 日韩一级二级 | 日韩欧美精品 | 日产精品久久久一区二区 | 一区二区三区成人 | 亚洲欧美一区二区三区在线 | 亚洲视频一区二区 | 国产精品永久久久久久久久久 | 久久福利视频导航 |