SlideShare a Scribd company logo
1 of 44
Download to read offline
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;
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


using System;

If you want to use any variable in more than one method then it is necessary to declare it
under class
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;
}
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
using System;
class one
{
public int t;
}
class two
{
void second()
{
one r = new one();
r.t = 10;
Console.WriteLine(r.t);
}

((inheritance not used)
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");
}
}
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");
}
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();
}
}

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;
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);
}
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();
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();
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.");
}
}
}
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.");
}
}
}
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()
{
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();
}
}
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");
}
}
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);
}
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
//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;
}
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;
}
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]);
}
}
}

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());
}
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
string
string
string
int r,

a2 = string.Copy(a1);
a3 = "work is worship";
a4,a5;
k1, k2;
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);
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;
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;

}
}
}
}

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;
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
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);
}
}

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);

}
}

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();

}
}
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
{
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.
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");
}
}
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
{
}
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
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;
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
//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
{
Program
Program
Program

Main(string[] args)
n1 = new Program(1,2,3);
n2 = new Program(10,10,10);
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");
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
{
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);
}
}
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");
}
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();
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);
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);
}
}

More Related Content

What's hot

Java Practical File Diploma
Java Practical File DiplomaJava Practical File Diploma
Java Practical File Diplomamustkeem khan
 
Java_practical_handbook
Java_practical_handbookJava_practical_handbook
Java_practical_handbookManusha Dilan
 
Final JAVA Practical of BCA SEM-5.
Final JAVA Practical of BCA SEM-5.Final JAVA Practical of BCA SEM-5.
Final JAVA Practical of BCA SEM-5.Nishan Barot
 
Java PRACTICAL file
Java PRACTICAL fileJava PRACTICAL file
Java PRACTICAL fileRACHIT_GUPTA
 
DSU C&C++ Practical File Diploma
DSU C&C++ Practical File DiplomaDSU C&C++ Practical File Diploma
DSU C&C++ Practical File Diplomamustkeem khan
 
Java simple programs
Java simple programsJava simple programs
Java simple programsVEERA RAGAVAN
 
Lambda выражения и Java 8
Lambda выражения и Java 8Lambda выражения и Java 8
Lambda выражения и Java 8Alex Tumanoff
 
What is Pure Functional Programming, and how it can improve our application t...
What is Pure Functional Programming, and how it can improve our application t...What is Pure Functional Programming, and how it can improve our application t...
What is Pure Functional Programming, and how it can improve our application t...Luca Molteni
 
Csphtp1 08
Csphtp1 08Csphtp1 08
Csphtp1 08HUST
 
Csphtp1 16
Csphtp1 16Csphtp1 16
Csphtp1 16HUST
 
Working effectively with legacy code
Working effectively with legacy codeWorking effectively with legacy code
Working effectively with legacy codeShriKant Vashishtha
 

What's hot (17)

Java Practical File Diploma
Java Practical File DiplomaJava Practical File Diploma
Java Practical File Diploma
 
Java practical
Java practicalJava practical
Java practical
 
Java_practical_handbook
Java_practical_handbookJava_practical_handbook
Java_practical_handbook
 
.net progrmming part2
.net progrmming part2.net progrmming part2
.net progrmming part2
 
Final JAVA Practical of BCA SEM-5.
Final JAVA Practical of BCA SEM-5.Final JAVA Practical of BCA SEM-5.
Final JAVA Practical of BCA SEM-5.
 
39927902 c-labmanual
39927902 c-labmanual39927902 c-labmanual
39927902 c-labmanual
 
Java PRACTICAL file
Java PRACTICAL fileJava PRACTICAL file
Java PRACTICAL file
 
DSU C&C++ Practical File Diploma
DSU C&C++ Practical File DiplomaDSU C&C++ Practical File Diploma
DSU C&C++ Practical File Diploma
 
delegates
delegatesdelegates
delegates
 
Java simple programs
Java simple programsJava simple programs
Java simple programs
 
Lambda выражения и Java 8
Lambda выражения и Java 8Lambda выражения и Java 8
Lambda выражения и Java 8
 
Thread
ThreadThread
Thread
 
What is Pure Functional Programming, and how it can improve our application t...
What is Pure Functional Programming, and how it can improve our application t...What is Pure Functional Programming, and how it can improve our application t...
What is Pure Functional Programming, and how it can improve our application t...
 
Csphtp1 08
Csphtp1 08Csphtp1 08
Csphtp1 08
 
