3

是否可以一起用任何方式操作??,並在接下來的情況下,運營商&&空 - 結合運營商和運營商&&

bool? Any 
{ 
    get 
    { 
     var any = this.ViewState["any"] as bool?; 
     return any.HasValue ? any.Value && this.SomeBool : any; 
    } 
} 

這意味着下一個:

  • 如果any爲null那麼this.Any.HasValue返回false
  • 如果any有值,那麼它返回值考慮另一個布爾屬性,即Any && SomeBool
+0

你爲什麼會想用這個?運營商?爲了縮短它? – 2010-04-27 22:31:27

+0

@亨克霍爾特曼:是的。併爲操作員使用練習。 – abatishchev 2010-04-27 22:33:24

回答

4

我不知道爲什麼沒有人說得清這至今:

bool? any = this.ViewState["any"] as bool?; 
return any & this.SomeBool; 

這將返回

  • null如果any爲空,不管是什麼的this.SomeBool值;
  • true如果anythis.SomeBool都爲真;和
  • false如果any不爲空,並且this.SomeBool爲假。
+0

代碼看起來不錯,儘管描述是錯誤的(你怎麼能同時返回'null'和'false',然而這就是你的規則會發生的'any = = null'和'this.SomeBool == false'。 – 2010-04-28 00:32:14

+0

@Henk Holterman:爲什麼操作數是''bool'和'bool?''而不是''bool?'和'bool'!?左邊的操作數是顯然是'bool?'類型,因爲我聲明瞭這樣的結果。 – dtb 2010-04-28 10:16:36

+1

@亨克Holterman:是的,但是我在將它複製到我的答案時犯了一個錯字:-(現在修正了。 bool y = true; bool?z = x & y;' – dtb 2010-04-28 11:18:24

3

這是你的意思嗎?

bool? Any 
{ 
    get 
    { 
     return ((this.ViewState["any"] as bool?) ?? false) && this.SomeBool; 
    } 
} 

我已將返回值保留爲bool?但它看起來可以改變爲布爾值。

這個測試是這樣的:

class Program 
{ 
    private static readonly Dictionary<string, object> ViewState = new Dictionary<string, object>(); 
    private static bool SomeBool; 

    static void Main(string[] args) 
    { 
     ViewState["any"] = (bool?)null; SomeBool = true; Console.WriteLine(Any); 
     ViewState["any"] = (bool?)false; SomeBool = true; Console.WriteLine(Any); 
     ViewState["any"] = (bool?)true; SomeBool = true; Console.WriteLine(Any); 
     ViewState["any"] = (bool?)null; SomeBool = false; Console.WriteLine(Any); 
     ViewState["any"] = (bool?)false; SomeBool = false; Console.WriteLine(Any); 
     ViewState["any"] = (bool?)true; SomeBool = false; Console.WriteLine(Any); 
     Console.ReadLine(); 
    } 

    static bool? Any 
    { 
     get 
     { 
      return ((ViewState["any"] as bool?) ?? false) && SomeBool; 
     } 
    } 
} 

返回

False 
False 
True 
False 
False 
False 

這裏的行爲是不完全一樣的原爲空應該是測試用例1返回,4是相同。但也許這種行爲不是必需的?

+0

不幸的是,沒有。 '結果爲bool?'總是'HasValue = true'。不是嗎? – abatishchev 2010-04-27 22:25:02

+0

你是對的Henk,行爲是不同的 – 2010-04-27 22:31:44

+1

他希望返回null,如果任何爲null並且其他兩個條件爲false。所以,他希望得到TRUE,FALSE和NULL – Armstrongest 2010-04-27 22:34:10

-1

這樣做的行爲你描述:

return (any ?? false) && this.SomeBool

+0

作爲丹尼爾倫肖的回答,你的人也總是有價值 - '真'或'假',從不'空' – abatishchev 2010-04-27 22:27:58

4

既然你想萬一返回null源是null,我不認爲??是要幫你寫這個的任何短或更清晰。

+0

看來你是對的。運算符'??'用於在任何情況下返回一個值。這正是我所不需要的。 – abatishchev 2010-04-27 22:35:39

3

我認爲你正在試圖做的是這樣的:

return any ?? (any.Value && this.SomeBool) ? true : new Nullable<bool>(); 

不過,我覺得在這樣的情況下,它可能更清楚使用if塊:

if (!any.HasValue) 
    return (any.Value && this.SomeBool) ? true : any; 
else 
    return any; 

如果任何是null,那麼你想返回truenull,對吧?

1

事情是,你真的不想使用?? ??運營商。它的意思是讓它容易避免空值,你實際上想保留空值。

3

Null合併操作符不會爲您如何爲您的方法構建邏輯而工作。當然你可以強迫它在那裏,但它會看起來很醜,只是混淆了誰讀它。

我發現原始代碼很難閱讀和理解,所以重構和刪除三元操作符來揭示意圖。

bool? any = this.ViewState["any"] as bool?; 

if (any == null) 
    return null; 

return any.Value && this.SomeBool; 

空合併就是不錯的簡寫,應謹慎使用

Person contact = Mother ?? Father ?? FirstSibling; 

更意圖暴露,並且更易於閱讀+維護比:

Person contact = Mother; 
if (contact == null) 
    contact = Father; 
if (contact == null) 
    contact = FirstSibling;