Part 2) Reading and writing on console
using System;
class Program {
static void Main() {
Console.WriteLine("Enter your first name");
string firstname = Console.ReadLine();
Console.WriteLine("hello "+firstname);
Console.ReadKey();
}
4) verbatim literal
Verbatim literal converts escape sequences in printable characters
@”/hello/” will print /hello/
using System;
class Program {
static void Main() {
string name=@"/hello/ rashid/";
Console.WriteLine(name);
Console.ReadKey();
}
}
Output:/hello/ rashid/
5)Ternary operator
using System;
class Program {
static void Main() {
int num = 10;
Boolean result = num == 10 ? true : false;// it checks if num==10 print true else
print false
Console.WriteLine(result);
Console.ReadKey();
}
}
Output:true
6)Nullcolessing operator ??
using System;
class Program {
static void Main() {
int? ticketonsale = 10;//? operator used to make value type to nullable
int availticket = ticketonsale ?? 0; //if tickets are null the it will asign 0
otherwise the value of tickets
Console.WriteLine("tickets are {0}",availticket);
Console.ReadKey();
}
}
Output:10
7) difference between parse() and TryParse() function
These methods are used for convert the string into another data type if the data in
correct format then parse() method will convert otherwise it will display error and
TryParse() method will return true or false in a condition
using System;
class Program {
static void Main() {
string name = "20f";
int result=0;
bool a = int.TryParse(name, out result);
Console.WriteLine(a);
Console.WriteLine(result);
Console.ReadKey();
}
}
Now data is not in formate it will return false
Output:False
0