Convert string to int in C#

C#

In this post, we are going to learn how to convert string to int in C#. We will use convert class, Parse(), TryParse() method and convert string number to int to o an equivalent 16,32,64 bit signed integer with program example.

3 methods to Convert string to int in C#


  • Using System.convert class method
  • Using Parse() method
  • using TryParse() method

1. Convert class to Convert string to int in C#


The system namespace convert class has static methods to convert a string numeric value to equivalent 16,32,64 bit signed integer and throw FormatException exception, So it is best practice to use try-catch block. if the string value is not a valid number or in case of null value converted to 0 no exception throw.

Diiferent method for signed integer in convert class

  • Convert.ToInt16() : It convert a string number to 16 bit signed integer(short).
  • Convert.ToInt32() : It converts a string number to 32 bit signed integer
  • Convert.ToInt64() : It converts a string number to 64 bit signed integer(long)

C# Program Example

using System;  
namespace ProgramExample
{
    class conversion
    {
        static void Main(string[] args)
        {
         try
           {
            Console.WriteLine("Please Enter int value :");
            string str_val = Console.ReadLine();
            Int16 int16_res = Convert.ToInt16(str_val);
            Int32 int32_res = Convert.ToInt32(str_val);
            Int64 int64_res = Convert.ToInt64(str_val);
            Console.WriteLine("string to int conversion :\n int16 :{0}\n int32:{1}\n int64:{2}", int16_res, int32_res, int64_res);

            Console.WriteLine(Convert.ToInt32(null));

            Console.ReadLine();
}
catch(FormatException)
{
Console.WriteLine("Please enter valid string number:");
}
        }
    }
}

Output

Please Enter int value :
90
string to int conversion :
 int16 :90
 int32:90
 int64:90

2. Parse() method to convert string to int in C#


The parse() method converts a string number to an equivalent 16, 32, 64 bit signed integer and if it is a valid string number. It can be used to convert culture-specific formats.

Diiferent Parse() method for signed integer

  • Int16.Parse() : It convert a string number to 16 bit integer(short)
  • Int32.Parse() : It converts a string number to 32 bit integer
  • Int64.Parse() : It converts a string number to 64 bit integer(long).

C# Program Example

using System;
using System.Globalization;
namespace ProgramExample
{
    class conversion
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please Enter int value :");
            string str_val = Console.ReadLine();
            Int16 int16_res = Int16.Parse(str_val);
            Int32 int32_res = Int32.Parse(str_val);
            Int64 int64_res = Int64.Parse(str_val);
             Console.WriteLine("string to int conversion :\n int16 :{0}\n int32:{1}\n int64:{2}", int16_res, int32_res, int64_res);

           //culture specifc format
           Int64 res_culspec = Int64.Parse("532,100", NumberStyles.AllowThousands, new CultureInfo("en-US"));
            Console.WriteLine( res_culspec);
           
            Console.ReadLine();
        }
    }
}

Output

Please Enter int value :
90
string to int conversion :
 int16 :90
 int32:90
 int64:90

532100

3. TryParse() method string to int in C#


The tryparse() method is used when we are not sure about the input is a valid numeric string or not. converts a string number to an equivalent 16,32,64 bit signed integer. It is the best way to convert string to int. It is also used for culture-specific conversion.

It returns the boolean value TRUE if conversion is successful and returns the converted number in an out parameter else returns False when the conversion fails or is not a valid string.

C# Program Example

using System;
using System.Globalization;

namespace ProgramExample
{
    class conversion
    {
        static void Main(string[] args)
        {
            Int16 int16_res;
            Int32 int32_res;
            Int64 int64_res;
            Int64 culture;

            Console.WriteLine("Please Enter int value :");
            string str_val = Console.ReadLine();
            bool sucess16 = Int16.TryParse(str_val,out int16_res);
            bool sucess32 = Int32.TryParse(str_val,out int32_res);
            bool sucess64 = Int64.TryParse(str_val, out int64_res);
            Console.WriteLine("string to int conversion successful :\n int16 :{0}\n int32:{1}\n int64:{2}", sucess16, sucess32, sucess64);
            Console.WriteLine("string to int conversion value :\n int16 :{0}\n int32:{1}\n int64:{2}", int16_res, int32_res, int64_res);

            //culture specifc format
            bool res_culspec = Int64.TryParse("532,100", NumberStyles.AllowThousands, new CultureInfo("en-US"),out culture);
            Console.WriteLine(culture);

            Console.ReadLine();
        }

    }
}

Output

Please Enter int value :
89
string to int conversion successful :
 int16 :True
 int32:True
 int64:True

string to int conversion value :
 int16 :89
 int32:89
 int64:89
532100


Summary

In this post, we have learned 3 methods for how to convert Convert string to int in C# that includes with C# program example.

  • Convert class to Convert string to int in C# –
    • Convert.ToInt16()
    • Convert.ToInt32()
    • Convert.ToInt64()
  • Parse() method to convert string to int in C# –
    • Int16.Parse()
    • Int32.Parse()
    • Int64.Parse()
  • TryParse() method string to int in C#-
    • Int16.TryParse()
    • Int32.TryParse()
    • Int64.TryParse()