csharp Pathak part2
csharp Pathak part2
Class Program
{
Static void Main(string[] args)
{
int[] x = new int[5];
int i;
Console.WriteLine("Enter the elements");
for (i = 0; i <= 4; i++)
x[i] = int.Parse(Console.ReadLine());
Console.WriteLine("Elements Are---");
for (i = 0; i <= 4; i++)
Console.WriteLine(x[i]);
Console.ReadKey();
}
}
Sum of Elements
Class Program
{
Static void Main(string[] args)
{
int[] x = new int[5];
int i,s=0;
Console.WriteLine("Enter the
elements");
for (i = 0; i<= 4; i++)
x[i] =
int.Parse(Console.ReadLine());
Console.WriteLine("Elements Are---
");
for (i = 0; i <= 4; i++)
{
s = s + x[i];
Console.WriteLine(x[i]);
}
Console.WriteLine(s);
Console.ReadKey();
}
}
Linear Search
One Sorting
Largest
Smallest
2D Array
Class Program
{
Static void Main(string[] args)
{
int[,] x = new int[3,3];
int i,j;
Console.WriteLine("Enter the
elements");
for (i = 0; i <= 2; i++)
{
for(j=0;j<=2;j++)
x[i,j] =
int.Parse(Console.ReadLine());
}
Console.WriteLine("Elements Are---");
for (i = 0; i <= 2; i++)
{
for (j = 0; j <= 2; j++)
Console.Write(x[i, j]+" ");
Console.WriteLine();
}
Console.ReadKey();
}
}
Matrix Addition
Forward Diagonal
Matrix Multiplication
For each Loop Example
Class Program
{
Static void Main(string[] args)
{
int[] x = { 1, 2, 3, 4, 5 };
foreach (int j in x)
Console.WriteLine(j);
Console.ReadKey();
}
}
Class and Object
Class Program
{
int x;
static void Main(string[] args)
{
Program p = new Program();
p.x = 12;
Console.WriteLine(p.x);
Console.ReadKey();
}
}
Example2
Class test
{
int a;
int b;
public void set()
{
a = 10;
b = 15;
}
Public void display()
{
Console.WriteLine(a+" "+b);
}
}
Class Program
{
int x;
static void Main(string[] args)
{
test t = new test();
t.set();
t.display();
Console.ReadKey();
}
}
Example 3
Class test
{
int a;
int b;
public void set()
{
Console.WriteLine("Enter a and b");
a = int.Parse(Console.ReadLine());
b = int.Parse(Console.ReadLine());
}
Public void display()
{
Console.WriteLine(a+" "+b);
}
}
classProgram
{
int x;
staticvoid Main(string[] args)
{
test t = new test();
t.set();
t.display();
Console.ReadKey();
}
}
Example 4
Class test
{
int n, s = 0,b;
public void input()
{
Console.WriteLine("Enter any number");
n = int.Parse(Console.ReadLine());
b = n;
}
Public void find()
{
int a;
while (n != 0)
{
a = n % 10;
s = s + (a * a * a);
n = n / 10;
}
if(s==b)
Console.WriteLine("Armstrong");
else
Console.WriteLine("Not Armstrong");
}
}
Class Program
{
int x;
static void Main(string[] args)
{
test t = new test();
t.input();
t.find();
Console.ReadKey();
}
}
EXAMPLE OF CONSTRUCTOR
Class test
{
public test()
{
Console.WriteLine("Cons called");
}
}
Class Program
{
int x;
static void Main(string[] args)
{
test t = new test();
Console.ReadKey();
}
}
Example 6
Class test
{
int x;
public test()
{
x = 5;
}
Public void display()
{
Console.WriteLine(x);
}
}
Class Program
{
int x;
static void Main(string[] args)
{
test t = new test();
t.display();
Console.ReadKey();
}
}
Class test
{
int x;
public test(int a)
{
x = a;
}
Public void display()
{
Console.WriteLine(x);
}
}
Class Program
{
int x;
static void Main(string[] args)
{
test t = new test(5);
t.display();
test t1 = new test(8);
t1.display();
test t2 = new test(12);
t2.display();
Console.ReadKey();
}
}
EXAMPLE OF PARACONS
Class test
{
int x;
public test(int a)
{
x = a;
}
public test()
{
x = -2;
}
Public void display()
{
Console.WriteLine(x);
}
}
Class Program
{
int x;
static void Main(string[] args)
{
test t = new test(5);
t.display();
test t2 = new test();
t2.display();
Console.ReadKey();
}
}
12/02/18
EXAMPLE OF STATIC METHOD
Class Program
{
Static void display()
{
Console.WriteLine("Hello");
}
Static void Main(string[] args)
{
display();
Console.ReadKey();
}
}
EXAMPLE OF STATIC VARIABLE
Class test
{
Static int x;
int y;
public void set(int a , int b)
{
x = a;
y = b;
}
Public void display()
{
Console.WriteLine(x + " " + y);
}
}
Class Program
{
Static void Main(string[] args)
{
classProgram
{
staticvoid Main(string[] args)
{
test t = new test();
int a;
Console.WriteLine("Enter any value");
a = int.Parse(Console.ReadLine());
t.num = a;
Console.WriteLine(t.num);
t.str = "Welcome";
Console.WriteLine(t.str);
Console.ReadKey();
}
}
Indexers Example 1
classtest
{
int []x;
public test()
{
x = newint[5];
for (int i = 0; i <= 4; i++)
x[i] = -1;
publicintthis[int index]
{
set
{
x[index] = value;
}
get
{
return x[index];
}
}
classProgram
{
staticvoid Main(string[] args)
{
test t = new test();
int x = int.Parse(Console.ReadLine());
for (int i = 0; i <= 4; i++)
{
t[i] = x;
x = x + 1;
}
Indexers Example 2
classtest
{
string []x;
public test()
{
x = newstring[5];
for (int i = 0; i <= 4; i++)
x[i] = "pls assign";
publicstringthis[int index]
{
set
{
x[index] = value;
}
get
{
return x[index];
}
}
classProgram
{
staticvoid Main(string[] args)
{
test t = new test();
t[0] = "C";
t[1] = "C++";
t[2] = "VB";
t[3] = "JAVA";
t[4] = "C#";
for (int i=0;i<=4;i++)
Console.WriteLine(t[i]);
Console.ReadKey();
}
}
Indexers Overloading
classtest
{
string []x;
public test()
{
x = newstring[5];
for (int i = 0; i <= 4; i++)
x[i] = "pls assign";
publicstringthis[int index]
{
set
{
x[index] = value;
}
get
{
return x[index];
}
}
publicintthis[string p]
{
set
{
}
get
{
int i = 0;
while(i<=4)
{
if(x[i]==p)
{
break;
}
i++;
}
if (i <= 4)
return i;
else
return -1;
}
}
classProgram
{
staticvoid Main(string[] args)
{
test t = new test();
t[0] = "C";
t[1] = "C++";
t[2] = "VB";
t[3] = "JAVA";
t[4] = "C#";
for (int i=0;i<=4;i++)
Console.WriteLine(t[i]);
int x = t["C++"];
if(x!=-1)
Console.WriteLine("Found");
else
Console.WriteLine("Not Found");
Console.ReadKey();
}
}
15/02/18
Exception
Class Program
{
Static void Main(string[] args)
{
int x, y, z;
try
{
Console.WriteLine("Enter x and y");
x = int.Parse(Console.ReadLine());
y = int.Parse(Console.ReadLine());
z = x / y;
Console.WriteLine(z);
}
catch (DivideByZeroException)
{
Console.WriteLine("Pls Dont enter Y as
Zero");
}
catch (FormatException)
{
Console.WriteLine("Pls Enter Integers");
}
Console.WriteLine("C#");
Console.WriteLine("Language");
Console.ReadKey();
}
}
17/02/18
Class Program
{
Static void Main(string[] args)
{
int []x = { 21, 12, 33, 24, 35 ,67};
int [] y = {3,4,5,9,2 };
int i,z;
try
{
for (i = 0; i <= 5; i++)
{
z = x[i] / y[i];
Console.WriteLine(z);
}
}
catch (IndexOutOfRangeException e)
{
Console.WriteLine(e);
}
catch (DivideByZeroException e)
{
Console.WriteLine(e);
}
Console.ReadKey();
}
}
throw
class test
{
int x;
public void display(int a)
{
x = a;
if (x > 30)
throw new Exception("Kya Kar Rahe ho??");
else
Console.WriteLine(x);
}
}
classProgram
{
staticvoid Main(string[] args)
{
test t = new test();
try
{
t.display(3);
}
catch (Exception)
{
Console.WriteLine("Error....");
}
Console.WriteLine("C#");
Console.WriteLine("Java");
Console.ReadKey();
}
}
rethrow
classtest
{
int x;
publicvoid display()
{
try
{
Catchall Exception
classtest
{
int x;
public void display()
{
try
{
}catch(Exception)
{
Console.WriteLine("Error");
}
finally
{
Console.WriteLine("Final");
}
Console.ReadKey();
}
}
19/02/18
using System;
class testarg
{
static void Main(string [] arg)
{
int x,y,z;
x=int.Parse(arg[0]);
y=int.Parse(arg[1]);
z=x+y;
Console.WriteLine("Sum is= "+z);
}
}
Inheritance
classA
{
Public void display()
{
Console.WriteLine("Base");
}
}
Class B : A
{
Public void show()
{
Console.WriteLine("Derived");
}
}
Class Program
{
Static void Main(string[] args)
{
B b1 = new B();
b1.display();
b1.show();
Console.ReadKey();
}
}
MultiLevel Inheritance
Class A
{
Public void display()
{
Console.WriteLine("Class A");
}
}
Class B:A
{
publicvoid show()
{
Console.WriteLine("Class B");
}
}
Class c:B
{
publicvoid print()
{
Console.WriteLine("Class C");
}
}
classProgram
{
staticvoid Main(string[] args)
{
c b1 = new c();
b1.display();
b1.show();
b1.print();
Console.ReadKey();
}
}
Constructor and Inheritance
Class A
{
public A()
{
Console.WriteLine("Class A");
}
}
Class B:A
{
public B()
{
Console.WriteLine("Class B");
}
}
Class Program
{
Static void Main(string[] args)
{
A a1 = new A();
B b1 = new B();
Console.ReadKey();
}
}
20/2/18
Class A
{
public A(int x)
{
Console.WriteLine("Base " +x);
}
}
classB:A
{
public B():base(5)
{
Console.WriteLine("Derived ");
}
}
classProgram
{
Static void Main(string[] args)
{
B b1 = new B();
Console.ReadKey();
}
}
Example Of Overriding
Class A
{
Public void show()
{
Console.WriteLine("Base ");
}
}
classB:A
{
Public new void show()
{
Console.WriteLine("Derived ");
}
}
classProgram
{
Static void Main(string[] args)
{
B b1 = new B();
b1.show();
Console.ReadKey();
}
}
Protected MemberMethod
Class A
{
Protected void show()
{
Console.WriteLine("Base ");
}
}
Class B:A
{
Public void display()
{
show();
Console.WriteLine("Derived ");
}
}
Class Program
{
Static void Main(string[] args)
{
B b1 = new B();
//b1.show();
b1.display();
Console.ReadKey();
}
}
Runtime Polymorphism
Class A
{
Public virtual void show()
{
Console.WriteLine("Base Show");
}
}
Class B:A
{
Public override void show()
{
Console.WriteLine("Class B Show");
}
}
Class C : A
{
Public override void show()
{
Console.WriteLine("Class C Show");
}
}
Class D : A
{
Public override void show()
{
Console.WriteLine("Class D Show");
}
}
Class Program
{
Static void Main(string[] args)
{
A a1;
a1 = new A();
a1.show();
a1 = new B();
a1.show();
a1 = new C();
a1.show();
a1 = new D();
a1.show();
Console.ReadKey();
}
}
Abstract Class
Abstract class test
{
Abstract public void show();
Abstract public void display();
}
Class A : test
{
public override void show()
{
Console.WriteLine("Class A Show");
}
Public override void display()
{
Console.WriteLine("Class A display");
}
}
classProgram
{
Static void Main(string[] args)
{
A t=new A();
t.display();
t.show();
Console.ReadKey();
}
}
Example 2
Abstract class test
{
Abstract public void show();
Abstract public void display();
}
Class A : test
{
Public override void show()
{
Console.WriteLine("Class A Show");
}
Public override void display()
{
Console.WriteLine("Class A display");
}
}
Class B : test
{
Public override void show()
{
Console.WriteLine("Class B Show");
}
Public override void display()
{
Console.WriteLine("Class B display");
}
}
Class Program
{
Static void Main(string[] args)
{
A t=new A();
t.display();
t.show();
B t1 = new B();
t1.display();
t1.show();
Console.ReadKey();
}
}
Interface Example
Interface test
{
void print();
}
Class A : test
{
Public void print()
{
Console.WriteLine("print called");
}
Public void geting()
{
Console.WriteLine("Getting called");
}
}
Class Program
{
Static void Main(string[] args)
{
A a1 = new A();
a1.print();
a1.geting();
Console.ReadKey();
}
}
Interface Example2
Interface exam
{
void show();
}
Interface test:exam
{
void print();
}
Class A : test
{
Public void print()
{
Console.WriteLine("print called");
}
Public void show()
{
Console.WriteLine("print called");
}
Public void geting()
{
Console.WriteLine("Getting called");
}
}
classProgram
{
staticvoid Main(string[] args)
{
A a1 = new A();
a1.print();
a1.geting();
a1.show();
Console.ReadKey();
}
}
Multiple Inheritance
Interface test
{
void print();
}
classA
{
Public void show()
{
Console.WriteLine("print called");
}
}
classB : A,test
{
Public void print()
{
Console.WriteLine("print called");
}
Public void geting()
{
Console.WriteLine("Getting called");
}
}
Class Program
{
Static void Main(string[] args)
{
B a1 = new B();
a1.print();
a1.geting();
a1.show();
Console.ReadKey();
}
}