3 Replies - 392 Views - Last Post: 16 July 2020 - 03:22 PM Rate Topic: -----

#1 petabyte   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 19-February 20

Unable to print out variable in C#

Posted 28 April 2020 - 05:30 PM

Hello Forums,

Can someone explain why this code isn't working as its suppose to display the name and a number but instead it displays the placeholders?

using System;

namespace BeginnersCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Console.WriteLine("Learning C#");


            int x = 10;
            double y = 20;
            Console.WriteLine("x={0};y={1}", x, y);

            int a = 4;
            int b = 2;
            Console.Write(a);
            Console.Write(B)/>;
            Console.WriteLine("");

            string yourName;
            Console.WriteLine("What is your name?");
            yourName = Console.ReadLine();
            Console.WriteLine("Hello {0}, yourName");


            int age = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("You are {0} years old, age");
        }
    }
}


Ignore the beginning code above, this part is having the issue

string yourName;
            Console.WriteLine("What is your name?");
            yourName = Console.ReadLine();
            Console.WriteLine("Hello {0}, yourName");


            int age = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("You are {0} years old, age");


Here's what it looks like in the terminal output
https://i.postimg.cc...riableerror.png

Is This A Good Question/Topic? 0
  • +

Replies To: Unable to print out variable in C#

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15797
  • View blog
  • Posts: 63,297
  • Joined: 12-June 08

Re: Unable to print out variable in C#

Posted 28 April 2020 - 05:45 PM

This line:
26	            Console.WriteLine("Hello {0}, yourName");

is not like this line:
15	            Console.WriteLine("x={0};y={1}", x, y);


The first has no variables used for interpolatin.

String interpolation.
https://docs.microso...ns/interpolated

(the important part about the variables used AFTER the closing quote and a comma.)
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7453
  • View blog
  • Posts: 25,095
  • Joined: 05-May 12

Re: Unable to print out variable in C#

Posted 29 April 2020 - 06:39 PM

Console.WriteLine("x={0};y={1}", x, y);


Actually, in the code above, there is no string interpolation happening. It's just the classic format string. The "x={0};y={1}" is the format string, and the x and y.

This is the interpolated string version available in new version of the C# language:
Console.WriteLine($"x={x};y={y}");



Anyway moving this thread to the general C# help section instead of the C# language discussion section.
Was This Post Helpful? 0
  • +
  • -

#4 KeyWiz   User is offline

  • D.I.C Regular
  • member icon

Reputation: 14
  • View blog
  • Posts: 454
  • Joined: 26-October 06

Re: Unable to print out variable in C#

Posted 16 July 2020 - 03:22 PM

View Postpetabyte, on 28 April 2020 - 06:30 PM, said:

Hello Forums,

Can someone explain why this code isn't working as its suppose to display the name and a number but instead it displays the placeholders?

using System;

namespace BeginnersCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Console.WriteLine("Learning C#");


            int x = 10;
            double y = 20;
            Console.WriteLine("x={0};y={1}", x, y);

            int a = 4;
            int b = 2;
            Console.Write(a);
            Console.Write(B)/>/>;
            Console.WriteLine("");

            string yourName;
            Console.WriteLine("What is your name?");
            yourName = Console.ReadLine();
            Console.WriteLine("Hello {0}, yourName");


            int age = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("You are {0} years old, age");
        }
    }
}


Ignore the beginning code above, this part is having the issue

string yourName;
            Console.WriteLine("What is your name?");
            yourName = Console.ReadLine();
            Console.WriteLine("Hello {0}, yourName");


            int age = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("You are {0} years old, age");


Here's what it looks like in the terminal output
https://i.postimg.cc...riableerror.png


Here is the problem,
            string yourName;
            Console.WriteLine("What is your name?");
            yourName = Console.ReadLine();
            Console.WriteLine("Hello {0}", yourName);


            int age = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("You are {0} years old", age);



The {0} is a placeholder for a value, the value must be assigned after the Quote but within the Parenthesis. the {0} gets replaced with the value of the variable you place after the quote. Very easy to misunderstand.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1