If I try to do this it doesn’t work:
static void Main(string[] args)
{
int a = 5000;
Console.WriteLine((string)a);
}
But somehow this works fine:
static void Main(string[] args)
{
int a = 5000;
Console.WriteLine(a + "");
}
Why is that? Is it because the first is trying to change the base type and the second just appends the value to the string?
intcannot be cast tostring(1), but there is an operator+that acceptsintas left-hand argument andstringas right-hand argument (2). This operator converts theinttostringand concatenates the twostrings.A better approach would probably be to just use
Console.WriteLine(a);which would calla.ToString()for you.