developer tip

nullable bool로 변환 하시겠습니까?

optionbox 2020. 8. 6. 08:14
반응형

nullable bool로 변환 하시겠습니까? 부울


C #에서 nullable bool?어떻게 변환 bool합니까?

나는 시도 x.Value또는 x.HasValue...


결국 널 부울이 무엇을 나타내는 지 결정해야합니다. null해야하는 경우 false다음을 수행 할 수 있습니다.

bool newBool = x.HasValue ? x.Value : false;

또는:

bool newBool = x.HasValue && x.Value;

또는:

bool newBool = x ?? false;

null-coalescing 연산자 :를 사용할 수 있습니다 . x ?? something여기서 somethingif x사용하려는 부울 값입니다 null.

예:

bool? myBool = null;
bool newBool = myBool ?? false;

newBool 거짓이 될 것이다.


Nullable{T} GetValueOrDefault()방법 을 사용할 수 있습니다 . null의 경우는 false를 돌려줍니다.

 bool? nullableBool = null;

 bool actualBool = nullableBool.GetValueOrDefault();

가장 쉬운 방법은 null 병합 연산자를 사용하는 것입니다. ??

bool? x = ...;
if (x ?? true) { 

}

??널 (NULL) 값은 제공 가능 표현식을 검사하여 작동합니다. 널 입력 가능 표현식에 값이 있으면 해당 값이 사용되며 그렇지 않으면 오른쪽의 표현식이 사용됩니다.??


bool?in if을 사용하려는 경우 가장 쉬운 방법은 true또는 과 비교하는 것 false입니다.

bool? b = ...;

if (b == true) { Debug.WriteLine("true"; }
if (b == false) { Debug.WriteLine("false"; }
if (b != true) { Debug.WriteLine("false or null"; }
if (b != false) { Debug.WriteLine("true or null"; }

물론 null과 비교할 수도 있습니다.

bool? b = ...;

if (b == null) { Debug.WriteLine("null"; }
if (b != null) { Debug.WriteLine("true or false"; }
if (b.HasValue) { Debug.WriteLine("true or false"; }
//HasValue and != null will ALWAYS return the same value, so use whatever you like.

응용 프로그램의 다른 부분으로 전달하기 위해 부울로 변환하려는 경우 Null Coalesce 연산자가 필요합니다.

bool? b = ...;
bool b2 = b ?? true; // null becomes true
b2 = b ?? false; // null becomes false

이미 null을 확인하고 값을 원하면 Value 속성에 액세스하십시오.

bool? b = ...;
if(b == null)
    throw new ArgumentNullException();
else
    SomeFunc(b.Value);

bool? a = null;
bool b = Convert.toBoolean(a); 

완전한 방법은 다음과 같습니다.

bool b1;
bool? b2 = ???;
if (b2.HasValue)
   b1 = b2.Value;

또는 다음을 사용하여 특정 값을 테스트 할 수 있습니다

bool b3 = (b2 == true); // b2 is true, not false or null

다음과 같은 것 :

if (bn.HasValue)
{
  b = bn.Value
}

이 답변은 단순히 bool?조건 을 테스트하려는 유스 케이스 에 대한 것입니다. 또한 법선을 얻는 데 사용될 수 있습니다 bool. 내가 개인적으로보다 쉽게 ​​읽을 수있는 대안 coalescing operator ??입니다.

조건을 테스트하려면 이것을 사용할 수 있습니다

bool? nullableBool = someFunction();
if(nullableBool == true)
{
    //Do stuff
}

The above if will be true only if the bool? is true.

You can also use this to assign a regular bool from a bool?

bool? nullableBool = someFunction();
bool regularBool = nullableBool == true;

witch is the same as

bool? nullableBool = someFunction();
bool regularBool = nullableBool ?? false;

This is an interesting variation on the theme. At first and second glances you would assume the true branch is taken. Not so!

bool? flag = null;
if (!flag ?? true)
{
    // false branch
}
else
{
    // true branch
}

The way to get what you want is to do this:

if (!(flag ?? true))
{
    // false branch
}
else
{
    // true branch
}

System.Convert works fine by me.

using System; ... Bool fixed = Convert.ToBoolean(NullableBool);

참고URL : https://stackoverflow.com/questions/6075726/convert-nullable-bool-to-bool

반응형