Simple C# code understanding

Questions about the code for C#

Page 1 of 1

2 Replies - 6001 Views - Last Post: 18 June 2010 - 04:23 PM Rate Topic: -----

#1 JAMSTA  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 18-June 10

Simple C# code understanding

Posted 18 June 2010 - 03:38 PM

using System;

class Program
{
    static void Main()
    {
        Console.WriteLine("What is your name?: ");
        string name = Console.ReadLine();
        Console.WriteLine("Hello, {0}! ", name);
        Console.WriteLine("Welcome to my C# training :)/> ");
        Console.ReadLine();
    }
}



I get it all apart from: Where it says 'string name = Console.ReadLine();'. Why can't I just put Console.ReadLine() ?
There are 2 'Console.ReadLine();'
Where does the '{0}' come from and what does it mean ?
I am new to C Sharp as you can most likely predict, please either leave a detailed description that is simple for a 14 year old to understand in the forum or IM me at *******@hotmail.co.uk.

Thanks,
James :)

This post has been edited by JackOfAllTrades: 18 June 2010 - 05:05 PM
Reason for edit:: Added code tags.


Is This A Good Question/Topic? 0
  • +

Replies To: Simple C# code understanding

#2 Skaggles  Icon User is offline

  • THE PEN IS MIGHTIER
  • member icon





Reputation: 251
  • View blog
  • Posts: 640
  • Joined: 01-March 09

Re: Simple C# code understanding

Posted 18 June 2010 - 04:16 PM

The method Console.ReadLine() returns a string containing the data inputed by the user. So if I were to type in my name and press return, it would return a string containing my name. So by using a line like string myName = Console.ReadLine() you are assigning the variable myName to contain the string returned from ReadLine(). If you assign no variable to store the information, then the return value is discarded. The second ReadLine() does this only to wait for the user to press return before continuing with the code.

EDIT
As f or your other question regarding the {0}, this is a way to format a string with already existing data. Basically, {0} will replace itself with the first value following the string, {1} will replace itself with the second, {2} the third and so forth.

string myName = "Skaggles";
string greeting = string.format("Hi, my name is {0}!", myName);


This post has been edited by Skaggles: 18 June 2010 - 04:19 PM

Was This Post Helpful? 0
  • +
  • -

#3 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4437
  • View blog
  • Posts: 7,719
  • Joined: 08-June 10

Re: Simple C# code understanding

Posted 18 June 2010 - 04:23 PM

View PostJAMSTA, on 18 June 2010 - 02:38 PM, said:

using System;

class Program
{
static void Main()
{
Console.WriteLine("What is your name?: ");
string name = Console.ReadLine();
Console.WriteLine("Hello, {0}! ", name);
Console.WriteLine("Welcome to my C# training :) ");
Console.ReadLine();
}
}


I get it all apart from: Where it says 'string name = Console.ReadLine();'. Why can't I just put Console.ReadLine() ?
There are 2 'Console.ReadLine();'
Where does the '{0}' come from and what does it mean ?
I am new to C Sharp as you can most likely predict, please either leave a detailed description that is simple for a 14 year old to understand in the forum or IM me at james-b09@hotmail.co.uk.

Thanks,
James :)


Well:

Quote

'string name = Console.ReadLine();'. Why can't I just put Console.ReadLine()?

Console.ReadLine() returns a string. It takes everything you enter in the command line up until you hit "enter" and then gives it to you. The thing is, if you want to use what it's giving you, you have to put it somewhere. So the string name part makes a string for you to store the stuff that Console.ReadLine(); gives you.

Quote

There are 2 'Console.ReadLine();'

The second one is kind of like a "pause" before ending. What happens when you run a C# console application is that it will open the console, run the program until it ends, and then exit. But you probably want to see what actually happens, so you have to find a way to make it pause right before it ends. Well, an easy way to do that is make it wait for input. Now, since you don't care what input you get, you don't store the input anywhere. That's why you can just call Console.ReadLine(); without anything behind it: because you don't want to keep the stuff it gives you back.

Quote

Where does the '{0}' come from and what does it mean ?

The "{0}" is a placeholder for some value. Notice this part:
Console.WriteLine("Hello, {0}! ", name);

There's a comma, and then a variable called "name". Remember earlier, we grabbed what came back from the console and stored it in "name", so when we use "name" in this context, it gets replaced by its value. So the {0} is a placeholder for whatever is inside "name".

You can hold more than one place:
string word1 = "Hello";
string word2 = "World";
Console.WriteLine("{0}, {1}!", word1, word2);

Will print: "Hello, World!". It replaces {0} with what's in word1, and {1} with word2.

Hope that has helped. I suggest picking up a C# for beginners book.
Was This Post Helpful? 3
  • +
  • -

Page 1 of 1