44
C# Program Program 1 //program to understand the concept of get value from user and then print and calculate them using System; class Program { static void Main(string[] args) { int a, b, c; Console.WriteLine("enter any two num"); a = Convert.ToInt32(Console.ReadLine()); b = Convert.ToInt32(Console.ReadLine()); c = a + b; Console.Write("Sum "+c); } } Program 2 //program to understand the concept of method in class using System; class demo { void d() { Console.WriteLine("called"); } public static void Main(string[] args) { demo d1 = new demo(); d1.d(); } } using System;

DocumentC#

Embed Size (px)

DESCRIPTION

Act academy provides different course and provide job with 100% assurance

Citation preview

Page 1: DocumentC#

C# Program

Program 1

//program to understand the concept of get value from user and then print and

calculate them

using System;

class Program

{

static void Main(string[] args)

{

int a, b, c;

Console.WriteLine("enter any two num");

a = Convert.ToInt32(Console.ReadLine());

b = Convert.ToInt32(Console.ReadLine());

c = a + b;

Console.Write("Sum "+c);

}

}

Program 2 //program to understand the concept of method in class

using System;

class demo

{

void d()

{

Console.WriteLine("called");

}

public static void Main(string[] args)

{

demo d1 = new demo();

d1.d();

}

}

using System;

Page 2: DocumentC#

class demo

{

void d()

{

Console.WriteLine("called");

}

public static void Main(string[] args)

{

demo d1 = new demo();

d1.d();

}

}

Program 3 //program to understand the concept of method in class

using System;

class Program

{

void demo()

{

int a, b, c;

Console.WriteLine("enter any two num");

a = Convert.ToInt32(Console.ReadLine());

b = Convert.ToInt32(Console.ReadLine());

c = a + b;

Console.Write("Sum " + c);

}

static void Main(string[] args)

{

Program k = new Program();

k.demo();

}

}

Program 4 //program to understand the concept of instance variable in class

Points to remember

If you want to use any variable in more than one method then it is necessary to declare it under class

using System;

Page 3: DocumentC#

class Program

{

int c;

void demo()

{

int a, b;

Console.WriteLine("enter any two num");

a = Convert.ToInt32(Console.ReadLine());

b = Convert.ToInt32(Console.ReadLine());

c = a + b;

}

void show()

{

Console.WriteLine("value of c" + c);

}

static void Main(string[] args)

{

Program k = new Program();

k.demo();

k.show();

}

}

Program 5 //program to understand the concept of nested method in class

using System;

class Program

{

int a,b;

void assign(int x, int y)

{

a = x;

b = y;

}

int largest()

{

if (a > b)

return a;

else

return b;

}

Page 4: DocumentC#

void show()

{

Console.WriteLine("largest num is " + largest ());

}

static void Main(string[] args)

{

Program k = new Program();

k.assign(4, 5);

k.show();

}

}

Program 6 //program to understand the concept of static variable and static method

using System;

class Program

{

static int a;

static void Main(string[] args)

{

a = 10;

Console.WriteLine(a);

}

}

Program 7 //accessing variable stored in another class ((inheritance not used)

using System;

class one

{

public int t;

}

class two

{

void second()

{

one r = new one();

r.t = 10;

Console.WriteLine(r.t);

}

Page 5: DocumentC#

static void Main(string[] args)

{

two m = new two();

m.second();

}

}

Program 8 //accessing variable stored in another class (inheritance used)

using System;

class one

{

public int t;

}

class two :one

{

void second()

{

t = 10;

Console.WriteLine(t);

}

static void Main(string[] args)

{

two m = new two();

m.second();

}

}

Program 9 //accessing method stored in another class (inheritance not used)

using System;

class one

{

public void show1()

{

Console.WriteLine("iam in one");

}

public void show2()

{

Console.WriteLine("iam in two");

Page 6: DocumentC#

}

}

class two

{

void three()

{

Console.WriteLine("iam in three");

}

void four()

{

Console.WriteLine("iam in four");

}

static void Main(string[] args)

{

one t = new one();

two t1 = new two();

t.show1();

t.show2();

t1.three();

t1.four();

}

}

Program 10 //accessing method stored in another class (inheritance used)

using System;

class one

{

public void show1()

{

Console.WriteLine("iam in one");

}

public void show2()

{

Console.WriteLine("iam in two");

}

}

class two :one

