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

在 C# 中檢查對象是否為空

Checking if an object is null in C#(在 C# 中檢查對象是否為空)
本文介紹了在 C# 中檢查對象是否為空的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

如果對象為空,我想阻止對其進(jìn)行進(jìn)一步處理.

在下面的代碼中,我檢查對象是否為空:

if (!data.Equals(null))

if (data != null)

但是,我在 dataList.Add(data) 收到一個(gè) NullReferenceException.如果對象為空,它甚至不應(yīng)該進(jìn)入 if 語句!

因此,我在問這是否是檢查對象是否為空的正確方法:

public List數(shù)據(jù)列表;public bool AddData(ref 對象數(shù)據(jù))布爾成功=假;嘗試{//我也用過 "if (data != null)" 也沒有用如果 (!data.Equals(null)){//此處發(fā)生NullReferenceException ...dataList.Add(data);成功 = doOtherStuff(data);}}捕獲(例外 e){拋出新的異常(e.ToString());}返回成功;}

如果這是檢查對象是否為空的正確方法,我做錯(cuò)了什么(如何防止對對象進(jìn)行進(jìn)一步處理以避免 NullReferenceException)?

解決方案

null 不是data,而是dataList.

您需要?jiǎng)?chuàng)建一個(gè)

public ListdataList = new List();

更好:因?yàn)樗且粋€(gè)字段,所以將其設(shè)為私有.如果沒有什么可以阻止您,請將其設(shè)置為 readonly.只是很好的做法.

旁邊

檢查無效性的正確方法是if(data != null).這種檢查在引用類型中無處不在;甚至 Nullable<T> 覆蓋了相等運(yùn)算符,以便在檢查 null 性時(shí)更方便地表達(dá) nullable.HasValue.

如果你執(zhí)行 if(!data.Equals(null)) 那么你會(huì)得到一個(gè) NullReferenceException 如果 data == null.這有點(diǎn)可笑,因?yàn)楸苊膺@種例外是首要目標(biāo).

你也在這樣做:

catch(異常 e){拋出新的異常(e.ToString());}

這絕對不好.我可以想象你把它放在那里只是為了你可以在方法內(nèi)部時(shí)進(jìn)入調(diào)試器,在這種情況下忽略這一段.否則,不要無緣無故地捕獲異常.如果你這樣做了,只需使用 throw; 重新拋出它們.

I would like to prevent further processing on an object if it is null.

In the following code I check if the object is null by either:

if (!data.Equals(null))

and

if (data != null)

However, I receive a NullReferenceException at dataList.Add(data). If the object was null, it should never have even entered the if-statement!

Thus, I'm asking if this is proper way of checking if an object is null:

public List<Object> dataList;
public  bool AddData(ref Object data)
    bool success = false;
    try
    {
        // I've also used "if (data != null)" which hasn't worked either
        if (!data.Equals(null))
        {
           //NullReferenceException occurs here ...
           dataList.Add(data);
           success = doOtherStuff(data);
        }
    }
    catch (Exception e)
    {
        throw new Exception(e.ToString());
    }
    return success;
}

If this is the proper way of checking if the object is null, what am I doing wrong (how can I prevent further processing on the object to avoid the NullReferenceException)?

解決方案

It's not data that is null, but dataList.

You need to create one with

public List<Object> dataList = new List<Object>();

Even better: since it's a field, make it private. And if there's nothing preventing you, make it also readonly. Just good practice.

Aside

The correct way to check for nullity is if(data != null). This kind of check is ubiquitous for reference types; even Nullable<T> overrides the equality operator to be a more convenient way of expressing nullable.HasValue when checking for nullity.

If you do if(!data.Equals(null)) then you will get a NullReferenceException if data == null. Which is kind of comical since avoiding this exception was the goal in the first place.

You are also doing this:

catch (Exception e)
{
    throw new Exception(e.ToString());
}

This is definitely not good. I can imagine that you put it there just so you can break into the debugger while still inside the method, in which case ignore this paragraph. Otherwise, don't catch exceptions for nothing. And if you do, rethrow them using just throw;.

這篇關(guān)于在 C# 中檢查對象是否為空的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

How to check if String is null(如何檢查字符串是否為空)
Equals(item, null) or item == null(Equals(item, null) 或 item == null)
Overriding == operator. How to compare to null?(覆蓋 == 運(yùn)算符.如何與空值進(jìn)行比較?)
What does the question mark in member access mean in C#?(成員訪問中的問號在 C# 中是什么意思?)
The || (or) Operator in Linq with C#(||(或)C# 中的 Linq 運(yùn)算符)
C# null coalescing operator equivalent for c++(C# 空合并運(yùn)算符等效于 C++)
主站蜘蛛池模板: 亚洲综合色自拍一区 | 欧美激情一区二区三区 | 精品欧美二区 | 欧美日韩国产综合在线 | 欧美国产精品一区二区三区 | 免费天天干| 国产一区二区三区四区在线观看 | 四虎在线视频 | 五月婷婷导航 | 一级免费毛片 | 免费看一区二区三区 | 亚洲成人免费网址 | 福利二区 | 亚洲一区二区国产 | 亚洲欧美综合精品久久成人 | 国产日韩一区二区 | 一本一道久久a久久精品综合蜜臀 | 国产在线精品一区 | 欧美精品中文字幕久久二区 | 蜜桃视频一区二区三区 | 看黄在线 | 国产一区视频在线 | a在线视频 | 日韩av一二三区 | 日韩免费一区二区 | 中文久久| 日韩精品视频在线 | 久久久久久久久综合 | 国产二区在线播放 | 国产精品久久久久久久久久免费看 | 国产精品美女久久久久久久久久久 | 欧美视频二区 | 国产精品毛片在线 | 日一区二区三区 | 天天射夜夜操 | 亚洲网站在线观看 | 91极品视频 | 精品视频一区二区三区在线观看 | 草草视频在线免费观看 | 99精品在线 | 蜜桃视频在线观看免费视频网站www |