Forming C# string with multiple values using curly braces

In the last post we discussed about mutable string object, which is derived from the list which is mutable object. Today we will learn how make string with different type of values for console programs.

The boast string will help you to understand the concept. Actually C# has pretty neater treatment with strings. Start a fresh COnsole Application project and here is what we usually do when we have thing like this

            int count=20;
            float am=100 ;
            string symbol=” $”;
            string boastStr=”minutes”;
            double   min=2.50;
  Console.Write(“Boast string : I have finished ” + count + ” Apples and Oranges ( ” + am + symbol + “) in ” + min + ” ” + boastStr + ” , wow”) ;

The message is just a joke , lol.

 You can put values inside within a single string by using a { }, curly braces and cardinals instead of the old fashioned + operator, but limited to console only. It will look like,
“I have to reach the station at {0}”, min . Can also use many variables in a string.

Lets convert our boast string too

            Console.WriteLine (“\nBoast string : I have finished {0} Apples and Oranges (  {1} {2}  ) in   {3}  {4} , wow”,count,am,symbol,min,boastStr );

Remember it’s only work with Console.Write().

Published by


A developer,teacher and a blogger obsessed with Python,Dart and open source world from India

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.