Find the Error in following program – wrong casting in C#

Find the Error in following program-

static void Main(string[] args)
{
// Syntax Error
string FirstNumber = "100";
long SecondNumber = 500;
long Result = FirstNumber + SecondNumber;
Console.WriteLine(Result);
}

 

Solution:

FIrstNumber is string datatype so it can not be sum with SecondNumber which is long.

Error Message “Cannot implicitly convert type ‘string’ to ‘long'”

 

Correct program is as follows

static void Main(string[] args)
{
// Syntax Error
long FirstNumber = 100;
long SecondNumber = 500;
long Result = FirstNumber + SecondNumber;
Console.WriteLine(Result);
}

 

See the Boxing and Unboxing in c#