• No results found

CC

N/A
N/A
Protected

Academic year: 2021

Share "CC"

Copied!
34
0
0

Loading.... (view fulltext now)

Full text

(1)

C# PROGRAMS

(2)

1.

/* BASE:- this is used to pass the value from derived class constructor to the base class constructor

*/

namespace base_kw_usage {

class A {

public int a; public int b; public A()

{ }

public A(int a, int b) {

this.a = a; this.b = b; }

public void sub() {

int s = a - b;

Console.WriteLine("subtraction of two numbers {0} and {1} results {2}",a,b,s);

} }

class B:A {

public int a; public int b; public int c; public B(int a, int b, int c)

: base(a, b) // By using this base method the value of a and b from this contructor is transmitted o the constructor of the base class { this.a = a; this.b = b; this.c = c; }

public int add() { int z = a + b + c; return z; } } class Program {

static void Main(string[] args) {

B b = new B(15,6,4); // Constructor initialized at the object creation step

int x=b.add();

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

b.sub(); // the value from the derived class constr. will get passed to base class constr. with that base method usage

} } }

(3)

2.

namespace Findlargest {

class Program {

static void Main(string[] args) {

Console.WriteLine("Enter any ten elements to compare for largest number");

large();

}

public static void large() {

int[] input ={ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; for (int i = 0; i < 9; i++)

{

int a = Convert.ToInt32 (Console.ReadLine()); input[i] = a; } for (int k = 1; k<9;k++ ) { if (input[0] > input[k]) { } else { // int temp; // temp = input[0]; input[0] = input[k]; // input[k] = temp; }

} Console.WriteLine("Largest no. is" + input[0]); } } } 3. class outkw {

public int add(int a, int b,out int o) // method having argument and return value with out keyword in input argument

{ o = a + b; return o; } } namespace outkeywordus {

(4)

class Program {

static void Main(string[] args) {

int m; // variable declared for out variable outkw ok = new outkw();

int z=ok.add(30,7,out m); // variable m declared with keyword out to get from the function

Console.WriteLine(z); } } } 4. class Program {

static void Main(string[] args) {

int[] list ={ 1,2,3,4,5,6,7,8,9}; Console.WriteLine("Even numbers"); for (int i = 0; i < 9; i++)

{

if (list[i]%2 == 0) {

Console.WriteLine(list[i]); }

} Console.WriteLine("Odd numbers"); for (int i = 0; i < 9; i++)

{ if (list[i] %2!= 0) { Console.WriteLine(list[i]); } } } } 5. class MatrixA {

public int[,] mata = new int[3, 3]; //Declaring the matrix A to have 3 rows and 3 columns

public int i; public int j; public void matrixa()

{

Console.WriteLine("Enter the Matrix A elements\n"); {

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

(5)

{

mata[i, j] = Convert.ToInt32(Console.ReadLine()); }

} } } }

class MatrixB : MatrixA // Interfacing Matrix A to Matrix B {

public int[,] matb = new int[3, 3]; //Declaring the matrix B to have 3 rows and 3 columns

public int i; public int j; public void matrixb()

{

matrixa();

Console.WriteLine("Enter the Matrix B elements\n"); {

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

for (j = 0; j < 3; j++) {

matb[i, j] = Convert.ToInt32(Console.ReadLine()); }

} } } }

class MatrixAdd : MatrixB // Interfacing the matrix B to this class {

public int[,] matc = new int[3, 3];//Declaring the matrix C to have 3 rows and 3 columns

public int j; public int i; public void matadd()

{

matrixb();

Console.WriteLine("Matrix A is\n"); for (i = 0; i < 3; i++)

{

for (j = 0; j < 3; j++) {

Console.Write("\t" + mata[i, j]);

} Console.WriteLine(); Console.WriteLine(); } Console.WriteLine("Matrix B is\n");

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

for (j = 0; j < 3; j++) {

Console.Write("\t" + matb[i, j]);

} Console.WriteLine(); Console.WriteLine(); } Console.WriteLine("Matrix C is\n");

(6)

{

for (j = 0; j < 3; j++) {

matc[i, j] = mata[i, j] + matb[i, j]; Console.Write("\t" + matc[i, j]);

} Console.WriteLine(); Console.WriteLine(); }

} }

