4.4. Text strings
4.4.1. Definition. Reading from keyboard
We have seen how to read strings (Console.ReadLine) and how to display on screen (Console.Write) as well as how to assign a value (=). We can also compare its value, using ==, or create a string from others if we join them with the symbol of the sum (+):
Thus, an example to ask our names and greet us using all these possibilities could be:
/*---------------------------*/
/* C# Example #42: */
/* example42.cs */
/* */
/* Text strings (1) */
/* */
/* Intro to C#, */
/* Nacho Cabanes */
/*---------------------------*/
using System;
public class Example42
{
public static void Main()
{
string greeting = "Hello";
string secondGreeting;
string name, farewell;
secondGreeting = "How are you?";
Console.WriteLine("What is your name?");
name = Console.ReadLine();
Console.WriteLine("{0} {1}", greeting, name);
Console.WriteLine(secondGreeting);
if (name == "Albert")
Console.WriteLine("So you say you are Albert?");
else
Console.WriteLine("So you are not Albert?");
farewell = "Bye " + name + "!";
Console.WriteLine(farewell);
}
}
No comments:
Post a Comment