The Convert Class and Parsing
Parse and convert with Convert and TryParse.
The Convert Class and Parsing is a free C# Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Converting Strings to Numbers
User input arrives as text. To compute with it you must convert strings to numbers. C# offers Convert, Parse, and TryParse.
using System;
class Program
{
static void Main()
{
string input = "42";
int number = int.Parse(input);
Console.WriteLine(number + 8);
}
}int.Parse
int.Parse turns a numeric string into an int. It throws a FormatException if the text is not a valid number.
using System;
class Program
{
static void Main()
{
try
{
int n = int.Parse("abc");
Console.WriteLine(n);
}
catch (FormatException)
{
Console.WriteLine("Not a valid number");
}
}
}int.TryParse
int.TryParse avoids exceptions: it returns true on success and puts the result in an out parameter, or returns false on failure.
using System;
class Program
{
static void Main()
{
if (int.TryParse("123", out int n))
Console.WriteLine("Parsed: " + n);
else
Console.WriteLine("Failed");
}
}TryParse for Safe Input
TryParse is the preferred way to handle untrusted input because invalid text is handled gracefully instead of crashing.
using System;
class Program
{
static void Main()
{
string[] inputs = { "10", "oops", "30" };
int total = 0;
foreach (var s in inputs)
{
if (int.TryParse(s, out int v))
total += v;
}
Console.WriteLine("Total of valid: " + total);
}
}The Convert Class
The Convert class converts between many base types: Convert.ToInt32, Convert.ToDouble, Convert.ToBoolean, and more.
using System;
class Program
{
static void Main()
{
int i = Convert.ToInt32("256");
double d = Convert.ToDouble("3.14");
bool b = Convert.ToBoolean("true");
Console.WriteLine(i + ", " + d + ", " + b);
}
}Convert Handles null
Unlike int.Parse, Convert.ToInt32(null) returns 0 instead of throwing, which can be convenient or surprising.
using System;
class Program
{
static void Main()
{
string s = null;
int n = Convert.ToInt32(s); // returns 0
Console.WriteLine("Result: " + n);
}
}Convert Between Numeric Types
Convert also turns one numeric type into another and rounds (not truncates) when converting floating values to integers.
using System;
class Program
{
static void Main()
{
double d = 7.6;
int cast = (int)d; // truncates -> 7
int converted = Convert.ToInt32(d); // rounds -> 8
Console.WriteLine("cast=" + cast + ", convert=" + converted);
}
}Parsing Other Types
Most built-in types have their own Parse/TryParse: double.TryParse, bool.TryParse, DateTime.TryParse, and so on.
using System;
class Program
{
static void Main()
{
bool okD = double.TryParse("2.5", out double d);
bool okB = bool.TryParse("True", out bool flag);
Console.WriteLine(okD + ":" + d + ", " + okB + ":" + flag);
}
}ToString for the Reverse
To go from a number back to text, call ToString(), optionally with a format string.
using System;
class Program
{
static void Main()
{
int n = 255;
double price = 12.5;
Console.WriteLine(n.ToString());
Console.WriteLine(price.ToString("C")); // currency format
}
}Choosing the Right Tool
Use TryParse for user input you cannot trust, Parse when input is guaranteed valid, and Convert when handling nulls or converting between many types.
using System;
class Program
{
static void Main()
{
string userInput = "55x";
int value = int.TryParse(userInput, out int v) ? v : -1;
Console.WriteLine(value == -1 ? "Invalid input" : "Got " + value);
}
}Putting It Together
A robust input flow validates with TryParse, defaults on failure, then computes safely.
using System;
class Program
{
static int ReadAge(string input)
{
if (int.TryParse(input, out int age) && age >= 0)
return age;
return 0; // safe default
}
static void Main()
{
Console.WriteLine("Age: " + ReadAge("28"));
Console.WriteLine("Age: " + ReadAge("bad"));
}
}Quick Check
Test your understanding of parsing and conversion.
Recap
Convert text to numbers with int.Parse (throws on bad input), int.TryParse (safe, returns a bool + out value), or the Convert class (handles null, rounds floats, converts many types). Prefer TryParse for untrusted input and ToString() for the reverse direction.
using System;
class Program
{
static void Main()
{
if (int.TryParse("99", out int n))
Console.WriteLine("Value: " + n);
Console.WriteLine("Convert: " + Convert.ToInt32("7"));
}
}Frequently asked questions
Is the “The Convert Class and Parsing” lesson free?
Yes — the full text of “The Convert Class and Parsing” is free to read here on the web, and the C# Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.
What will I learn in “The Convert Class and Parsing”?
Parse and convert with Convert and TryParse. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “The Convert Class and Parsing” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Value Types vs Reference Types
- Boxing and Unboxing
- Implicit and Explicit Conversions
- The Convert Class and Parsing