class Program {

static void Main(string[] args) {

MatrixAdd ma = new MatrixAdd();

// mm.matrixa();accessing the method by object // mm.matrixb();

ma.matadd();// accessing the parent classes through the derived class object by having the methods included in the class by interface

} }

6.

class MatrixA {

public int[,] mata = new int[3, 3]; public int i;public int j; public void matrixa()

{

Console.WriteLine("Enter the Matrix A elements\n"); { for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) {

mata[i,j] = Convert.ToInt32(Console.ReadLine()); } } } } }

class MatrixB:MatrixA {

public int[,] matb = new int[3, 3]; public int i; public int j; public void matrixb()

{

Console.WriteLine("Enter the Matrix B elements\n"); { for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) {

(7)

matb[i, j] = Convert.ToInt32(Console.ReadLine()); } } } } }

class MatrixMult:MatrixB // Interfacing the matrix A to this class {

public int[,] matc = new int[3, 3]; public int j; public int i; public void matmul()

{

Console.WriteLine("Matrix A is"); for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { Console.Write("\t"+mata[i, j]);

} Console.WriteLine(); Console.WriteLine(); } Console.WriteLine("Matrix B is");

for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { Console.Write("\t"+matb[i, j]);

} Console.WriteLine(); Console.WriteLine(); } Console.WriteLine("Matrix C is");

for (i = 0; i < 3; i++) { for ( j = 0; j < 3; j++) { matc[i,j]=mata[i,j]*matb[i,j]; Console.Write("\t"+matc[i, j]);

} Console.WriteLine(); Console.WriteLine(); }

} }