Java and j2ee_lab-manual
Java and j2ee_lab-manualJava and j2ee_lab-manual
Java and j2ee_lab-manual
 
Csphtp1 16
Csphtp1 16Csphtp1 16
Csphtp1 16
 
Working effectively with legacy code
Working effectively with legacy codeWorking effectively with legacy code
Working effectively with legacy code
 

Similar to C# (20)

C# programs
C# programsC# programs
C# programs
 
.net progrmming part3
.net progrmming part3.net progrmming part3
.net progrmming part3
 
39927902 c-labmanual
39927902 c-labmanual39927902 c-labmanual
39927902 c-labmanual
 
C# programs
C# programsC# programs
C# programs
 
Java programs
Java programsJava programs
Java programs
 
Test Engine
Test EngineTest Engine
Test Engine
 
Test Engine
Test EngineTest Engine
Test Engine
 
Java doc Pr ITM2
Java doc Pr ITM2Java doc Pr ITM2
Java doc Pr ITM2
 
Java generics
Java genericsJava generics
Java generics
 
C#.net
C#.netC#.net
C#.net
 
Unit-3 Practice Programs-5.docx
Unit-3 Practice Programs-5.docxUnit-3 Practice Programs-5.docx
Unit-3 Practice Programs-5.docx
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Mar-2021_L14...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Mar-2021_L14...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Mar-2021_L14...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Mar-2021_L14...
 
Java Programs
Java ProgramsJava Programs
Java Programs
 
Java Inheritance
Java InheritanceJava Inheritance
Java Inheritance
 
Sam wd programs
Sam wd programsSam wd programs
Sam wd programs
 
P2
P2P2
P2
 
Adding Love to an API (or How to Expose C++ in Unity)
Adding Love to an API (or How to Expose C++ in Unity)Adding Love to an API (or How to Expose C++ in Unity)
Adding Love to an API (or How to Expose C++ in Unity)
 
Uta005
Uta005Uta005
Uta005
 
Java practical
Java practicalJava practical
Java practical
 
Java programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswarJava programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswar
 

More from actacademy

More from actacademy (12)

Autocad
AutocadAutocad
Autocad
 
Computer fundamental
Computer fundamentalComputer fundamental
Computer fundamental
 
Computer fundamental
Computer fundamentalComputer fundamental
Computer fundamental
 
Css
CssCss
Css
 
Asp.net0
Asp.net0Asp.net0
Asp.net0
 
Accounting ppt
Accounting pptAccounting ppt
Accounting ppt
 
Asp.net tips
Asp.net tipsAsp.net tips
Asp.net tips
 
Css
CssCss
Css
 
Ado
AdoAdo
Ado
 
Autocad
AutocadAutocad
Autocad
 
Autocad
AutocadAutocad
Autocad
 
Computer fundamental
Computer fundamentalComputer fundamental
Computer fundamental
 

Recently uploaded

Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesShubhangi Sonawane
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIShubhangi Sonawane
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 

Recently uploaded (20)

Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 

C#

  • 1. 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;
  • 2. 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  using System; If you want to use any variable in more than one method then it is necessary to declare it under class
  • 3. 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; }
  • 4. 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 using System; class one { public int t; } class two { void second() { one r = new one(); r.t = 10; Console.WriteLine(r.t); } ((inheritance not used)
  • 5. 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");
  • 6. } } 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");
  • 7. } 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();
  • 8. } } 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;
  • 9. 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); }
  • 10. 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();
  • 11. 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();
  • 12. 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."); } } }
  • 13. 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."); } } }
  • 14. 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() {
  • 15. 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(); } }
  • 16. 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"); } }
  • 17. 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); }
  • 18. 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
  • 19. //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; }
  • 20. 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; }
  • 21. 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]); }
  • 22. } } 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()); }
  • 23. 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 string string string int r, a2 = string.Copy(a1); a3 = "work is worship"; a4,a5; k1, k2; 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);
  • 24. 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;
  • 25. 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; } } }
  • 26. } 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;
  • 27. 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
  • 28. 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);
  • 29. } } 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); }
  • 30. } 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(); } }
  • 31. 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 {
  • 32. 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.
  • 33. 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"); } }
  • 34. 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
  • 35. { } 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
  • 36. 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;
  • 37. 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
  • 38. //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 { Program Program Program Main(string[] args) n1 = new Program(1,2,3); n2 = new Program(10,10,10); 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");
  • 39. 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 {
  • 40. 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); } }
  • 41. 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");
  • 42. } 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();
  • 43. 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);
  • 44. 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); } }