{

void three()

{

Console.WriteLine("iam in three");

Page 7: DocumentC#

}

void four()

{

Console.WriteLine("iam in four");

}

static void Main(string[] args)

{

two t1 = new two();

t1.show1();

t1.show2();

t1.three();

t1.four();

}

}

Program 11 //accessing private variable stored in base class (inheritance used)

using System;

class one

{

int a;

public void assign(int x)

{

a = x;

}

public int re()

{

return a;

}

}

class two :one

{

int k;

void three()

{

k = re();

Console.WriteLine(k);

}

static void Main(string[] args)

{

two t1 = new two();

t1.assign(30);

t1.three();

Page 8: DocumentC#

}

}

Program 12 // object as argument

using System;

class demo

{

int t;

void assign(int x)

{

t = x;

}

void process(demo r1)

{

Console.WriteLine("value" +t);

Console.WriteLine("value "+ r1.t);

}

public static void Main(string[] args)

{

demo y = new demo();

demo y1 = new demo();

y.assign(2);

y1.assign(4);

y.process(y1);

}

}

Program 13

//static variable

using System;

class demo

{

static int t;

Page 9: DocumentC#

void co()

{

t = t + 1;

}

void show()

{

Console.WriteLine(t);

}

public static void Main(string[] args)

{

demo y = new demo();

demo y1 = new demo();

y.co();

y1.co();

y.show();

y1.show();

}

}

Program 14 //static method

using System;

class demo

{

static int t;

int y;

void co()

{

y=++t;

}

void show()

{

Console.WriteLine("value of y"+ y);

}

static void sh()

{

Console.WriteLine("value of t" + t);

}

Page 10: DocumentC#

public static void Main(string[] args)

{

demo a = new demo();

demo b = new demo();

a.co();

b.co();

sh();

a.show();

b.show();

}

}

Program 15 //static method called from another class

using System;

class demo

{

static int t;

int y;

public void co()

{

y = ++t;

}

public void show()

{

Console.WriteLine("value of y" + y);

}

public static void sh()

{

Console.WriteLine("value of t" + t);

}

}

class work

{

public static void Main(string[] args)

{

demo a = new demo();

demo b = new demo();

Page 11: DocumentC#

a.co();

b.co();

demo.sh();

a.show();

b.show();

}

}

Program 16 //static method called from derived class

using System;

class demo

{

static int t;

int y;

public void co()

{

y = ++t;

}

public void show()

{

Console.WriteLine("value of y" + y);

}

public static void sh()

{

Console.WriteLine("value of t" + t);

}

}

class work:demo

{

public static void Main(string[] args)

{

work a = new work();

work b = new work();

a.co();

b.co();

sh();

Page 12: DocumentC#

a.show();

b.show();

}

}

Program 17

// Namespace Declaration

using System;

using csharp;

// Program start class

class demo

{

// Main begins program execution

public static void Main()

{

// Call namespace member

myExample t = new myExample();

t.myPrint();

myPrint();

}

// Potentially ambiguous method

static void myPrint()

{

Console.WriteLine("Not a member of");

}

}

namespace csharp

{

class myExample

{

public void myPrint()

{

Console.WriteLine("This is a member of myExample.");

}

}

}

Page 13: DocumentC#

Program 18 // Namespace Declaration using alias name concept

using System;

using t = csharp.myExample; // alias

// Program start class

class demo

{

// Main begins program execution

public static void Main()

{

// Call namespace member

t.myPrint();

myPrint();

}

// Potentially ambiguous method

static void myPrint()

{

Console.WriteLine("Not a member of");

}

}

namespace csharp

{

class myExample

{

public static void myPrint()

{

Console.WriteLine("This is a member of myExample.");

}

}

}

Page 14: DocumentC#

Program 19 //foreach

using System;

class ForEachLoop

{

public static void Main()

{

string[] names = { "Cheryl", "Joe", "Matt", "Robert" };

int[] t = new int[4];

int i;

foreach (string person in names)

{

Console.WriteLine("{0}", person);

}

Console.WriteLine("enter any four num");

for (i = 0; i < 4; i++)

{

t[i] = Convert.ToInt32(Console.ReadLine());

}

foreach (int y in t)

{

Console.WriteLine(" {0} ", y);

}

}

}

Program 20 // default constructor ,parametrized constructor

using System;

public class Parent