class Program {

static void Main(string[] args) {

MatrixMult mm = new MatrixMult(); mm.matrixa(); mm.matrixb(); mm.matmul(); } } 7. class paramex {

(8)

public static void parray(params int[] m) { Console .WriteLine("array elements"); foreach (int i in m) { Console.WriteLine(i); } } } namespace paramsusage { class Program {

static void Main(string[] args) { int[] x ={ 1,2,3}; paramex. parray(x); paramex.parray(); paramex.parray(100,200); } } } 8. class ClassI {

public int x; public int y; public ClassI()

{ Console.WriteLine(@" ClassI:

Default Constructor is employed in this class ClassII:

addition process done and the result is "); }

}

class ClassIII:ClassI {

public ClassIII()

{ Console.WriteLine("Class III accessed"); } }

class ClassII : ClassIII {

public ClassII(int a, int b) { this.x = a; this.y = b; } public void adder()

{ int c = x + y; Console.WriteLine(c); } } namespace Inheritanceex { class Program {

static void Main(string[] args) {

ClassII c2 = new ClassII(5,5); c2.adder();

} }

(9)

}

9. SUM OF DIGITS

class SoDprocess {

public void SoDcal(int a) {

int b; int sum = 0; while (a != 0) {

b = a % 10; a = a / 10; sum = sum + b;

} Console.WriteLine("The sum of digits in given number is " + sum); } } namespace sumofdigitsclass { class Program {

static void Main(string[] args) {

int m;

Console.WriteLine("Enter a number"); string ah = Console.ReadLine(); m = Int32.Parse(ah);

SoDprocess sod = new SoDprocess(); sod.SoDcal(m); } } } 10. namespace print_no_in_rev_order { class Program {

static void Main(string[] args) {

int[] input ={1,2,3,4,5,6,7,8,9,0}; {

for (int i = 0; i < 10; i++) {

input[i]=Int16 .Parse ( Console.ReadLine()); }

// Array.Sort(input);// ascending sort Array.Reverse(input);

foreach (int j in input) {

Console .WriteLine (j); }

(10)

} } } 11. class Input {

public int x; public int y; public Input()

{ }

/* public Input(int a, int b) {

this.x = a; this.y = b; }*/

public void printer() {

for (int i = 1; i <= 500; i++) {

Console.WriteLine(i); if (i == 100)

{

Console.WriteLine("101 to 199 skipped"); i = 200;

}

else if (i == 300) {

Console.WriteLine("301 to 399 skipped"); i = 400; } } } } class Program {

static void Main(string[] args) {

Input i = new Input(); i.printer();

} }

12. SWAPPING OF TWO NUMBERS

class SwapInput

{ public int s; public int t;

public SwapInput() // Default constructor { }

public SwapInput(int x, int y) // Constructor with two arguments {

this.s = x; this.t = y;

(11)

}

~SwapInput() // Destructor { }

public void swapper() {

int temp;

Console.WriteLine(@"Before swapping x=" + s + "\n\t\ty=" + t); temp = s;

s = t; t = temp;

Console.WriteLine(@"After swapping x=" + s + "\n\t\ty=" + t); }

}

class Program {

static void Main(string[] args) {

SwapInput si = new SwapInput(50,5); si.swapper(); } } 13. class methodregion {

// public int a; public int b;

public void add(int x, int y ,out int c) {

c=x+y;

Console.WriteLine(c); }

public void sub(int a, int b) { int z=a-b; Console.WriteLine(z); } } class MainProgram {

static void Main(string[] args) {

methodregion mr = new methodregion(); int o;

Console.WriteLine("Enter 1 to add and 2 to sub"); int m=Convert.ToInt16 (Console.ReadLine());

switch (m) {

case 1:

mr.add(5,5,out o); // invoking the method add from the class methodregion

break; case 2:

mr.sub(10, 5); // invoking the method sub from the class methodregion

(12)

default: break; }

} }

14. SORTING NUMBERS IN ASCENDING ORDER

class Sort {

/* public Sort(int a, int b, int c,int d)

{ this.a = a; this.b = b; this.c = c; this.d = d; }*/ public void sorter(int[] n)

{

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

for (int j = i+1; j <4 ; j++) { if (n[i] > n[j]) { int t = n[i]; n[i] = n[j]; n[j] = t; } else { } } Console.WriteLine(n[i]); } } } namespace sortingnumbers { class Program {

static void Main(string[] args) {

Sort s = new Sort() ; int[] num ={ 22,10,67,4};

Console.WriteLine("Before sorting" ); foreach (int ar in num)

{ Console.WriteLine(+ar); }

Console.WriteLine("The ascending sorted numbers are"); s.sorter(num);

} } }

15. TWO DIMENSIONAL ARRAY DECLARING AND INITIALIZATION

class TwoDArray {

(13)

public int[,]arr=new int[2,2]{{1,1},{2,2}};// Initializing the two dimensional array

public string[,] name = new string[2, 2] { { "venkatesh","venkat" }, { "venu","vee" } };

public void arraydis() {

foreach(object i in arr) {Console .WriteLine(i);} }

public void arraydis1()

{ foreach (object i in name ) Console .WriteLine (i); }

}

class Program {

static void Main(string[] args) {

TwoDArray td = new TwoDArray(); td.arraydis();

td.arraydis1(); }

}

16. FINDING LARGEST NUMBER

public class Program {

static void Main(string[] args) {

Console.WriteLine("Enter any ten elements to compare for largest number");

large();

}

public static void large() {

int[] input =new input[10]; for (int i = 0; i < 9; i++) {

int a = Convert.ToInt32 (Console.ReadLine()); input[i] = a; } for (int k = 1; k<9;k++ ) { if (input[0] > input[k]) { input[0] = input[k];} else { }

} Console.WriteLine("Smallest no. is" + input[0]); }

}

(14)

17. BANK ACCOUNT MGMT USING CONSTRUCTOR

class AccMgmt {

public int amt;

public AccMgmt(int x) // Constructor with one argument {

this.amt = x; }

public string name = "Venkatesh";

public void benq() // method for balance enquiry {

Console.WriteLine("\nName : {0}\n Balance : {1}",name ,amt ); }

public void withdrawl(int w) // method for money withdrawl {

if (w > amt)

Console.WriteLine("Not sufficient balance you have only Rs: {0} in your account", amt);

else {

int b = amt - w;

Console.WriteLine("Balance after your last transaction is" + b);

} }

public void deposit() // method for deposition {

Console.WriteLine("Enter the amount to deposit"); int d = Int16.Parse(Console .ReadLine ());

int b = d + amt;

Console.WriteLine("Your balance after your deposition of Rs {0} is {1}",d,b);

} }

// Start of Main Program class Program

