Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Converting String to Int in a Method With C#

DZone's Guide to

Converting String to Int in a Method With C#

·
Free Resource

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.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}