Convert String to Integer in C#

Hi Programmers, welcome to ozanecare. the new post to let you know three important ways to convert string into Integer. we can convert string into integer with the help of Convert.Int method , int.Parse method or int.TryParse method. Lets see the codes.

1.By Using Convert.ToInt method.see below image.

2.By Using int.Parse method.see below image.

3.By Using int.TryParse method.see below image.

Combined codes of all methods. see down.

using System;
namespace ConsoleApp1
    class Program
        static void Main(string[] args)
            // 1st way
	    // 2nd way
 	    String s = "45";
            int d = int.Parse(s);
            //3rd way
	    string s1 = "10";
            string s2 = "20";
            int n1, n2;
            int.TryParse(s1, out n1);
            int.TryParse(s2, out n2);
            Console.WriteLine("After Converting to Integer {0} and {1} ", n1, n2);       

Happy Programming…thanks.

Post Author: adama