{

static void Main(string[] args) {

AccMgmt am = new AccMgmt(10000); // passing the value to the constructor by object

Console.WriteLine("\t\t\tWelcome to My Bank\n\n"); start:

Console.WriteLine("Select your choice"); Console.WriteLine(@" 1.Balance Enquiry 2.Cash Withdrawl 3.Deposition 4.Exit");

int i=Int16.Parse(Console.ReadLine ()); switch (i)

(15)

case 1:

am.benq();

Console.WriteLine("Would you like to have an other transaction. If yes press Y or press any key to exit");

string s=Console.ReadLine(); int n = string.Compare(s,"Y"); int nn = string.Compare(s, "y"); if(n==0 || nn==0)

goto start; else

break; case 2:

Console.WriteLine("Enter the amount to withdraw "); int w = Int16.Parse(Console .ReadLine ());

am.withdrawl(w);

Console.WriteLine("Would you like to have an other transaction. If yes press Y or press any key to exit");

string s1 = Console.ReadLine(); int n1 = string.Compare(s1, "Y"); int n11 = string.Compare(s1, "y"); if (n1 == 0 || n11==0) goto start; else break; case 3: am.deposit();

Console.WriteLine("Would you like to have an other transaction. If yes press Y or press any key to exit");

string s2 = Console.ReadLine(); int n2 = string.Compare(s2, "Y"); int n21 = string.Compare(s2, "y"); if (n2 == 0 || n21==0) goto start; else break; case 4: break; default:

Console .WriteLine ("Sorry wrong option select the correct choice");

goto start; }

} }

18. FACTORIAL USING CLASS

class factorial {

public void fact(int a) {

int b = a; int c = a; {

for (int i = 1; i < c; i++) {

(16)

a = a * b; //a=a*(b-1); //b--;

}

} Console.WriteLine("Factorial for:" + b + "is" + a); }

}

namespace factex {

class MAinPgm

{ public static void Main(string [] args) { factorial fact=new factorial ();

Console.WriteLine("Ehter a value to find the factorial"); string it=Console .ReadLine ();

int i = Int32.Parse(it); Console.WriteLine(); fact.fact(i);

} } }

19. SUM OF SQUARE OF DIGITS

class squaredigit {

int x;

public squaredigit(int a) { this. x = a; }

public void process() { int ss = 0; while (x != 0) { int y = x % 10; x = x / 10; ss = ss + (y*y); } Console.WriteLine(ss); } } namespace squareofdigit { class MainPgm {

public static void Main(string[] args) {

squaredigit sd = new squaredigit(55); sd.process();

} } }

(17)

20. SCOPE AND VISIBILTY OF PRIVATE AND PUBLIC MEMBERS

// Showing the scope of private and public members in class namespace scope_of_pub_pri_mem

{

class A {

int a; int b; int c; // private members declares and hence the scope of the members are within this class A only and the method using this private members cannot be inherited nor accessed by the object of the derived class public A(int x, int y)

{

this.a = x; this.b = y; }

public int add() { return c = a + b; } } class B {

public void sub( int x, int y) {

//add(); //The method cannot be called here since the members involved in this are private

int c;

//return c = x - y; c = x - y;

Console.WriteLine("Subtraction result : "+c); }

}

class C:B // class B is inherited in to this class {

public int a; public int b; public int c; public void mul(int a,int b)

{ sub(a, b); // the members a and b since declared as public it is passed to the method sub() which is in class B

c=a*b;

Console .WriteLine ("Multiplication result: "+c); }

}

class Program {

static void Main(string[] args) {

A a = new A(5,5); int m=a.add();

Console.WriteLine("Addition result : " + m);

B b = new B();

// b.sub();// cannot be accessed by object b of class B since it takes the input arguments from the class C

(18)

// Console.WriteLine("Subtraction result : "+z);

// b.add(); //cannot be accessed by the object b since it cannot pass any arguments to the private members of the class

C c=new C (); c.mul(15,7); }

} }

20. SWAPPING OF TWO NUMBERS WITHOUT USING A TEMPORARY VARIABLE

namespace swapof2numwithouttempvaar {

class Program {

static void Main(string[] args) {

int a; int b;

a = Convert.ToInt16(Console.ReadLine()); b = Int16.Parse(Console.ReadLine() );

Console.WriteLine("Before swapping a={0} and b={1}", a, b);

a = a + b; b = a-b; a = a-b;

Console.WriteLine("After swapping a={0} and b={1}", a, b); }

} }

21. SIMPLE ARRAYLIST EXAMPLE

using System;

using System.Collections;// Remove the generic from it to make arraylist to work using System.Text; namespace ArrayListex { class Program {

