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

1)WAP to show a boxing and un-boxing of types .

using System; class prog1 { public static void Main() { int m=100; object o=m; m=200; Console.WriteLine(o); Console.WriteLine(m); int n=(int)o; Console.WriteLine(n); } }

Output: 100 200 100

2)WAP to use single , multi-dimensional and jagged array.


using System; class prog2 { public static void Main() { int[] m=new int[3]; int[,] n=new int[2,2]; int[][] o=new int[2][]; o[0]=new int[2]; o[1]=new int[1]; Console.WriteLine("enter value"); for(int i=0;i<2;i++) m[i]=int.Parse(Console.ReadLine()); Console.WriteLine("enter value"); for(int i=0;i<2;i++) { for(int j=0;j<2;j++) { n[i,j]=int.Parse(Console.ReadLine()); } } Console.WriteLine("enter value"); for(int i=0;i<o.Length;i++) { for(int j=0;j<o[i].Length;i++) { o[i][j]=int.Parse(Console.ReadLine()); } }

for(int i=0;i<2;i++) Console.WriteLine(m[i]); for(int i=0;i<2;i++) { for(int j=0;j<2;j++) { Console.WriteLine(n[i,j]); } Console.WriteLine(); }

for(int i=0;i<o.Length;i++) { for(int j=0;j<o[i].Length;i++) { Console.WriteLine(o[i][j]); } Console.WriteLine(); }

} }

Output: enter the value 123 enter the value 12 34 enter the value 12 1

123 12 34 12 1

3)WAP to show method overriding in a class.


using System; class p { int m=3; public virtual void display() { Console.WriteLine(m); } } class q:p { int n=6; public override void display() { Console.WriteLine(n); } } class prog3 { public static void Main() { q o1=new q(); o1.display(); } }

output: 6

4)WAP to pass parameter by ref and by out parameter


using System; class p { public static void swap(ref int x,ref int y) { int temp=x; x=y; y=temp; } } class prog4 { public static void Main() { int m=3,n=4; Console.WriteLine("Before Swapping"); Console.WriteLine(m+""+n); p.swap(ref m,ref n); Console.WriteLine("After Swapping"); Console.WriteLine(m+""+n); } }

output: Before Swapping 34 After Swapping 43

5)WAP to show overloading of binary operator.


using System; class ch { int x,y; public ch(int m,int n) { x=m; y=n; } public ch() { } public static ch operator + (ch c1,ch c2) { ch c3 =new ch(); c3.x = c1.x + c2.x; c3.y = c1.y + c2.y; return (c3); } public void display() { Console.WriteLine(x+" "+y); } } class prog5 { public static void Main() { ch q1,q2,q3; q1=new ch(4,5); q2=new ch(4,5); q3=q1+q2; q3.display(); } }

output: 8 10

6)WAP to show overloading of boolean operator


using System; class ch { int x; public ch(int m) { x=m; } public ch() { } public static bool operator ! (ch c1) { if(c1.x==0) return(true); else return(false); } public void display() { Console.WriteLine(x); } } class prog6 { public static void Main() { ch q1,q2; q1=new ch(4); q2=new ch(0); Console.WriteLine(!q1); Console.WriteLine(!q2); } }

output: False True

7)WAP to implement interface.


using System; interface disp { void display(); } class ch:disp { int x; public ch(int m) { x=m; } public void display() { Console.WriteLine(x); } } class prog7 { public static void Main() { ch q1=new ch(5); q1.display(); disp d=(disp)q1; d.display(); disp d1=q1 as disp; d1.display(); } }

output: 5 5 5

8)WAP to implement Polymorphism


using System; class ch { int x; public ch(int m) { x=m; } public virtual void display() { Console.WriteLine("base:"+x); } } class ch1:ch { int x1; public ch1(int m,int n):base(m) { x1=n; } public override void display() { Console.WriteLine("derived"+x1); } } class prog8 { public static void Main() { ch1 q1=new ch1(5,6); q1.display(); ch q2; q2=new ch1(4,5); q2.display(); ch1 q3=new ch1(5,8); ch q4=(ch)q3; q4.display(); } }

output: derived6 derived5 derived8

9)WAP to implement multicasting of events .


using System; delegate void delegates(); class ch { public static void display() { Console.WriteLine("base"); } } class ch1 { public static void print() { Console.WriteLine("derived"); } } class prog9 { public static void Main() { delegates d=new delegates(ch.display); delegates d1=new delegates(ch1.print); delegates d2=d+d1; d2(); } }

output: base: derived:

10)WAP to implement anonymous methods od a delegate.


using System; using System.Collections.Generic; class prog10 { static void Main() { try{ List<int> values = new List<int>() { 1, 1, 1, 2, 3 ,12};

List<int> res = values.FindAll(delegate(int element) { if (element > 10) { throw new ArgumentException("element"); } if (element == 8) { throw new ArgumentException("element"); } return element > 1; }); foreach (int val in res) { Console.WriteLine(val); } }catch(ArgumentException a) { Console.WriteLine(a); } } }

output: System.ArgumentException: element at prog10.<Main>b__1(Int32 element) at System.Collections.Generic.List`1.FindAll(Predicate`1 match) at prog10.Main()

11)WAP to implement multicasting of events.


using System; public delegate void EventHandler(); class prog11 { public static event EventHandler show; static void Main() { show += new EventHandler(Dog); show += new EventHandler(Cat); show += new EventHandler(Mouse); show.Invoke(); } static void Cat() { Console.WriteLine("Cat"); } static void Dog() { Console.WriteLine("Dog"); } static void Mouse() { Console.WriteLine("Mouse"); } }

output: Dog Cat Mouse

12)WAP to show indexer without an array.


class SampleCollection<T> { private T[] arr = new T[100]; public T this[int i] { get { return arr[i]; } set { arr[i] = value; } } } class prog12 { static void Main(string[] args) { SampleCollection<string> stringCollection = new SampleCollection<string>(); stringCollection[0] = "Hello, World"; System.Console.WriteLine(stringCollection[0]); } }

output: Hello, World

13)WAP to show a read-only property.


using System; class prog13 { static void Main() { Manager manager = new Manager(); Console.WriteLine(manager.GetStartup()); } } class Manager { readonly DateTime _startup; public Manager() { this._startup = DateTime.Now; } public DateTime GetStartup() { return this._startup; } }

output: 21-10-2012 18:26:56

14)WAP to show narrowing conversion in type casting .


using System; class prog14 { static void Main() { int value = 10; value += DateTime.Today.Day; Console.WriteLine(value); } }

output: 31

You might also like