{

string parentString;

public Parent()

{

Console.WriteLine("Parent Constuctor.");

}

public Parent(string myString)

{

parentString = myString;

Console.WriteLine(parentString);

}

public void print()

{

Page 15: DocumentC#

Console.WriteLine("I'm a Parent Class.");

}

public static void Main()

{

Parent k = new Parent();

Parent k1 = new Parent("ok");

k1.print();

}

}

Program 21 //use of base and new keyword on variable

using System;

public class Parent

{

public int i;

}

class second:Parent

{

new int i;

public void assign(int k,int p)

{

base.i = k;

i = p;

}

void show()

{

Console.WriteLine(base.i);

Console.WriteLine(i);

}

public static void Main()

{

second k = new second();

k.assign(6,8);

k.show();

}

}

Page 16: DocumentC#

Program 22 //use of base and new keyword on methods

using System;

public class Parent

{

public int i;

public void show()

{

Console.WriteLine(i);

}

}

class second:Parent

{

new int i;

public void assign(int k,int p)

{

base.i = k;

i = p;

}

new void show()

{

base.show();

Console.WriteLine(i);

}

public static void Main()

{

second k = new second();

k.assign(6,8);

k.show();

}

}

Program 23 //method overriding

using System;

public class Parent

{

public void show()

{

Console.WriteLine("iam in parent");

}

}

Page 17: DocumentC#

class second:Parent

{

new void show()

{

Console.WriteLine("iam in second");

}

public static void Main()

{

second k = new second();

k.show();

k.show();

}

}

Program 24 //method overloading

using System;

public class Parent

{

public void show()

{

Console.WriteLine("iam in parent");

}

public void show(int y)

{

int k;

k = y * y;

Console.WriteLine(k);

}

public void show(int m,int n)

{

int r;

r = m + n;

Console.WriteLine(r);

}

Page 18: DocumentC#

public static void Main()

{

Parent t = new Parent();

t.show();

t.show(4);

t.show(4,6);

}

}

Program 25 //Destructor

using System;

public class Parent

{

static int t;

Parent()

{

t = ++t;

Console.WriteLine("Constructor called " + t);

}

~Parent()

{

Console.WriteLine("Destructor called " + t);

t = --t;

}

public static void Main()

{

Parent t = new Parent();

Parent t1 = new Parent();

Parent t2 = new Parent();

Parent t3 = new Parent();

Parent t4 = new Parent();

}

}

Program 26

Page 19: DocumentC#

//copy constructor

using System;

public class Parent

{ int r;

Parent()

{

r = 10;

}

Parent(Parent k)

{

r = k.r;

}

void show()

{

Console.WriteLine(r);

}

public static void Main()

{

Parent t = new Parent();

Parent t1 = new Parent(t);

t.show();

t1.show();

}

}

Program 27 //example without this

using System;

public class Parent

{

int h, w;

public void assign(int x, int y)

{

h = x;

w = y;

}

Page 20: DocumentC#

int call()

{

return h * w;

}

public static void Main()

{

Parent t = new Parent();

Parent t1 = new Parent();

int a, b;

Console.WriteLine("enter any two num");

a =Convert.ToInt32 ( Console.ReadLine());

b = Convert.ToInt32(Console.ReadLine());

t.assign(a,b);

Console.WriteLine("enter any two num");

a = Convert.ToInt32(Console.ReadLine());

b = Convert.ToInt32(Console.ReadLine());

t1.assign(a, b);

Console.WriteLine("called" + t.call());

Console.WriteLine("called" + t1.call());

}

}

Program 28 //example of this

using System;

public class Parent

{

int h, w;

public void assign(int x, int y)

{

this.h = x;

this.w = y;

}

int call()

{

return this.h * this.w;

}

Page 21: DocumentC#

public static void Main()

{

Parent t = new Parent();

Parent t1 = new Parent();

int a, b;

Console.WriteLine("enter any two num");

a =Convert.ToInt32 ( Console.ReadLine());

b = Convert.ToInt32(Console.ReadLine());

t.assign(a,b);

Console.WriteLine("enter any two num");

a = Convert.ToInt32(Console.ReadLine());

b = Convert.ToInt32(Console.ReadLine());

t1.assign(a, b);

Console.WriteLine("called" + t.call());

Console.WriteLine("called" + t1.call());

}

}

Program 29 //array intialization example

using System;

public class Parent