static void Main(string[] args) {

ArrayList a = new ArrayList();// Declaring the arraylist a.Add("a"); // Adding elements to arraylist a.Add("b"); a.Add("c"); foreach (string s in a) { Console.WriteLine(s); } } } }

(19)

22. ARRAYLIST EXAMPLE USING CLASS

using System;

using System.Collections;// Remove the generic from it to make arraylist to work

using System.Text;

// ArrayList class usge: Accessed by the object of the main class namespace ArrayListex2

{

class ArrayListExample // Class defined for declaring the array list {

public void getdata() {

ArrayList al = new ArrayList(); //ArrayList declaration al.Add("venkatesh"); // Adding elements to arraylist al.Add(1); // Adding elements to arraylist al.Add("name"); // Adding elements to arraylist foreach (object i in al)

{ Console.WriteLine(i); } } } class Program {

static void Main(string[] args) {

ArrayListExample ale = new ArrayListExample(); ale.getdata();

} } }

23. STRING SORTING USING ARRAY LIST

class Program {

static void Main(string[] args) {

ArrayList name = new ArrayList();// Array list declaration

name.Add("Venkatesh"); // Adding elements starting from the index 0

name.Add("Babu"); name.Add("Kumar"); name.Add("Ramesh");

Console .WriteLine ("\nBefore Sorting\n"); foreach (string j in name)

{

Console.WriteLine(j); }

Console.WriteLine("\nAfter sorting\n"); name.Sort();

foreach (string i in name) {

Console.WriteLine(i); }

(20)

} }

24. STRING SORTING USING ARRAY

class Program {

static void Main(string[] args) {

string[] name ={ "venkat","kumar","zebastin","karna"}; Console.WriteLine("Before Sorting ");

foreach (string j in name) {

Console.WriteLine(j); }

Console.WriteLine("After Sorting "); Array.Sort(name);

foreach (string i in name) { Console.WriteLine(i); } } } 25.

// PROGRAM TO SHOW HOW TO ACCESS THE MEMBER WHICH IS DECLARED AS A STATIC MEMBER

class StatMemAcc {

public static void disp()

{ Console.WriteLine("Static member is accessed"); } }

