Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Лаба №1 / books / csharp_ebook.pdf
Скачиваний:
48
Добавлен:
03.03.2016
Размер:
3.69 Mб
Скачать

Programmers Heaven: C# School

A more interactive Hello World Application

Up until now, we have seen a very static hello world application that greets the whole world when it is executed. Let’s now make a more interactive hello world that greets the current user of it. This program will ask the user their name and will greet using his/her name, like 'Hello Faraz' when a user named 'Faraz' runs it. Let’s see the code first:

static void Main(string[] args)

{

Console.Write("Please enter your name: "); string name = Console.ReadLine(); Console.WriteLine

("Hello {0}, Good Luck in C#", name);

}

Author's Note: In the above code, we haven't shown the complete program but only the Main Method to save space. We will

follow this strategy in the rest of the course when appropriate.

Discussing a more interactive Hello World Application

In the first line, we have used another method, Write(), of the Console class. This is similar to the WriteLine() method, discussed in the previous program, but does not change the line after printing the string on the console.

In the second line, we declared a variable of the type string and called it 'name'. Then, we took a line of input from the user through the ReadLine() method of the Console class and stored the result in the 'name' variable. The variables are placeholders in memory for storing data temporarily during the execution of program. Variables can hold different types of data depending on their data-type, e.g., int variables can store integers while string variables can store a string (series) of characters. The ReadLine() method of the Console class (contrary to WriteLine()) reads a line of input given at the Console Window. It returns this input as string data, which we stored in our string variable 'name'.

Author's Note:

A string is implicit data-type in C# contrary to other languages. It starts with small 's'.

In the third line, we printed the name given by user in line 2, along with some greeting text using the WriteLine() method of the Console class. Here we used the substitution parameter {0} to state where in the line the data in the variable 'name' should be written when the WriteLine() method is called.

Console.WriteLine

("Hello {0}, Good Luck in C#", name);

When the compiler finds a substitution parameter, {n}, it replaces it with the (n+1)th variable following the string. The string is delimited with double quotation marks and each parameter is separated by a comma. Hence, in our

29

Programmers Heaven: C# School

case when the compiler finds {0}, it replaces it with (0+1)th, i.e., the 1st variable ('name') following the string. So at run-time, the CLR will read it as:

Console.WriteLine

("Hello Faraz, Good Luck in C#");

if the value of 'name' is Faraz at run-time. Alternatively, it can also be written as:

Console.WriteLine

("Hello " + name + ", Good Luck in C#");

removing the substitution parameter. Here we concatenate (add) the strings together to form a message. (The first approach is similar to C's printf() function while the second is similar to Java's System.out.println() method) When we compile and run this program the output will be:

Please enter your name: Faraz

Hello Faraz, Good Luck in C#

30

Соседние файлы в папке books