Over a million developers have joined DZone.

Converting String to Int in a Method With C#

·

C# makes it easy to move between fundamental object types.  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));
}
}
}

 

Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}