asked on
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace EE_Q28585128
{
class Program
{
static int result = 1;
static bool? prevEnabled = true;
static void Main(string[] args)
{
Console.WriteLine("Should be false: {0}", ((result < 0) && (prevEnabled ?? false)));
result = -1;
prevEnabled = null;
Console.WriteLine("Should be false: {0}", ((result < 0) && (prevEnabled ?? false)));
prevEnabled = true;
Console.WriteLine("Should be true: {0}", ((result < 0) && (prevEnabled ?? false)));
Console.ReadLine();
}
}
}
if ((result<0) && (prevEnabled.HasValue && prevEnabled.Value == false))
Or as short-handed as:
if ((result<0) && !(prevEnabled ?? true))
They mean the same thing:
using System;
namespace EE_Q28585128
{
class Program
{
static int result = 1;
static bool? prevEnabled = false;
static void Main(string[] args)
{
Console.WriteLine("Has Value Version when result is greater than 0 and prevEnabled is false");
Console.WriteLine("Should be false: {0}", ((result < 0) && (prevEnabled.HasValue && prevEnabled.Value == false)));
Console.WriteLine();
Console.WriteLine("Null-Coalescing Version when result is greater than 0 and prevEnabled is false");
Console.WriteLine("Should be false: {0}", ((result < 0) && !(prevEnabled ?? true)));
result = -1;
Console.WriteLine();
Console.WriteLine("Has Value Version when result is less than 0 and prevEnabled is false");
Console.WriteLine("Should be true: {0}", ((result < 0) && (prevEnabled.HasValue && prevEnabled.Value == false)));
Console.WriteLine();
Console.WriteLine("Null-Coalescing Version when result is less than 0 and prevEnabled is false");
Console.WriteLine("Should be true: {0}", ((result < 0) && !(prevEnabled ?? true)));
prevEnabled = null;
Console.WriteLine();
Console.WriteLine("Has Value Version when result is less than 0 and prevEnabled is null");
Console.WriteLine("Should be false: {0}", ((result < 0) && (prevEnabled.HasValue && prevEnabled.Value == false)));
Console.WriteLine();
Console.WriteLine("Null-Coalescing Version when result is less than 0 and prevEnabled is null");
Console.WriteLine("Should be false: {0}", ((result < 0) && !(prevEnabled ?? true)));
result = 1;
Console.WriteLine();
Console.WriteLine("Has Value Version when result is greater than 0 and prevEnabled is null");
Console.WriteLine("Should be false: {0}", ((result < 0) && (prevEnabled.HasValue && prevEnabled.Value == false)));
Console.WriteLine();
Console.WriteLine("Null-Coalescing Version when result is greater than 0 and prevEnabled is null");
Console.WriteLine("Should be false: {0}", ((result < 0) && !(prevEnabled ?? true)));
Console.ReadLine();
}
}
}
Produces the following output -They mean the same thing:No, they do not.
!(prevEnabled ?? true)
In the explicit version HasValue, we can agree that HasValue returns false if prevEnabled is null and in the null-coalescing version if prevEnabled is null, then false is returned based on the not operator.if (prevEnabled.HasValue && prevEnabled.Value == false)
And
if !(prevEnabled ?? true)
Produce the same results.ASKER
ASKER
it will never be null when it reaches the if statement otherwise you need to test for that as well
Nullable<string> s = ...
The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.
TRUSTED BY
Something other issue is there.
Please copy paste the exact code, so quickly we will get the answer.
I tried with following and it's ok. No Error.
Open in new window