Console class provides ReadLine method to read input from console.
C#: Read input from console
Console class provides ReadLine method to read input from console.
ReadLine method reads the information from console and return the input in string format. String name = Console.ReadLine(); Program.cs using System ; class Program { static void Main ( string [] args) { Console.WriteLine("Welcome to C#, Please Enter Basic Informaiton"); Console.WriteLine("Enter Your Name"); String name = Console.ReadLine(); Console.WriteLine("Enter Your Age"); String age = Console.ReadLine(); Console.WriteLine("Hello " + name + " You are " + age + " years old"); } }
Output Welcome to C#, Please Enter Basic Informaiton Enter Your Name JJ TAM Enter Your Age Hello JJ TAM You are 26 years old
Place Holder Syntax
While concatenating the strings, place holder syntax is very convenient than using + operator. Following is the example of place holder syntax. Console.WriteLine("Hello {0}. You are {1} years old", name, age); Above statement is equivalent of following statement. Console.WriteLine("Hello " + name + " You are " + age + " years old"); Program.cs using System ; class Program { static void Main ( string [] args) { Console.WriteLine("Welcome to C#, Please Enter Basic Informaiton"); Console.WriteLine("Enter Your Name"); String name = Console.ReadLine(); Console.WriteLine("Enter Your Age"); String age = Console.ReadLine(); Console.WriteLine("Hello {0}", name); Console.WriteLine("You are {0} years old", age); Console.WriteLine("Hello {1}.
You are {0} years old", name, age); } } Output Welcome to C#, Please Enter Basic Informaiton Enter Your Name JJ TAM Enter Your Age Hello JJ TAM You are 26 years old Hello 26. You are JJ TAM years old Press any key to continue...
C#: Built in Types
C# support almost all kind of data types to work with. All data types are represented as classes in System namespace. Data types can be categorized into following types. a.
Boolean : bool b. Integer: byte, sbyte, int, uint, short, ushort, long, ulong c. Float: double, float, decimal d. Character : char e. String Following table summarizes all the data types
Short Name | Class | Min value | Max value |
byte | Byte | | |
sbyte | SByte | -128 | |
int | Int32 | -2,147,483,648 | 2,147,483,647 |
uint | UInt32 | | 4294967295 |
short | Int16 | -32,768 | 32767 |
ushort | UInt16 | | 65535 |
long | Int64 | - 9223372036854775808 | 9223372036854775807 |
ulong | UInt64 | | 18446744073709551615 |
float | Single | -3.402823e38 | 3.402823e38 |
double | Double | -1.79769313486232e308 | 1.79769313486232e308 |
decimal | Decimal | 1.0 10e28 | 7.9 10e28 |
char | Char | Represent Unicode character. |
string | String | Sequence of characters |
Note a.
string | String | Sequence of characters |
Note a.
Suffix Float variables by f. Ex: float floatVar = 18.18f; b. Suffix Decimal variables by m. Ex: decimal decimalVar = 20.20m; Program.cs using System ; class Program { static void Main ( string [] args) { byte byteVar = ; sbyte sbyteVar = ; int intVar = ; uint uintVar = ; short shortVar = ; ushort ushortVar = ; long longVar = ; ulong ulongVar = ; float floatVar = 18.18f ; double doubleVar = 19.19 ; decimal decimalVar = 20.20 m; char charVar = 'S'; string stringVar = "Hello World"; Console.WriteLine("byteVar = {0}", byteVar); Console.WriteLine("sbyteVar = {0}", sbyteVar); Console.WriteLine("intVar = {0}", intVar); Console.WriteLine("uintVar = {0}", uintVar); Console.WriteLine("shortVar = {0}", shortVar); Console.WriteLine("ushortVar = {0}", ushortVar); Console.WriteLine("longVar = {0}", longVar); Console.WriteLine("ulongVar = {0}", ulongVar); Console.WriteLine("floatVar = {0}", floatVar); Console.WriteLine("doubleVar = {0}", doubleVar); Console.WriteLine("decimalVar = {0}", decimalVar); Console.WriteLine("charVar = {0}", charVar); Console.WriteLine("stringVar = {0}", stringVar); } }
C#: Strings and Escape Sequences
String is a Collection of characters. Strings are enclosed in double quotes.