0% found this document useful (0 votes)
33 views4 pages

Program in C# To Demonstrate Escape Sequences and Verbatim Literal

The document demonstrates C# programs using escape sequences, verbatim string literals, boxing and unboxing, and variable scope. It includes code samples showing the use of escape sequences and tabs in output, verbatim string literals that span multiple lines, boxing an integer value into an object and unboxing back, and demonstrating the scope of variables within blocks.

Uploaded by

Shobha Kumar
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)
33 views4 pages

Program in C# To Demonstrate Escape Sequences and Verbatim Literal

The document demonstrates C# programs using escape sequences, verbatim string literals, boxing and unboxing, and variable scope. It includes code samples showing the use of escape sequences and tabs in output, verbatim string literals that span multiple lines, boxing an integer value into an object and unboxing back, and demonstrating the scope of variables within blocks.

Uploaded by

Shobha Kumar
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/ 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