Codechef4u is a community for computer professionals,by computer professionals,just like you; who loves sharing and helping each others,Join them
Share your post

String Interpolation


String Interpolation

With C# 5.0 or previously, we use one of the following ways to concatenate two or more strings together.

public class Student
    {
        private const string _firstName = "Nagnath";
        private const string _lastName = "Kendre";
      
        public void DisplayStudentDetails()
        {
            Console.WriteLine("Name : " + _firstName + " " + _lastName);
            Console.WriteLine(string.Format("Name : {0} {1}", _firstName, _lastName));

        }
    }


In C# 6.0, we have a clean and easy format with first WriteLine call, but we can also put expressions directly in the string literal to evaluate an expression as shown below (last WriteLine):

public class Student
    {
        private const string _firstName = "Nagnath";
        private const string _lastName = "Kendre";
        private int TotalSpentMony = 55000;
      
        public void DisplayStudentDetails()
        {
           WriteLine("User Name : \{firstName} \{lastName}");
           WriteLine("User Name : \{firstName} \{lastName}\n Shopping Amount :\{TotalSpentMony }");
      WriteLine("Name : \{firstName} \{lastName}\nDiscount :\{TotalSpentMony>= 250000 ? " You get 15% off onYour order!" : ""}");
 
        }
}

 < > 

Invalid entry,please enter valid data.

Loading