C# int.TryParse

Int keyword

The int.TryParse method converts strings into ints. It never throws an exception—even on invalid input and null. It is called in a different way than int.Parse. It is overall preferable to int.Parse in most program contexts.


Note:Use int.TryParse to parse integers in most situations. This is often faster and results in simpler logic.

Note 2:This method is ideal for input that is not always correct. When errors occur, performance is better.


False literal

Here we see the int.TryParse method, which is considered a tester-doer. This method does not throw an exception when the input is invalid. Instead, it handles errors by returning false.

Bool Method

Tip:Your code that calls this method can simply check the return value. This is fastest when you need to deal often with invalid input.

C# program that uses int.TryParse

using System;

class Program
    static void Main()
	// See if we can parse the 'text' string. If we can't, TryParse
	// will return false. Note the "out" keyword in TryParse.
	string text1 = "x";
	int num1;
	bool res = int.TryParse(text1, out num1);
	if (res == false)
	    // String is not a number.

	// Use int.TryParse on a valid numeric string.
	string text2 = "10000";
	int num2;
	if (int.TryParse(text2, out num2))
	    // It was assigned.

	// Display both results.


Main method

In this example, the int.TryParse method is called twice in the Main method. When you call the int.TryParse method, the first parameter must be a regular string type, and the second parameter is an out parameter.

Out keyword

In the CLR, the out keyword is equivalent to the ref keyword on parameters. However, the out keyword helps with definite assignment analysis. With "out", the compiler can help you make sure your variables are always in a valid state.

Parameters Ref, OutThis section provides information

Next, the program uses int.TryParse on two input strings. The first input string is invalid—it uses the text "x". Obviously, that is not a numeric value. The second call uses the string "10000" which is parsed successfully.

Finally:It is useful to know that the word "int" in the C# language simply aliases the System.Int32 type.

Therefore:If you see code that uses "System.Int32.Parse" or "System.Int32.TryParse", it is equivalent to int.Parse and int.TryParse.


The C# Programming Language: C-Sharp

The C# compiler uses a process called definite assignment analysis to prove that variables are in a valid state whenever used. This is a form of static analysis. The algorithm is described in detail in the specification.

Tip:The out-keyword in int.TryParse tells the compiler that on return, the second parameter will be assigned to a value.



C# programming language

We used the int.TryParse method in a C# program. This method is ideal for parsing strings into numbers, when the strings may not be valid or may contain erroneous characters. The method returns true if it succeeds, and false if it doesn't.

Also:We described the out keyword and definite assignment analysis. And we explored some related issues.


C#: String