{

public static void Main()

{

int[] t ={ 3, 45, 5 };

for (int i = 0; i < t.Length; i++)

{

Console.WriteLine(t[i]);

}

Page 22: DocumentC#

}

}

Program 30

//jagged array

using System;

public class Parent

{

public static void Main()

{

int[][] jk = new int[3][];

jk[0] = new int[2];

jk[1] = new int[3];

jk[2] = new int[4];

int i;

Console.WriteLine("enter any two num for first row");

for (i = 0; i < 2; i++)

{

jk[0][i] = Convert.ToInt32(Console.ReadLine());

}

Console.WriteLine("enter any three num for second row");

for (i = 0; i < 3; i++)

{

jk[1][i] = Convert.ToInt32(Console.ReadLine());

}

Console.WriteLine("enter any four num for third row");

for (i = 0; i < 4; i++)

{

jk[2][i] = Convert.ToInt32(Console.ReadLine());

}

Page 23: DocumentC#

Console.WriteLine("the elements in first row is");

for (i = 0; i < 2; i++)

{

Console.WriteLine(jk[0][i]);

}

Console.WriteLine("the elements in second row is");

for (i = 0; i < 3; i++)

{

Console.WriteLine(jk[1][i]);

}

Console.WriteLine("the elements in third row is");

for (i = 0; i < 4; i++)

{

Console.WriteLine(jk[2][i]);

}

}

}

Program 31

//String functions

using System;

public class Parent

{

public static void Main()

{

string a1 = "hello act";

string a2 = string.Copy(a1);

string a3 = "work is worship";

string a4,a5;

string k1, k2;

int r, m, j;

Console.Write("length of string" + a1.Length);

k1 = a1.ToLower();

k2 = a1.ToUpper();

Console.WriteLine("string in lowercase" + k1);

Console.WriteLine("string in uppercase" + k2);

Page 24: DocumentC#

for (int i = 0; i < a1.Length; i++)

{

Console.WriteLine(a1[i]);

}

if (a1 == a2)

{

Console.WriteLine("both a1 and a2 are same");

}

else

Console.WriteLine("both a1 and a2 are not same");

if (a1 == a3)

Console.WriteLine("both a1 and a3 are same");

else

Console.WriteLine("both a1 and a3 are not same");

r = a1.CompareTo(a3);

if (r == 0)

{

Console.WriteLine("both a1 and a3 are same");

}

else if (r < 0)

{

Console.WriteLine(" a1 is smaller");

}

else

Console.WriteLine("a2 is greater");

a4 = "one two three four one";

m = a4.IndexOf("one");

j = a4.LastIndexOf("one");

Console.WriteLine("index of first occurence of one is" + m);

Console.WriteLine("index of last occurence of one is" + j);

a5 = a1.Substring(2, 4);

Console.WriteLine(a5);

}

}

Program 32 //string array

using System;

Page 25: DocumentC#

public class Parent

{

public static void Main()

{

string[] k = new string[4];

int i;

Console.WriteLine("enter four names");

for (i = 0; i < k.Length ; i++)

{

k[i] = Console.ReadLine();

}

Console.WriteLine("the name u have entered are");

foreach (string t in k)

{

Console.WriteLine(t);

}

}

}

Program 33 //String

using System;

public class Parent

{

public static void Main()

{

string[] a1 ={ "one", "two", "three", "two", "one" };

foreach (string s in a1)

{

switch (s)

{

case "one":

Console.Write(1);

break;

case "two":

Console.Write(2);

break;

case "three":

Console.Write(3);

break;

}

}

}

Page 26: DocumentC#

}

Program 34

//String

using System;

public class Parent

{

public static void Main()

{

string[] a1 = new string[4];

for (int i = 0; i < 4; i++)

{

a1[i] = Console.ReadLine();

}

foreach (string s in a1)

{

switch (s)

{

case "one":

Console.Write(1);

break;

case "two":

Console.Write(2);

break;

case "three":

Console.Write(3);

break;

default:

Console.Write("invalid");

break;

}

}

}

}

Program 35 //passed by value

using System;

public class Parent

