Cor Ligthert said:
For me is a try catch the same as a late binding, when it is for in advance
catchable error handling, it should be avoided.
Just for fun, I included using Information.IsDate in the benchmark
posted earlier. Here's the appropriate method:
static void TestIsDate()
{
for (int i=0; i < Iterations; i++)
{
foreach (string x in invalid)
{
if (Information.IsDate(x))
{
try
{
DateTime.ParseExact(x, "dd/mm/yyyy",
CultureInfo.InvariantCulture);
throw new Exception("Invalid date passed");
}
catch
{
}
}
}
foreach (string x in valid)
{
if (Information.IsDate(x))
{
try
{
DateTime.ParseExact(x, "dd/mm/yyyy",
CultureInfo.InvariantCulture);
}
catch
{
throw new Exception("Valid date failed");
}
}
else
throw new Exception("Valid date failed: "+x);
}
}
}
The additional call to DateTime.ParseExact is required if you want to
actually *get* the date. If you don't, and you trust IsDate completely,
the performance will obviously be better.
It only worked after setting the current culture of the current thread
to US:
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture
("en-US");
Results:
With DateTime.ParseExact as well: 27.53s
Without DateTime.ParseExact : 25.97s
So using the VB libraries is about 5 times slower than using a hard-
coded method, and requires extra changes to the running environment.
It's also over twice as slow as just using try/catch directly.