How to concatenate string with integer in C#

2 Answers

0 votes
using System;

class Program
{
    static void Main()
    {
        int number = 42;
        string str = "The answer is: ";
        string result = str + number;
        
        Console.WriteLine(result); 
    }
}


/*
run:

The answer is: 42

*/

 



answered Jun 17, 2025 by avibootz
0 votes
using System;

class Program
{
    static void Main()
    {
        int number = 42;
        string result = $"The answer is: {number}";
        
        Console.WriteLine(result); 
    }
}


/*
run:

The answer is: 42

*/

 



answered Jun 17, 2025 by avibootz

Related questions

1 answer 208 views
1 answer 234 views
2 answers 308 views
1 answer 227 views
227 views asked Dec 9, 2023 by avibootz
1 answer 323 views
323 views asked Aug 11, 2021 by avibootz
3 answers 390 views
1 answer 251 views
...