KEMBAR78
Part 2) Reading and Writing On Console: Program | PDF | Boolean Data Type | Software Engineering
0% found this document useful (0 votes)
70 views2 pages

Part 2) Reading and Writing On Console: Program

This document contains code examples demonstrating various C# concepts: 1) Reading user input from the console using Console.ReadLine() and writing output using Console.WriteLine(). 2) Using a verbatim string literal @ to print a string containing escape characters literally. 3) The ternary operator ?: to assign a boolean value based on comparing a variable to a value. 4) The null-coalescing operator ?? to assign a non-null value if a nullable type is null. 5) The difference between the Parse() and TryParse() methods, where Parse() throws an exception on invalid input and TryParse() returns a boolean.

Uploaded by

rashid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views2 pages

Part 2) Reading and Writing On Console: Program

This document contains code examples demonstrating various C# concepts: 1) Reading user input from the console using Console.ReadLine() and writing output using Console.WriteLine(). 2) Using a verbatim string literal @ to print a string containing escape characters literally. 3) The ternary operator ?: to assign a boolean value based on comparing a variable to a value. 4) The null-coalescing operator ?? to assign a non-null value if a nullable type is null. 5) The difference between the Parse() and TryParse() methods, where Parse() throws an exception on invalid input and TryParse() returns a boolean.

Uploaded by

rashid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

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

You might also like