這是:
if(x != y)
{
}
與此不同:
if (x is not y)
{
}
或者這兩個條件之間沒有區別嗎?
uj5u.com熱心網友回復:
| 操作員 | != |
is not |
|---|---|---|
| 初衷 | 價值不平等 | 否定模式匹配 |
| 可以執行價值不等式 | 是的 | 是的 |
| 可以執行否定模式匹配 | 不 | 是的 |
可以implicit在左側運算元上呼叫運算子 |
是的 | 不 |
可以implicit在右側運算元上呼叫運算子 |
是的 | 是1 |
| 是自己的運營商 | 是的 | 沒有2 |
| 可多載 | 是的 | 不 |
| 自從 | C# 1.0 | C# 9.0 3 |
省音的null對值型通用型引數引數比較4 |
是的 | 否[需要參考] 5 |
| 不可能的比較 | 錯誤 | 警告 |
| 左運算元 | 任何表情 | 任何表情 |
| 右運算元 | 任何表情 | 僅常量運算式6 |
| 句法 | <any-expr> != <any-expr> |
<any-expr> is [not] <const-expr> [or|and <const-expr>]* 和更多 |
| 例子 | != |
is not |
|---|---|---|
| 不為空 | x != null |
x is not null |
| 價值不平等的例子 | x != 'a' |
x is not 'a' |
| 運行時型別(錯誤)匹配 | x.GetType() != typeof(Char) |
x is not Char7 |
查詢陳述句 x NOT IN ( 1, 2, 3 ) |
x != 1 && x != 2 && x != 3 |
x is not 1 or 2 or 3 |
要直接和具體地回答 OP 的問題:
if( x != y ) { }
// vs:
if( x is not y ) { }
如果
x是整數值型別(例如int/Int32)并且y是const-expression(例如const int y = 123;) 那么no,沒有區別,并且兩個陳述句都會生成相同的 .NET MSIL 位元組碼(啟用和不啟用編譯器優化):
如果
y是一個型別的名稱(而不是值名稱),那么就是一個區別:第一個if宣告是無效的,并不會進行編譯,并且if( x is not y )宣告是一個型別的模式匹配,而不是一個恒定的模式匹配。
腳注:
"Constant Pattern" : "當輸入值不是開放型別時,常量運算式被隱式轉換為匹配運算式的型別"。
x is not nullis more analogous to!(x == null)thanx != null.C# 7.0 introduced some limited forms of constant-pattern matching, which was further expanded by C# 8.0, but it wasn't until C# 9.0 that the
notnegation operator (or is it a modifier?) was added.Given a non-constrained generic method, like so:
void Foo<T>( T x ) { if( x == null ) { DoSomething(); } DoSomethingElse(); }...when the JIT instantiates the above generic method (i.e.: monomorphization) when
Tis a value-type (struct) then the entireif( x == null ) { DoSomething(); }statement (and its block contents) will be removed by the JIT compiler ("elision"), this is because a value-tupe can never be equal tonull. While you'd expect that to be handled by any optimizing compiler, I understand that the .NET JIT has specially hardcoded rules for that particular scenario.- Curiously in earlier versions of C# (e.g. 7.0) the elision rule only applied to the
==and!=operators, but not theisoperator, so whileif( x == null ) { DoSomething(); }would be elided, the statementif( x is null ) { DoSometing(); }would not, and in fact you would get a compiler error unlessTwas constrained towhere T : class. Since C# 8.0 this seems to now be allowed for unconstrained generic types.
- Curiously in earlier versions of C# (e.g. 7.0) the elision rule only applied to the
Surprisingly I couldn't find an authoritative source on this (as the published C# specs are now significantly outdated; and I don't want to go through the
cscsource-code to find out either).- If neither the C# compiler or JIT do apply impossible-branch-elision in generic code with Constant-pattern expressions then I think it might simply because it's too hard to do at-present.
Note that a constant-expression does not mean a literal-expression: you can use named
constvalues,enummembers, and so on, even non-trivial raw expressions provided all sub-expressions are also constant-expressions.- I'm curious if there's any cases where
static readonlyfields could be used though.
- I'm curious if there's any cases where
Note that in the case of
typeof(X) != y.GetType(), this expression will returntruewhenXis derived fromy's type (as they are different types), butx is not Yis actuallyfalsebecausexisY(becausexis an instance of a subclass ofY). When usingTypeit's better to do something liketypeof(X).IsSubclassOf(y.GetType()), or the even loosery.GetType().IsAssignableFrom(typeof(X)).- Though in this case, as
Charis a struct and so cannot participate in a type-hierarchy, so doing!x.IsSubclassOf(typeof(Char))would just be silly.
- Though in this case, as
uj5u.com熱心網友回復:
在優異的接受的答案列出的額外的不同之處在于(因為C#7.0),is2個NaN值之間是一個圖案匹配,因為x.Equals(y)是true當兩個x和y為NaN,和NaN值不具有整數型別。因此,is not在兩個 NaN 值之間回傳模式不匹配。
然而,C#遵循IEEE浮點和C通過指定一個!=2個NaN值之間的比較是true與一個==在它們之間的比較是false。這主要是因為早在 1980 年的 Intel 8087 浮點協處理器就沒有其他方法來測驗 NaN。
uj5u.com熱心網友回復:
Nan 和 null 是變數可以包含的沒有值的屬性。相等性檢查需要一個實際值來確定相等性。畢竟,在沒人知道莎莉和彼得有多少個蘋果的情況下,關于他們是否擁有相同數量的蘋果的問題毫無意義。
有時你想檢查一個變數是否有一個沒有值的屬性。為此,基本的平等檢查是不夠的。那是什么時候 is / is not 運算子很有用。可以說 != 是一個值檢查,其中 is / 不是一個屬性檢查。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/362782.html
