Home > OS >  C# Console.Write(" ", var);
C# Console.Write(" ", var);

Time:12-05

I'm starting to learn C# after learning Python, Css, HTML and a little bit of Js.

I have this code:

// Variables 

var name = "Bob";
var messages = "3";
var temperature ="34,4";

// Print First Part

Console.Write("Hello, ", name);
Console.Write("You have ", messages);
Console.Write("in your inbox. ");

//Print Part II

Console.Write("The temperature is ", temperature);
Console.Write("celsius.");

But for some reason the output is this:

Hello, You have in your inbox. The temperature is celsius.

It doesn't print the var. I really know it's a very basic question lol,where's the error? Any help ?

Thanks y'all for your responses and patience, I'm sorry for my misunderstanding, I think I'm doing wrong in applying other Coding Languages Knowledge in some of the things which are new for me in C# in this case. I would take a look at all your answers, analyze them and learn from them of course. Again thanks, a lot and have a nice day. I'm gonna mark one of your answers as solutions even tough all are correct.

Thanks and have a nice day.

CodePudding user response:

Coming from the other languages you posted I can see how it would be confusing. In eg python and JS you can write something like

print("hi ", name, " you have ", messages, " messages);

And you get a nice string printed out like you expect

C# doesn't print messages like this, but unfortunately (for you), the way you have tried is actually a legal c# syntax but it calls a method that does not work like you think it does

That version of Write you used has a signature like this:

Write(string formatString, params object[] dataItems)

The formatString argument is a string template that requires N parameter placeholders inside the first string then N parameters supplied as arguments after the string, which will be used to fill in the placeholders. Placeholders start at zero number. You can call the method like this:

Console.Write("Hello, {0}, you have {1} messages in your inbox", name, messages);

And the compiler will rewrite it for you so that all your different arguments after the first are scooped up and put into an array:

Console.Write("Hello, {0}, you have {1} messages in your inbox", new []{name, messages});

When the message is being formatted, the array item 0 is put into {0}. This means the name will be inserted into {0}, the messages will be inserted into the {1}

Hopefully you can now see why your attempt doesn't print the var; you didn't put a placeholder into the format string it indicate where the var should be printed. I know why you did this; you didn't know you had to because you wouldn't in python etc..

In essence you provided one data item, but put no placeholders. It is permitted to supply more data items than placeholders; extra data items are not used but you cannot supply more placeholders than data items:

//this will crash
Console.Write("Hello, {0}, you have {1} messages in your inbox", name);

//but this is fine, it just doesn't have a {1} placeholder so name will appear in the output, but messages will not 
Console.Write("Hello, {0}", name, messages);

The placeholders can accept formats too, after a colon

Console.Write("The temperature is {0:0.00} Celsius  ", temperature);

Note: Temperature needs to be some kind of "with decimal points" number, not a string, for a format like 0.00 to work (eg float temperature = 34.9;

Don't forget, if you want these things on different lines, you have the WriteLine method

Have a read of the string.Format documentation for a detailed look at string formatting. This version of Write accepts strings on the same pattern as string.Format does

CodePudding user response:

You can use string interpolation: start string with $ prefix: $"..." then print the text while inserting variables wrapping them in braces: {someValue}:

// Print First Part

Console.WriteLine($"Hello, {name}. You have {messages} in your inbox.");

// Print Second Part

Console.WriteLine($"The temperature is {temperature} celsius.");

CodePudding user response:

Try the following:

Console.Write("Hello, "   name);
Console.Write("You have "   messages);

Or

Console.Write($"Hello, {name}");
Console.Write($"You have {messages}");

CodePudding user response:

Try that:

// Variables 

        var name = "Bob ";
        var messages = "3 ";
        var temperature = "34,4 ";

        // Print First Part

        Console.Write("Hello, {0}", name);
        Console.Write("You have {0}", messages);
        Console.Write("in your inbox. ");

        //Print Part II

        Console.Write("The temperature is {0}", temperature);
        Console.Write("celsius.");
  •  Tags:  
  • c#
  • Related