MVC C# Converting string to int

Here is an example for converting string to int in c#.


You can use System.Convert library to convert this.

 private static string myString = "123";
 private int id = Convert.ToInt32(myString);


Or, you can use System.Int32
 private string myString = "123";
 private int id = Int32.Parse(myString);


If your string is not numeric then it will throw an exception. In that case you can use the following.           
  private static string myString = "123";
 private static int convertedValue;
 private bool success = Int32.TryParse(myString, out convertedValue);



Here success will determine if your string was converted successfully or not. The convertedValue will contain the converted string value if its successful, otherwise contain its initialized value.