Alternative to double-not(!!)-C#

In C#, the double not operator (!!) is not available. Nonetheless, it can be attained comparable functionality by utilizing the following methods in C#

Sukhpinder Singh | C# .Net
.Net Programming
Published in
2 min readJun 28, 2023

--

Photo by Nubelson Fernandes on Unsplash

Objective

The article demonstrates alternative methods in C# to achieve functionality similar to the double not operator (!!) by using the Convert.ToBoolean() and bool.Parse() methods. It contains the code examples for both methods and recommends explicit conversions instead of relying on the double-not operator.

The Convert.ToBoolean() method

In C#, use the Convert.ToBoolean() method to convert a value to its boolean equivalent. Please find below the code example for the same.

string value= "Hello, world!";
bool isTrue = Convert.ToBoolean(value);
Console.WriteLine(isTrue); // Outputs: True
string value= "";
bool isFalse= Convert.ToBoolean(value);
Console.WriteLine(isFalse); // Outputs: False

The bool.Parse() method

In C#, the bool.Parse() method provides another option for converting a value into a boolean type. Please find below the code example for the same.

string value= "Hello, world!";
bool…

--

--

Sukhpinder Singh | C# .Net
.Net Programming

.Net Developer - Editor at .Net Programming with ~48K views per month, Looking for C# writers. Link: https://medium.com/c-sharp-programming