This is the simplest and fastest approach:
return "True".Equals(readValue("Useable"), StringComparison.OrdinalIgnoreCase);
Note: Boolean.TryParse
is not a good choice as it is significantly slower than a simple string comparison. Please see the results of this test (using Jon Skeet's BenchmarkHelper):
using System;
using BenchmarkHelper;
class Example
{
static void Main()
{
var results = TestSuite.Create
("Boolean.TryParse vs. String comparison", "True", true)
.Add(tryParse)
.Add(stringComparison)
.RunTests()
.ScaleByBest(ScalingMode.VaryDuration);
results.Display(ResultColumns.NameAndDuration | ResultColumns.Score,
results.FindBest());
}
static Boolean tryParse(String input)
{
Boolean result;
Boolean.TryParse(input, out result);
return result;
}
static Boolean stringComparison(String input)
{
return "True".Equals(input, StringComparison.OrdinalIgnoreCase);
}
}
Output:
============ Boolean.TryParse vs. String comparison ============
tryParse 12.118 6.03
stringComparison 21.895 1.00