C#
performance
data conversion
Double.TryParse
Convert.ToDouble

Double.TryParse or Convert.ToDouble - which is faster and safer?

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

Double.TryParse and Convert.ToDouble both produce a double, but they serve different purposes. If your input might be invalid and you want safe, exception-free handling, Double.TryParse is usually the better choice. If you are converting a value that is already a known numeric type or you want framework-style conversion semantics, Convert.ToDouble can be appropriate.

What Each API Is Designed To Do

Double.TryParse is specifically for parsing text:

csharp
1using System;
2
3string input = "12.34";
4
5if (double.TryParse(input, out double value))
6{
7    Console.WriteLine(value);
8}
9else
10{
11    Console.WriteLine("Invalid number");
12}

It returns true or false and avoids exceptions on bad input.

Convert.ToDouble is broader. It converts many types through .NET conversion rules:

csharp
1using System;
2
3object value1 = 12;
4object value2 = "12.34";
5
6Console.WriteLine(Convert.ToDouble(value1));
7Console.WriteLine(Convert.ToDouble(value2));

That flexibility is useful, but if conversion fails, it throws.

Safety Differences

For user input, TryParse is almost always safer:

csharp
1using System;
2
3string input = "not-a-number";
4
5bool ok = double.TryParse(input, out double parsed);
6Console.WriteLine(ok);     // false
7Console.WriteLine(parsed); // 0

The equivalent Convert.ToDouble call throws a FormatException:

csharp
1using System;
2
3string input = "not-a-number";
4double parsed = Convert.ToDouble(input); // throws

That difference matters in loops, APIs, and UI code where bad input is normal rather than exceptional.

Performance Differences

On valid input, the performance difference is often small enough that readability and behavior matter more. On invalid input, TryParse is typically much faster because exceptions are expensive.

That leads to a practical rule:

  • If failure is possible or common, use TryParse.
  • If the value is already validated or already numeric, Convert.ToDouble is fine.

For example, this is a good fit for Convert.ToDouble:

csharp
object number = 42m;
double value = Convert.ToDouble(number);
Console.WriteLine(value);

That is not really a parsing scenario. It is a conversion scenario.

Culture Matters

Both APIs can be affected by culture when strings are involved. If you parse machine-readable input, be explicit:

csharp
1using System;
2using System.Globalization;
3
4string input = "12.34";
5
6bool ok = double.TryParse(
7    input,
8    NumberStyles.Float | NumberStyles.AllowThousands,
9    CultureInfo.InvariantCulture,
10    out double value
11);
12
13Console.WriteLine(ok);
14Console.WriteLine(value);

Without an explicit culture, the current thread culture may interpret decimal separators differently from what your data source expects.

Null Handling Differences

Convert.ToDouble(null) returns 0, which surprises many developers:

csharp
1using System;
2
3object? input = null;
4double value = Convert.ToDouble(input);
5Console.WriteLine(value); // 0

TryParse does not silently convert a missing numeric string into a meaningful number. That often makes it easier to reason about.

If null should remain distinct from 0, handle it before conversion rather than relying on Convert.

Choosing The Right API

Use Double.TryParse when:

  • You are reading text from users, files, or HTTP requests.
  • Invalid input is expected and should not throw.
  • You want explicit success or failure handling.

Use Convert.ToDouble when:

  • You are converting already-known framework values.
  • You intentionally want exceptions on invalid string input.
  • You want .NET's general conversion behavior from object.

Common Pitfalls

The biggest mistake is using Convert.ToDouble on user input inside a hot path and then relying on exception handling for validation. That is both slower on bad input and harder to read.

Another common issue is ignoring culture. A string that parses correctly on one machine may fail or mean something else on another if decimal separators differ.

Developers also sometimes compare these APIs only on speed and ignore semantics. "Faster" is not the main question if the method behaves differently for nulls, invalid strings, or non-string objects.

Finally, if you already know the value is a string and you only want to parse it safely, TryParse communicates that intent better than Convert.

Summary

  • 'Double.TryParse is usually safer for parsing uncertain string input.'
  • 'Convert.ToDouble is broader and useful for general .NET type conversion.'
  • On invalid input, TryParse is typically faster because it avoids exceptions.
  • Be explicit about culture when parsing machine-readable numeric strings.
  • Choose the API based on behavior first, then performance.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.