{

public void one(int i, int j)

{

i = i + j;

j = -j;

Page 27: DocumentC#

Console.WriteLine("value in function of one");

Console.WriteLine(i);

Console.WriteLine(j);

}

public static void Main()

{

Parent p = new Parent();

int a = 15, b = 20;

Console.WriteLine("before calling function the value of a is {0} and

b is {1}", a, b);

p.one(a, b);

Console.WriteLine("a and b after call" + a + " " + b);

}

}

Program 36

//passed by reference

using System;

public class Parent

{

public int a, b;

public Parent(int i, int j)

{

a = i;

b = j;

}

public void one(Parent t)

{

t.a = t.a + t.b;

t.b = -t.b;

}

public static void Main()

{

Parent p = new Parent(15,20);

Console.WriteLine("before calling function the value of a is {0} and

b is {1}", p.a, p.b);

p.one(p);

Console.WriteLine("a and b after call" +p.a + " " + p.b);

}

}

Program 37

//ref parameter modifier causes c# to create a call by reference

Page 28: DocumentC#

using System;

public class Parent

{

public void a1(ref int i)

{

i = i * i;

}

public static void Main()

{

Parent t = new Parent();

int a = 10;

Console.WriteLine("value before calling function " + a);

t.a1(ref a);

Console.WriteLine("value after calling function" + a);

}

}

Program 38

//out (sometimes you will want to use a reference parameter to receive a

value from a method but not pass in a value)

using System;

public class Parent

{

public int parts(double n, out double frac)

{

int whole;

whole = (int)n;

frac = n - whole;

return whole;

}

public static void Main()

{

Parent t = new Parent();

int i;

double f;

i = t.parts(10.125, out f);

Console.WriteLine("Integer fraction is " + i);

Console.WriteLine("Fractional part is " + f);

Page 29: DocumentC#

}

}

Program 39

//the params modifier is used to declare an array parameter that will be able

to recive zero or more arguments.the number of elements in the array will be

equal to the number of arguments passed to the method.

using System;

class Program

{

public int minval(params int[] nums)

{

int m;

if (nums.Length == 0)

{

Console.WriteLine("Error : no arguments");

return 0;

}

m = nums[0];

for (int i = 1; i < nums.Length; i++)

if (nums[i] < m)

m = nums[i];

return m;

}

static void Main(string[] args)

{

Program ob = new Program();

int min;

int a = 10, b = 20;

//call with two values

min = ob.minval(a, b);

Console.WriteLine("minimum is " + min);

//call with 3 values

min = ob.minval(a, b, -1);

Console.WriteLine("minimum is " + min);

//call with 5 values

min = ob.minval(18, 23, 3, 14, 25);

Console.WriteLine("minimum is " + min);

//can call with an int array too

int[] ar ={ 45, 56, 67, 2 };

min = ob.minval(ar);

Console.WriteLine("minimum is " + min);

}

Page 30: DocumentC#

}

Program 40

//returning object

using System;

class Program

{

int pg, pr;

Program()

{

}

public Program(int x,int y)

{

pg = x;

pr = y;

}

Program task(Program m)

{

Program t=new Program();

t.pg = pg + m.pg;

t.pr = pr + m.pr;

return t;

}

void show()

{

Console.WriteLine("pages" + pg);

Console.WriteLine("price" + pr);

}

static void Main(string[] args)

{

Program k1 = new Program(200,300);

Program k2 = new Program(400,500);

Program k3= k1.task(k2);

Console.WriteLine("k1 object assign");

k1.show();

Console.WriteLine("k2 object assign");

k2.show();

Console.WriteLine("k3 object assign");

k3.show();

}

}

Page 31: DocumentC#

Program 41

//returning an array

using System;

class Program

{

public int[] assign(out int k)

{

int[] t = new int[4];

k = 4;

Console.WriteLine("enter four num");

for (int i = 0; i < 4; i++)

{

t[i] = Convert.ToInt16(Console.ReadLine());

}

return t;

}

static void Main(string[] args)

{

int[] r;

Program t = new Program();

int m;

r = t.assign(out m);

Console.WriteLine("u have entered ");

for (int i = 0; i < m; i++)

{

Console.WriteLine(r[i]);

}

}

}

Program 42

//static constructor

Program 43

//static class exp1

using System;

static class Program

{

Page 32: DocumentC#

static public void one()

{

Console.WriteLine("iam in one method of static class");

}

static public void two()

{

Console.WriteLine("iam in second method of static class");

}

static void Main(string[] args)

{

one();

two();

}

}

Program 44

//static class exp2

using System;

static class Program

{

static public void one()

{

Console.WriteLine("iam in one method of static class");

}

static public void two()

{

Console.WriteLine("iam in second method of static class");

}

}

class work

{

static void Main(string[] args)

{

Program.one();

Program.two();

}

}

Program 45

//virtual method is a method that is declared as virtual in base class and

redefined in one or more derived classes.

Page 33: DocumentC#

using System;

class base1

{

public virtual void who()

{

Console.WriteLine("iam in base");

}

}

class derived1:base1

{

public override void who()

{

Console.WriteLine("iam in derived1");

}

}

class derived2:base1

{

public override void who()

{

Console.WriteLine("iam in derived2");

}

}

class work

{

static void Main(string[] args)

{

base1 a1=new base1();

derived1 a2=new derived1();

derived2 a3=new derived2();

base1 r1;

r1=a1;

r1.who();

r1 = a2;

r1.who();

r1 = a3;

r1.who();

}

}

Program 46

//when a virtual method is not overridden the base class method is used

using System;

class base1

{

public virtual void who()

{

Console.WriteLine("iam in base");

}

}

Page 34: DocumentC#

class derived1:base1

{

public override void who()

{

Console.WriteLine("iam in derived1");

}

}

class derived2:base1

{

}

class work

{

static void Main(string[] args)

{

base1 a1=new base1();

derived1 a2=new derived1();

derived2 a3=new derived2();

base1 r1;

r1=a1;

r1.who();

r1 = a2;

r1.who();

r1 = a3;

r1.who();

}

}

Program 47

//In the case of multilevel hierarchy if a derived class does not override a

virtual method then while moving up the hierarchy the first override of the

method that is encountered is the one executed

using System;

class base1

{

public virtual void who()

{

Console.WriteLine("iam in base");

}

}

class derived1:base1

{

public override void who()

{

Console.WriteLine("iam in derived1");

}

}

class derived2:derived1

Page 35: DocumentC#

{

}

class derived3 : derived2

{

}

class work

{

static void Main(string[] args)

{

derived3 a3=new derived3();

base1 r1;

r1 = a3;

r1.who();

}

}

Program 48

//to prevent a class from being inherited ,precede its decleration with

sealed

using System;

sealed class base1

{

}

class derived1:base1

{

static void Main(string[] args)

{

}

}

Program 49

//Boxes causes the value of a value type to be stored in an object instance

//unboxing is the process of retrieving a value from a boxed object

using System;

class demo

{

static void Main(string[] args)

{

int x;

object ob;

x = 10;

ob = x; //box x to an object

int y = (int)ob; //unbox object into an int

Page 36: DocumentC#

Console.WriteLine(y);

}

}

Program 50

//Boxing also occurs when passing value

using System;

class demo

{

static void Main(string[] args)

{

int x;

x = 10;

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

x = demo.st(x);

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

}

static int st (object r)

{

int y;

y=(int)r *(int)r;

return y;

}

}

Program 51

//using object as a universal data type

using System;

class demo

{

static void Main(string[] args)

{

object[] k = new object[5];

k[0] = 3;

k[1] = 2.5;

k[2] = true;

Page 37: DocumentC#

k[3] = "hello";

k[4] = 'a';

for(int t=0;t<k.Length ;t++)

{

Console.WriteLine(k[t]);

}

}

}

Program 52

//abstract class

//sometimes you will want to create a base class that defines only a

generalized form that will be shared by all its derived classes,leaving it to

each derived class to fill in the details.

//such a class determines the nature of the methods that the derived classes

must implement,but does not itself provide an implementation of one or more

of these methods

using System;

abstract class demo

{

public void show()

{

Console.WriteLine("iam in show method of abstract class");

}

public abstract void one();

}

class demo2:demo

{

public override void one()

{

Console.WriteLine("one method define in a demo2 class");

}

void two()

{

Console.WriteLine("iam in two method of demo2 class");

}

static void Main(string[] args)

{

demo2 r = new demo2();

r.one();

r.show();

r.two();

}

}

Program 53

Page 38: DocumentC#

//operator overloading

using System;

public class Program

{

int x, y, z;

public Program()

{

x = y = z = 0;

}

public Program(int i, int j, int k)

{

x = i;

y = j;

z = k;

}

public static Program operator +(Program a, Program b)

{

Program c = new Program();

c.x = a.x + b.x;

c.y = a.y + b.y;

c.z = a.y + b.y;

return c;

}

public static Program operator -(Program a, Program b)

{

Program c = new Program();

c.x = a.x - b.x;

c.y = a.y - b.y;

c.z = a.y - b.y;

return c;

}

public void show()

{

Console.WriteLine(x + " , " + y + " , " + z);

}

}

class second

{

static void Main(string[] args)

{

Program n1 = new Program(1,2,3);

Program n2 = new Program(10,10,10);

Program n3 = new Program();

Console.WriteLine("here is n1");

n1.show();

Console.WriteLine("here is n2");

n2.show();

n3 = n1 + n2;

Console.WriteLine("Result of n1 + n2");

Page 39: DocumentC#

n3.show();

n3 = n1 + n2+n3;

Console.WriteLine("Result of n1 + n2 + n3");

n3.show();

n3 = n3 - n1;

Console.WriteLine("Result of n3 - n1");

n3.show();

n3 = n3 - n2;

Console.WriteLine("Result of n3 - n2");

n3.show();

}

}

Program 54

// Property

class Employee

{

int empid;

string name;

double basic;

public int Empid

{

set

{

empid = value;

}

get

{

return empid;

}

}

public string Name

{

set

{

name = value;

}

get

{

Page 40: DocumentC#

return name;

}

}

public double Basic

{

set

{

basic = value;

}

get

{

return basic;

}

}

public double Salary

{

get

{

return basic * 2.9;

}

}

public static void Main()

{

Employee e = new Employee();

e.Empid = 345;

e.Name = "Rakesh Verma";

e.Basic = 9000;

System.Console.WriteLine("Salary of {0} is {1}", e.Name, e.Salary);

}

}

Program 55

//pointer

class PointerTest

{

unsafe static void Change(int* n)

{

*n += 10;

}

public static void Main()

{

int k = 8;

unsafe

{

Change(&k);

}

System.Console.WriteLine(k);

}

}

Page 41: DocumentC#

Compile this program using

csc /unsafe a1.cs

run

a1

Program 55

//delegate

class Delegate

{

delegate void MyDelGate();

public static void show1()

{

System.Console.WriteLine("CAlling from Show1");

}

public static void show2()

{

System.Console.WriteLine("Calling from Show2");

}

public static void Main()

{

MyDelGate d1 = new MyDelGate(show1);

MyDelGate d2 = new MyDelGate(show2);

MyDelGate d3 = d1 + d2;

d3();

}

}

Program 55

//interface example

using System;

public interface test1

{

void one();

void two();

}

class demo:test1

{

public void one()

{

Console.WriteLine("hello iam in one");

Page 42: DocumentC#

}

public void two()

{

Console.WriteLine("hello iam in two");

}

public static void Main()

{

demo d = new demo();

d.one();

d.two();

}

}

Program 56

//interface can be inherited

using System;

public interface test1

{

void one();

void two();

}

public interface test2:test1

{

void three();

}

class demo:test2

{

public void one()

{

Console.WriteLine("hello iam in one");

}

public void two()

{

Console.WriteLine("hello iam in two");

}

public void three()

{

Console.WriteLine("hello iam in three");

}

public static void Main()

{

demo d = new demo();

d.one();

d.two();

Page 43: DocumentC#

d.three();

}

}

Program 57

//delegate is an object that can refer to a method.thus when you create a

delegate you are creating an object that can hold a reference to a method

using System;

delegate string st(string k);

class Program

{

static string replacespaces(string a)

{

Console.WriteLine("replaces space with hyphens");

return a.Replace (' ','-');

}

static string removespaces(string a)

{

string temp = "";

int i;

Console.WriteLine("remove spaces");

for (i = 0; i < a.Length; i++)

{

if (a[i] != ' ')

{

temp += a[i];

}

}

return temp;

}

static string reverse(string a)

{

string temp = "";

int i, j;

Console.WriteLine("reversing string");

for (j = 0, i = a.Length - 1; i >= 0; i--, j++)

{

temp += a[i];

}

return temp;

}

static void Main(string[] args)

{

st mk = new st(replacespaces);

Page 44: DocumentC#

string t;

//call methods through the delegate

t = mk("This is a Test");

Console.WriteLine("REsulting string " + t);

mk = new st(removespaces);

t = mk("This is a Test");

Console.WriteLine("Resulting String" + t);

mk = new st(reverse);

t = mk("This is a Test");

Console.WriteLine("Resulting String " + t);

}

}