Programing

==가 null 값에 대해 true를 반환 할 때> =가 false를 반환하는 이유는 무엇입니까?

lottogame 2020. 10. 12. 07:02
반응형

==가 null 값에 대해 true를 반환 할 때> =가 false를 반환하는 이유는 무엇입니까?


int 유형의 변수가 두 개 있습니까? (또는 원하는 경우 Nullable <int>). 두 변수에 대해 크거나 같음 (> =) 비교를하고 싶었지만 두 변수가 모두 null이면 false를 반환하고 == 연산자는 true를 반환합니다.

> = 연산자의 의미 론적 정의에 "or"라는 단어가 포함되어 있기 때문에 이것이 왜 논리적인지 설명해 줄 수 있습니까?


이 기능이 원래 C # 2.0으로 다시 설계되었을 때이 이상한 점에 대해 큰 논쟁이있었습니다. 문제는 C # 사용자가이 의미에 완전히 익숙하다는 것입니다.

if(someReference == null)

동등성을 nullable 값 유형으로 확장 할 때 다음을 선택할 수 있습니다.

  1. Nullable 평등이 진정으로 해제 됩니다. 피연산자 중 하나 또는 둘 모두가 널이면 결과는 참도 거짓도 아닌 널입니다. 이 경우 다음 중 하나를 수행 할 수 있습니다.

    • a) ifif에 nullable bool이 아닌 bool이 필요 하기 때문에 문 에서 nullable 값 유형이 같음을 갖는 것은 불법으로 만듭니다 . 대신 HasValuenull과 비교 하려면 모든 사람이 사용하도록 요구하십시오 . 이것은 장황하고 자극적입니다.

    • b) 자동으로 null을 false로 변환합니다. 이것의 단점은 x==nullx가 null 인 경우 false 반환 한다는 것입니다 . 이는 혼란스럽고 참조 유형과의 null 비교에 대한 사람들의 이해에 반합니다.

  2. Nullable 같음은 해제되지 않습니다. Nullable 같음은 true 또는 false이고 null과의 비교는 null 검사입니다. 이로 인해 nullable 같음이 nullable 부등식과 일치하지 않게됩니다.

이러한 선택 중 어느 것도 분명히 정확하지 않습니다. 그들은 모두 장단점이 있습니다. 예를 들어 VBScript는 1b를 선택합니다. 많은 논쟁 끝에 C # 디자인 팀은 # 2를 선택했습니다.


Equality는 Comparability와 별도로 정의되기 때문입니다.
테스트 할 수는 x == null있지만 x > null의미가 없습니다. C #에서는 항상 false입니다.


'> ='를 설명하는 또 다른 방법은 Not Less Than입니다. 동등에 대한 언급이 없습니다. 비동 등 테스트의 피연산자 중 하나가 Null이면 결과도 알 수 없습니다 (null 임). 그러나 두 피연산자가 모두 Null인지 알고 싶다면 Null == Null이 합리적인 테스트입니다 (true가되어야 함). 연산자의 불평등 부분을 제거하는 것이 모든 차이를 만듭니다.

http://msdn.microsoft.com/en-us/library/2cf62fcy.aspx#sectionToggle4 의 다음 코드 예제는 C #에서 Null을 처리하는 방법을 요약합니다.

int? num1 = 10;   
int? num2 = null;   
if (num1 >= num2)   
{   
    Console.WriteLine("num1 is greater than or equal to num2");   
}   
else   
{   
    // This clause is selected, but num1 is not less than num2.   
    Console.WriteLine("num1 >= num2 returned false (but num1 < num2 also is false)");   
}   

if (num1 < num2)   
{   
    Console.WriteLine("num1 is less than num2");   
}   
else   
{   
    // The else clause is selected again, but num1 is not greater than   
    // or equal to num2.   
    Console.WriteLine("num1 < num2 returned false (but num1 >= num2 also is false)");   
}   

if (num1 != num2)   
{   
    // This comparison is true, num1 and num2 are not equal.   
    Console.WriteLine("Finally, num1 != num2 returns true!");   
}   

// Change the value of num1, so that both num1 and num2 are null.   
num1 = null;   
if (num1 == num2)   
{   
    // The equality comparison returns true when both operands are null.   
    Console.WriteLine("num1 == num2 returns true when the value of each is null");   
}   

/* Output:   
 * num1 >= num2 returned false (but num1 < num2 also is false)   
 * num1 < num2 returned false (but num1 >= num2 also is false)   
 * Finally, num1 != num2 returns true!   
 * num1 == num2 returns true when the value of each is null   
 */   

>=숫자 값에서 작동합니다. null이 아닙니다.

당신은 할 수 과부하>= 특정 유형에 원하는 것을 제공하는 연산자.


NULL is not zero (numeric or binary value), a zero-length string, or blank (character value). So any comparison operator will always return false on it. Read more about it here


What values would you expect?

null == null true

null >= null false

null > null false

null <= null false

null < null false

null != null false

1 == null false

1 >= null false

1 > null false

1 <= null false

1 < null false

1 != null true aka !(1 == null)


>= only means "greater than or equal" when used in that specific well defined way. When used on a class with overloaded operators it means anything the class developer wants it to mean. When applied to a string-like class, it might mean "sorts the same or higher" or it might mean "the same length or longer".


Since by default an int cannot be null and its value will be set to 0, the operator of > and < which is built for int type, expects to work with values and not with nulls.

see my answer to a similar question where I wrote some ways to handle nullable int with the less < and greater > operators https://stackoverflow.com/a/51507612/7003760

참고URL : https://stackoverflow.com/questions/4399932/why-does-return-false-when-returns-true-for-null-values

반응형