none
what is the significance of + while printing a value to console

    Question

  • Here is the program:

    1. public static void Main()

    2. {

    3. int m_sum = 12;
    4. int m_count = 2;
    5. float m_average;

    6. m_average = m_sum / m_count;
    7.Console.WriteLine("result is:" +m_average);
    8.Console.ReadLine();

    }

    In  line 7, if i write Console.WriteLine("result is:" +m_average); i can see the output as "RESULT IS : 6"

    but if i write Console.WriteLine("result is:" , m_average) i can see "RESULT IS : " numeric value is not there. can anyone please explain why? what is the significance of the '+'? it may be a very basic question but i am pretty new to this language. please help me out here.

    Thanks in advance.

    Friday, March 09, 2018 12:12 AM

Answers

  • Hello buddy, the + is a concatenation. So if you have multiple strings "Hello " + "World" = "Hello World".

    As for you example you concatenated the "result is:" + m_average(Which has a value of 6) so it became "result is: 6".

    btw, you can read more about Console.WriteLine() in this link

    https://msdn.microsoft.com/en-us/library/system.console.writeline(v=vs.110).aspx

    You can also use the , as concatenation like this

    using System;
    
    public class Example
    {
       public static void Main()
       {
          Console.WriteLine("Hello {0}", "World");
       }
    }
    // The example displays output like the following:
    // Hello World



    Friday, March 09, 2018 12:20 AM

All replies

  • Hello buddy, the + is a concatenation. So if you have multiple strings "Hello " + "World" = "Hello World".

    As for you example you concatenated the "result is:" + m_average(Which has a value of 6) so it became "result is: 6".

    btw, you can read more about Console.WriteLine() in this link

    https://msdn.microsoft.com/en-us/library/system.console.writeline(v=vs.110).aspx

    You can also use the , as concatenation like this

    using System;
    
    public class Example
    {
       public static void Main()
       {
          Console.WriteLine("Hello {0}", "World");
       }
    }
    // The example displays output like the following:
    // Hello World



    Friday, March 09, 2018 12:20 AM
  • For short, Console.WriteLine(string, param string[]) is treated as if you're writing Console.WriteLine(String.Format(string, param string[])). Any formatting string that is valid for String.Format() would be treated with the same behavior.
    Friday, March 09, 2018 1:34 AM
    Answerer
  • thank you so much
    Saturday, March 10, 2018 12:07 AM
  • Hi MR_C_Sharp_Learner,

    If your issue is solved please Mark as answer or Vote as helpful post to the appropriate answer so that it will help other members to find solution if they faces similar issue.

    Your understanding and cooperation will be grateful.

    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Monday, March 12, 2018 7:51 PM
    Moderator