namespace staticmemberaccess {

class Program {

static void Main(string[] args) { StatMemAcc.disp(); } } } 26. class stringarray {

public static void Main()

{ string[] name=new string[] {"Mon", "Tue", "Wed"}; {foreach(object i in name)

{Console.WriteLine(i);} }

(21)

}

27.

// STRUCTURE is a value type // It cannot be inherited

// STRUCTURE can have constructor and methods as like CLASS. // It does not support default constructor

// It does not support INHERITANCE namespace Structure2

{

public struct baseclass {

public int c; public int x; public int y;

// public baseclass() // default constructor not possible in structures

//{ }

public baseclass(int a, int b) {

// this.x = a; 'this' keyword is required for assigning for the variable which are alike

// this.y = b; // this.c = a + b; x = a; y = b; c = a + b; }

public void disp(baseclass b) { baseclass m; m.c = x+b.c; m.x = x + b.x; m.y = y + b.y; Console.WriteLine("c={0}\nx={1}\ny={2}",m.c,m.x,m.y); } } class MainProgram {

static void Main(string[] args) {

baseclass b = new baseclass(5, 4);// structure initialization b.disp(b); Console.WriteLine(b.c); } } } 28. namespace structex3

(22)

{

struct details

{public string name; public string adr; public int phn;public int pin; public string cty;

public details(string n, string adr, int phn, int pin, string cty) { this.name = n; this.adr = adr; this.phn = phn; this.pin = pin; this.cty = cty; }

public void getdet(details d) {

// Console.WriteLine(" Name: {0}\nAddr: {1}\n City: {2}\n Pin no. : {3}\n Phone no. :{4}\n",d.name ,d.adr,d.cty ,d.pin ,d.phn );

Console.WriteLine(" Name: {0}\nAddr: {1}\n City: {2}\n Pin no. : {3}\n Phone no. :{4}\n", name, adr, cty, pin, phn);

} }

class Program {

static void Main(string[] args) {

Console.WriteLine("Enter your name"); string a = Console.ReadLine();

Console.WriteLine("Address"); string b = Console.ReadLine(); Console .WriteLine ("city"); string c = Console.ReadLine(); Console.WriteLine("Pin no");

int d = Int32.Parse(Console .ReadLine ()); Console.WriteLine("phone no.");

int e = Int32.Parse(Console .ReadLine ()); details dt =new details (a,b,e,d,c); dt.getdet(dt);

} } }

Output:

Enter your name Venkatesh Address geetha nagar city trichy Pin no 620017 phone no. 0 Name: Venkatesh Addr: geetha nagar City: trichy

Pin no. :620017 Phone no. :0

(23)

29.

namespace Structure_example {

struct Example {

public int x; public int y;

/* public Example()  Structures cannot have a default constructor { }*/

public Example(int a, int b) // But structures can have the constructor with arguments

{

this.x = a; this.y = b; }

public Example structaccess(Example e) // Structure employed as a datatype for both the input and output arguments

{

Example ex; // object created for the structure ex.x = x + e.x; // Accessing the variable by object ex.y = y + e.y;

return ex; // returning the object }

}

class Program {

static void Main(string[] args) {

Example el = new Example(5, 5); // invoking the constructor class with input

Example v;

v=el.structaccess(el);

Console.WriteLine(v.x + v.y); }

} }

30.

class GetArrayList

{//public string list; public void getdata() {

ArrayList list = new ArrayList(); list.Add("Index 0: Item1");

list.Add("Index 1: Item2"); list.Add("Index 2: Item3"); list.Add("Index 3: Item4"); disp(list);

Console.WriteLine("Count" + list.Count);

Console.WriteLine("Removing the item ftom the index 3"); list.RemoveAt(3);

disp(list);

Console.WriteLine("Count"+list .Count );

Console.WriteLine("Inserting a new item at index 2"); list.Insert(2, "new item inserted here");

(24)

Console.WriteLine("Count" + list.Count);

Console.WriteLine("Printing the array list in reverse order"); list.Reverse();

disp(list);

Console.WriteLine("Count" + list.Count); Console.WriteLine("Sorting the array list"); list.Sort();

disp(list);

Console.WriteLine("Removing the index by range from 0 to 2"); list.RemoveRange(0, 2);

Console.WriteLine("Clearing all contents from array list"); Console.WriteLine("All contents are cleared successfully"); list.Clear();

disp(list); }

/* public void cleararraylist() {

getdata();

}*/

public static void disp(ArrayList list) {

foreach (object i in list) { Console.WriteLine(i); } } } class Program {

static void Main(string[] args) {

GetArrayList g = new GetArrayList(); g.getdata(); } } 31. class sortdescend {

public void DscendingSort(params int[] x) {for(int i=0;i<7;i++) { for (int j = i + 1; j < 7; j++) { if (x[i] < x[j]) {

int temp = x[i]; x[i] = x[j]; x[j] = temp; } else { } } Console.WriteLine(x[i]);

(25)

} } } namespace descendingsort { class Program {

static void Main(string[] args) {

sortdescend sd = new sortdescend(); int[] m ={ 43,11,76,1,99,56,79,100}; Console.WriteLine("Before sorting"); foreach (object i in m)

{ Console.WriteLine(i); }

Console.WriteLine("After sorting"); sd.DscendingSort(m); //sd.DscendingSort(2,3,4,5,6,7,8,9); } } } 32. namespace delegatessamplepgm { class Program {

delegate int delmtd(int a,int b); static void Main(string[] args) {

delmtd dm = new delmtd(add); int a=5;int b=6;

int r=dm(a,b);

Console.WriteLine(r); }

static int add(int a, int b) { int z=a+b; return z; //Console.WriteLine(z); } } } 33.

// As a delegate method usage it can be used as a substitute for the method which looks similar as delegate method with the same dataype of return value and no. of input arguments

namespace Delegate_Multipleaccess {

class Program {

public delegate int delmulacc(int a, int b,int c); //delclaring the delegate

(26)

{

delmulacc dma = null; // Initializing the delegate to null value dma = new delmulacc(add);

// dma += new delmulacc(sub); IT shows error since the return type and no. of input arguments doesnot matchwith the delegate

dma+= new delmulacc(mult );

//dma += new delmulacc(div); it is erroneous since the datatype of the delegate and this function mismatches

int s = 15; int t = 8; int u = 10;

/* Method I in accessing methods by delegate class // int r = dma(s,t,u);

// Console.WriteLine(r); */

// Method II in accessing the methods by delegate class delmet(5,4,7,dma);

}

static void delmet(int s,int t, int u,delmulacc dma1) // dma1 is the object created as like dma created earlier

{

int r = dma1(s,t,u); Console.WriteLine(r); }

static int add(int a, int b,int c) {

return a + b+c; }

static void sub(int a, int b) {

Console.WriteLine("a+b=" + (a + b)); }

static int mult(int a, int b, int c) {

return a * b*c;

//Console.WriteLine("a*b="+(a*b)); }

static float div(float a, float b,float c) { return (a + b)/c; } } } 34. class Program {

static void disp()

{ Console.WriteLine("Static member is accessed"); } public static void Main(string[] args)

{

disp(); // static member is accesses by its class not by the object of main class

(27)

} }

35.

class TwoDArray {

public int[,]arr=new int[2,2]{{1,1},{2,2}};// Initializing the two dimensional array

public string[,] name = new string[2, 2] { { "venkatesh","venkat" }, { "venu","vee" } };

public void arraydis() {

foreach(object i in arr) {Console .WriteLine(i);} }

public void arraydis1()

{ foreach (object i in name ) Console .WriteLine (i); }

}

class Program {

static void Main(string[] args) {

TwoDArray td = new TwoDArray(); td.arraydis();

td.arraydis1(); }

}

36.

// Interface is used for implementing multiple inheritance

// In the interface it only contains the defition for variables used and methods employed in the derived class

namespace interface_ex {

interface A {

int m // Defining the variables with get{} set{} property { get; set;}

int n

{ get; set;}

// Defining the methods void dispa();

void add(int m, int n); }

(28)

class B : A {

public int x; public int y;

void A.dispa() // Accessing the method dispa from interface A

{

Console.WriteLine("I'm Interface A"); }

public B(int a, int b) // Constructor to get and set values to m and n

{

this.x=a; this.y=b; }

int A.m // setting value of x from constructor to m

{

get { return x; } set{x=value ;} }

int A.n // setting value of x from constructor to n

{

get {return y;} set {y=value ;} }

void A.add(int m, int n) //Accessing the method add from interface A with input arguments m and n from interface A

{

int z=m+n;

Console .WriteLine ("sum is"+z); }

}

class Program {

static void Main(string[] args) {

B m = new B(5,5); // creating object for derived class as usual A a=(A)m; // creating object to interface with ref. to derived class object

a.dispa(); // accessing the interface A methhod using interface object. Note interface methods and members cannot be accessed by the derived class objects

a.m = 5; a.n = 6; a.add(a.m,a.n); Console.WriteLine("m="+a.m+"\nn="+a.n); } } } 37. interface A { int m1 { get; set;}

(29)

}

interface B:A {

new void m1(); // hiding the method m1 by putting new before of it }

class C : B {

private int x;

int A.m1 // assigning the value to m1 int he interface m1 from x { get { return x; } set { x = value; } } void B.m1() { Console.WriteLine("M1"); } } class Program {

static void Main(string[] args) {

C c=new C() ;

B m11 = (B)c; //creating object for interface B withrespect to the derived class C

A m12 = (A)c; //creating object for interface A withrespect to the derived class C

m12.m1 = 123; // Accessing the method m1 of by object of A m11.m1(); // Accessing the method m1 of by object of B Console.WriteLine(m12.m1); } } 38. // MULTPLE INHERITANCE namespace ConsoleApplication3 { interface A { int m1 { get; set;} } interface B {

new void m1(); // hiding the method m1 by putting new before of it }

class C : B,A // Multiple inheritance {

(30)

int A.m1 // assigning the value to m1 int he interface m1 from x { get { return x; } set { x = value; } } void B.m1() { Console.WriteLine("M1"); } } class Program {

static void Main(string[] args) {

C c=new C() ;

B m11 = (B)c; //creating object for interface B withrespect to the derived class C

A m12 = (A)c; //creating object for interface A withrespect to the derived class C

m12.m1 = 123; // Accessing the method m1 of by object of A m11.m1(); // Accessing the method m1 of by object of B Console.WriteLine(m12.m1); } } } 39. MULTIPLE INHERITANCES: interface A { int m { get; set; } int n { get; set; } } interface B { void add(); }

class C:B,A // INHERITED WITH MULTIPLE INTERFACES A & B {private int a; private int b;

public C(int a, int b) {

this.a = a; this.b = b; }

(31)

{

get { return a; } // returns the value to m but as a variable a set { a = value; }

}

int A.n {

get { return b; } // returns the value to n but as a variable b set { b = value; }

}

void B.add() {

int z = a + b; // here a is m and b is n. the constructor values are assigned to m and n

Console.WriteLine("a="+a); Console.WriteLine("b="+b); Console.WriteLine("z="+z); } } class Program {

static void Main(string[] args) {

C c = new C(15,5); A a = (A)c;

B b = (B)c;

// a.m = 5; m is given 5

// a.n = 6; n is given 6 trying with this m and n values will overwrite the constructor value and yield the reults as 11

b.add();

Console.WriteLine("m="+a.m+"\nn="+a.n);// to show the value of m and n } } Output: a=15 b=5 z=20 m=15 n=5 40. class Array_initializing_with_size {

static void Main(string[] args) {

int[] no=new int [5]; // Array declared with size of five

(32)

for (int i = 0; i < 5; i++) {

no[i] = int.Parse(Console .ReadLine ()); }

foreach (int j in no) {

Console.WriteLine("The array elements are "+j); }

} } Output:

Enter 5 elements to find its square root 6 4 8 3 9 Square root of 6 is 2.44948974278318 Square root of 4 is 2 Square root of 8 is 2.82842712474619 Square root of 3 is 1.73205080756888 Square root of 9 is 3 41. DELEGATES IN CLASS

public delegate void sampleDelegate( ); class sampleClass1

{

public static void sampleMethod( ) {

Console.WriteLine("Executing sampleMethod of sampleClass1"); }

}

class sampleClass2 {

static void Main( ) {

sampleDelegate dele1 = new sampleDelegate(sampleClass1.sampleMethod); dele1();

} }

(33)

Output:

Executing sampleMethod of sampleClass1

42. BASE KEYWORD USAGE

class A {

public int x; public int y; public A(int a, int b) {

this.x = a; this.y = b; }

public void displaya() {

Console.WriteLine("a={0} and b={1}", x, y); }

}

class B:A {

public int x; public int y; public int z; public B(int a, int b, int c)

: base(a, b) // value passes to its base class A {

this.x = a; this.y = b; this.z = c; }

public void displayb() {

Console.WriteLine("a={0} and b={1} and c={2}", x, y,z); }

} class C:B {

public int m; public int n; public int o; public int p;

public C(int a, int b, int c, int d):base(a,b,c) // value passes to its base class B

{ this.m = a; this.n = b; this.o = c; this.p = d; }

public void displayc() {

Console.WriteLine("a={0} and b={1} and c={2} and d={3}", m, n, o,p); } } class Program {

static void Main(string[] args) {

(34)

C c = new C(5,3,7,10); c.displayc(); c.displayb(); c.displaya(); } } Output:

a=5 and b=3 and c=7 and d=10 a=5 and b=3 and c=7

References

Related documents

Survival curves based on Cox analysis comparing cumulative mild cognitive impairment (MCI) incidence in subjects who were cognitively normal at the first evaluation by

Mourhab valley 202, 207 Mozambique 48, 61–62 Mtera–Kidatu hydropower complex 11, 177, 181, 182–183, 186 Municipalities, water use 32, 33–34, 160, 161, 243 Murray River

The job creation curve, equation (33), says that a higher wage rate leads to reduced job vacancies and thus lowers the equilibrium ratio of jobs to workers. The wage curve,

Do other metabolic risk factors such as HTN, a history of stroke, obesity, and dyslipidemia contribute to the development of Alzheimer’s disease in older Hispanic adults.. H o

7 The fact that certain prophetic activities display an unmistakably shamanistic strain, such as magic, miracles, spirit possession and ecstasy, cannot be denied,.. and,

The maximum shear stress values were calculated in three distinct segments of the SG erenal artery ( Fig. 5): the SG segment originating from the EG main body and mating the

The coherence-incoherence chimera states emerging in the case of weakly coupled Kuramoto phase oscillators are replaced with chimera states consisting of coexisting domains of

Correct Answer: C Section: none Explanation Explanation/Reference: Explanation: QUESTION 16 www.vceplus.com - Download A+ VCE latest free Open VCE Exams - VCE to PDF Converter -