Program in C# To Demonstrate Escape Sequences and Verbatim Literal

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Program in C# to demonstrate escape sequences and verbatim literal.

Escape Sequence
using System;

public class DemoEscapeSequence

public static void main()

Console.WriteLine(“This line\t contains two\ttabs”);

Console.WriteLine(“This statement\n contains a new line”);

Console.WriteLine(“This statement sounds” + “three alerts\a\a\a”);

// Demonstrate verbatim string literals.


using System;

class Verbatim {

static void Main() {

Console.WriteLine(@"This is a verbatim string literal that spans several lines. ");


Console.WriteLine(@"Here is some tabbed output: 1 2 3 4 5 6 7 8 ");
Console.WriteLine(@"Programmers say, ""I like C#."""); } }
Program in C# to demonstrate Boxing and unBoxing

// A simple boxing/unboxing example.

using System;

class BoxingDemo {

static void Main() {

int x;

object obj;

x = 10;

obj = x; // box x into an object

int y = (int)obj; // unbox obj into an int

Console.WriteLine(y); }

Boxing

class TestBoxing {

static void Main() {

int i = 100;

// Boxing copies the value of i into object o.

object o = i;

// Change the value of i.

i = 200;

// The change in i doesn't affect the value stored in o.

System.Console.WriteLine("The value-type value = {0}", i);

System.Console.WriteLine("The object-type value = {0}", o); }

}
Unboxing

class TestUnboxing {

static void Main() {

int i = 100;

object o = i;

// implicit boxing

try { int j = (short)o;

// attempt to unbox

System.Console.WriteLine("Unboxing OK."); }

catch (System.InvalidCastException e)

{ System.Console.WriteLine("{0} Error: Incorrect unboxing.", e.Message);

}
Program in C# to demonstrate scope of a variable.

// Demonstrate block scope.

using System;

class ScopeDemo {

static void Main() {

int x; // known to all code within Main()

x = 10;

if(x == 10) { // start new scope

int y = 20; // known only to this block

// x and y both known here.

Console.WriteLine("x and y: " + x + " " + y);

x = y * 2;

// y = 100; // Error! y not known here.

// x is still known here.

Console.WriteLine("x is " + x);

You might also like