How to insert values into a string using string interpolation in C#

4 Answers

0 votes
using System;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            int csharp = 1, cpp = 2, java = 3;

            string s = $"c# = {csharp} c++ = {cpp} java = {java}";

            Console.WriteLine(s);
        }
    }
}


/*
run:

c# = 1 c++ = 2 java = 3

*/

 



answered Mar 8, 2017 by avibootz
0 votes
using System;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = { 1, 2, 3 };

            string s = $"c# = {arr[0]} c++ = {arr[1]} java = {arr[2]}";

            Console.WriteLine(s);
        }
    }
}


/*
run:

c# = 1 c++ = 2 java = 3

*/

 



answered Mar 8, 2017 by avibootz
0 votes
using System;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = 10;

            string s = $"a = {n*13} b = {n+200}";

            Console.WriteLine(s);
        }
    }
}


/*
run:

a = 130 b = 210

*/

 



answered Mar 8, 2017 by avibootz
0 votes
using System;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = $"result = {Calc(5)}";

            Console.WriteLine(s);
        }
        static int Calc(int n)
        {
            return n * n * n;
        }
    }
}


/*
run:

result = 125

*/

 



answered Mar 8, 2017 by avibootz
edited Mar 8, 2017 by avibootz

Related questions

1 answer 146 views
1 answer 88 views
1 answer 184 views
2 answers 213 views
1 answer 103 views
...