Home » ASP.Net

Converting String to Int in a Method With C#

28. January 2008 by jdelpay 0 Comments

In the sample below, I will show you how to convert a string to int inside a method 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace method001
{
    class Program
    {
        static int calculDob(int a)
        {
            int year = 2008 - a; //calculation
            return year; // the value being returned

        }
        static void Main(string[] args)
        {
            Console.WriteLine("How old are you? ");// asking for some data
            int age = Convert.ToInt16(Console.ReadLine());// convert the entry to a int type and assign the value to the age variable
            Console.WriteLine("You were born in: {0}",calculDob(age));

        }
    }
}

 

Comments