Parse in C#

Used to parse (convert) a string into a primitive data type.

///standard parse
dataType variableName = dataType.Parse(stringName);

///tryParse takes care of failure by assigning a default value, no need for try-catch
dataType result;
bool didItWork = dataType.TryParse(stringName, out result);


A standard parse throws an exception if the string cannot be converted to the specified type. It should be used inside a try-catch block.

TryParse returns a boolean to show whether or not the parsing worked. The second argument is used for the output variable, the 'out' keyword assigns the value to the variable.

string yearString = "2015";
int year;
bool validYear = int.TryParse(yearString, out year);

See Also

Try-Catch Block

Add to Slack

< Variable Declaration   |   Constants >

© 2019 SyntaxDB. All Rights Reserved.