Program in C# to Demonstrate arrays of interface types.


Program No: 1

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
public static void FindMatches(IList<string>iList,object[] ob)

{
Console.WriteLine(“Match array is:”);
foreach(object o in ob)
{
Console.WriteLine(“{0}”,o.ToString());
}
foreach(object o in ob)
{  if(iList.Contains(o.ToString()))
Console.WriteLine(“\niList contains {0} at index{1}”,o,iList.IndexOf(o.ToString()));
}
}

static void Main(string[] args)
{
string[] strings ={ “one”, “two”, “four”, “eight” };
Console.WriteLine(“Strings array values:\n”);
foreach(String s in strings)
{
Console.WriteLine(“{0}”,s);
}
Console.WriteLine(“\n”);
FindMatches(strings,new String[]{“zero”,”one”,”five”,”eight”});
Console.ReadKey();

}
}

}

Program No:2

using System;
using System.Collections;
public class ArrayList1
{
public static void Main()
{
ArrayList MyAL=new ArrayList();
MyAL.Add(“hello”);
MyAL.Add(“world”);
MyAL.Add(“!”);
Console.WriteLine(“MY ARRRAY LIST”);
Console.WriteLine(“count {0}”,MyAL.Count);
Console.WriteLine(“values”);
printValues(MyAL);
}
public static void printValues(IList MyList)
{
foreach(Object o in MyList)
Console.WriteLine(“{0}”,o);
}
}


Program in C# to illustrate the use of different properties.

using System;
class Point
{
int x,y;
public Point()
{
}
public Point(int x,int y) {
this.x=x;
this.y=y;
}
public int X
{
set {
this.x=value;
}
get {
return this.x;
}
}
public int Y
{
set {
this.y=value;
}
get {
return this.y;
}
}
}
class PrintApp {
public static void Main(String[] args)
{
Point p=new Point();
p.X=2;
p.Y=5;
Console.WriteLine(“x = “+p.X+” y = “+p.Y);
p.X++;
p.Y+=2;
Console.WriteLine(“After x++ and y+2″);
Console.WriteLine(“x = “+p.X+” y = “+p.Y);
}
}


Program in C# to build a class which implements an interface which is already existing.

using System;
namespace ConsoleApplication1
{
interface Addition
{
int Add();
}
interface Multiplication
{
int Multiply();
}
class Compute:Addition,Multiplication
{
int x, y;
public Compute(int a, int b)
{
this.x = a;
this.y = b;
}
public int Add()
{
return (x + y);
}
public int Multiply()
{
return (x * y);
}
}
class Interface
{
static void Main(string[] args)
{
int a, b;
Console.Write(“Enter 2 nos:”);
a = Convert.ToInt32(Console.ReadLine());
b = Convert.ToInt32(Console.ReadLine());
Compute ob1=new Compute(a,b);
Console.WriteLine(“Addition is:”+ob1.Add());
Console.WriteLine(“Multiplication is:”+ob1.Multiply());
Console.ReadLine();
}
}
}


Program in C# to demonstrate abstract class and abstract methods.


using System;
abstract class Test {
public int a;
public abstract void A();
}

class Derived1:Test {
public override void A() {
Console.WriteLine(“Derived1 A”);
base.a++;
Console.WriteLine(“a = {0}”,base.a);

}
}
class Derived2:Test {
public override void A() {
Console.WriteLine(“Derived2 A”);
base.a–;
Console.WriteLine(“a = {0}”,base.a);
}
}
class ProgramApp {
static void Main(String[] args) {
Test test1=new Derived1();
test1.A();
Test test2=new Derived2();
test2.A();
}
}


Program in C# to implement linked lists using the existing collections name space.


using System;
using System.Collections.Generic;
using System.Text;

namespace Linked_List
{
    class LinkedList
    {
        List lst;
        public LinkedList()
        {
            lst = new List();     // Create a list.
            Console.WriteLine("\n Initial number of elements in the LL : " + lst.Count);
        }
        public void insert()
        {
            Console.Clear();
            Console.WriteLine("\n *********************************************");
            Console.WriteLine("\n   1-----> INSERTION AT THE BEGINNING");
            Console.WriteLine("\n   2-----> INSERTION AT THE ENDING");
            Console.WriteLine("\n   3-----> INSERTION AFTER CERTAIN NODES");
            Console.WriteLine("\n *********************************************");
            Console.Write("\n\n Enter your choice: ");
            int ch = int.Parse(Console.ReadLine());
            string str;
            switch (ch)
            {
                case 1: Console.Write("\n Enter the data: ");
                        str=Console.ReadLine();
                        lst.Insert(0,str);
                        Console.WriteLine("\n {0} is inserted at the beginning.",str);
                        break;
                case 2: Console.Write("\n Enter the data: ");
                        str = Console.ReadLine();
                        lst.Add(str);
                        Console.WriteLine("\n {0} is inserted at the ending.",str);
                        break;
                case 3: if (lst.Count == 0)
                            {
                                Console.WriteLine("\n List is not built.");
                            }
                            else
                            {
                                Console.Write("\n After how many nodes? : ");
                                int n = int.Parse(Console.ReadLine());
                                if(n>=1&n
                                {
                                    Console.Write("\n Enter the data: ");
                                    str = Console.ReadLine();
                                    lst.Insert(n,str);
                                    Console.WriteLine("\n {0} is inserted after {1} nodes.", str, n);
                                }
                                else
                                    Console.WriteLine("\n After {0} nodes, insertion is not possible !",n);
                            }
                        break;
                default: Console.WriteLine("\n Sorry !!! Wrong  choice.");
                         break;
            }       
          }
        public void delete()
        {
            if (lst.Count == 0)
            {
                Console.WriteLine("\n LL is empty !");
            }

            else
            {

                Console.Clear();
                Console.WriteLine("\n *********************************************");
                Console.WriteLine("\n   1-----> DELETION (FIRST OCCURENCE)");
                Console.WriteLine("\n   2-----> DELETION  AT A SPECIFIED POSITION");
                Console.WriteLine("\n   3-----> DELETION OF ALL THE ELEMENTS");
                Console.WriteLine("\n *********************************************");
                Console.Write("\n\n Enter your choice: ");
                int ch = int.Parse(Console.ReadLine());
                switch (ch)
                {
                    case 1: Console.Write("\n Enter the data: ");
                            string str = Console.ReadLine();
                            if (lst.Contains(str))
                            {
                                lst.Remove(str);
                                Console.WriteLine("\n First occurence of {0} is deleted.", str);
                            }
                            else
                                Console.WriteLine("\n The data '{0}' doesn't exist.", str);
                            break;
                    case 2: Console.Write("\n Enter the position : ");
                            int n = int.Parse(Console.ReadLine());
                            if (n >= 1 & n
                            {
                                lst.RemoveAt(n - 1);
                                Console.WriteLine("\n Element at the position {0} is deleted.", n);
                            }                             
                            else
                                Console.WriteLine("\n Not a valid position to delete !");
                            break;
                    case 3: lst.Clear();
                            Console.WriteLine("\n All the elements are deleted");
                            break;
                    default: Console.WriteLine("\n Sorry !!! Wrong  choice.");
                            break;
                }
            }
        }
        public void status()
        {
            if (lst.Count == 0)
            {
                Console.WriteLine("\n LL is empty !");
            }
            else
            {
                Console.WriteLine("\n LL contains {0} elements.", lst.Count);
                Console.Write("\n The elements are : ");
                for (int i = 0; i < lst.Count; i++)
                    Console.Write(lst[i] + "\t");
            }
        }
        public void update()
        {
            if (lst.Count == 0)
            {
                Console.WriteLine("\n LL is empty !");
            }
            else
            {
                Console.Write("\n Enter the position of the element to update : ");
                int i = int.Parse(Console.ReadLine());
                if (i >= 1 & i
                {
                    Console.Write("\n Enter the data to update at the position {0} : ", i);
                    string str = Console.ReadLine();
                    lst[i-1] = str;
                    Console.WriteLine("\n Data at position {0} is changed to {1}", i, str);
                }
                else
                    Console.WriteLine("\n Invalid position ! ");
             }
        }
    }
    class GenListDemo
    {
        static void Main(string[] args)
        {
            LinkedList LL = new LinkedList();
            int i=1, ch;
            while(i==1)
            {
                Console.Clear();
                Console.WriteLine("\n ************************************");
                Console.WriteLine("\n   Welcome to LINKED LIST Operations.");
                Console.WriteLine("\n ************************************");
                Console.WriteLine("\n 1-----> INSERTION");
                Console.WriteLine("\n 2-----> DELETION");
                Console.WriteLine("\n 3-----> UPDATE");
                Console.WriteLine("\n 4-----> STATUS");
                Console.WriteLine("\n 5-----> EXIT");
                Console.WriteLine("\n ************************************");
                Console.Write("\n\n Enter your choice: ");
                ch = int.Parse(Console.ReadLine());
                switch (ch)
                {
                    case 1: LL.insert();
                            break;
                    case 2: LL.delete();
                            break;
                    case 3: LL.update();
                            break;
                    case 4: LL.status();
                            break;
                    case 5: Environment.Exit(-1);
                            break;
                    default: Console.WriteLine(" Sorry !!! Wrong  choice.");
                             break;
                }
                Console.Write("\n\n Press ENTER to Continue: ");
                Console.ReadLine();
            }
            Console.WriteLine("\n End of LINKED LIST operations... Bye");
        }
    }
}

Program in C# to demonstrate Use of Virtual and override key words with a simple program

using System;

namespace Lab_Ex_13
{
    class super
    {
        protected int x;
        public super(int x)
        {
            this.x = x;
        }
        public virtual void display()
        {
            Console.WriteLine("\n Super x = " + x);
        }
    }
    class sub : super
    {
        int y;
        public sub(int x, int y) : base(x)
        {
            this.y = y;
        }
        public override void display()
        {
            Console.WriteLine("\n Sub x = " + x);     // OR base.display();
            Console.WriteLine("\n Sub y = " + y);
        }
    }
    class overridetest
    {
        public static void Main()
        {
            sub s = new sub(100, 200);
            s.display();
            Console.ReadLine();
        }
    }
}

Program in C# to Design a simple calculator using Switch Statement.


using System;

namespace Lab_Ex_12
{
    class SimpleCalculator
    {
        double num1, num2;
        public void read()
        {
            Console.WriteLine("\n Enter any two numbers:");
            Console.Write("\n Number1 : ");
            num1 = double.Parse(Console.ReadLine());
            Console.Write("\n Number2 : ");
            num2 = double.Parse(Console.ReadLine());
        }
        public void add()
        {
            double sum = num1 + num2;
            Console.WriteLine("\n Result : ({0}) + ({1}) = {2}", num1, num2, sum);
        }
        public void subtract()
        {
            double diff = num1 - num2;
            Console.WriteLine("\n Result : ({0}) - ({1}) = {2}", num1, num2, diff);
        }
        public void multiply()
        {
            double prod = num1 * num2;
            Console.WriteLine("\n Result : ({0}) X ({1}) = {2}", num1, num2, prod);
        }
        public void divide()
        {
            double qt = num1 / num2;
            Console.WriteLine("\n Result : ({0}) / ({1}) = {2}", num1, num2, qt);
        }
    }
    class ArithmeticOperations
    {
        public static void Main()
        {
            SimpleCalculator SC = new SimpleCalculator();
            int ch, i=1;
            while(i==1)
            {
                Console.Clear();
                Console.WriteLine("\n *************************");
                Console.WriteLine("\n   SIMPLE CALCULATOR.");
                Console.WriteLine("\n *************************");
                Console.WriteLine("\n 1-----> ADDITION");
                Console.WriteLine("\n 2-----> SUBTRACTION");
                Console.WriteLine("\n 3-----> MULTIPLICATION");
                Console.WriteLine("\n 4-----> DIVISION");
                Console.WriteLine("\n 5-----> EXIT");
                Console.WriteLine("\n *************************");
                Console.Write("\n\n Enter your choice: ");
                ch = int.Parse(Console.ReadLine());
                switch (ch)
                {
                    case 1: SC.read();
                            SC.add();
                            break;
                    case 2: SC.read();
                            SC.subtract();
                            break;
                    case 3: SC.read();
                            SC.multiply();
                            break;
                    case 4: SC.read();
                            SC.divide();
                            break;
                    case 5: Environment.Exit(-1);
                            break;
                    default: Console.WriteLine(" Sorry !!! Wrong  choice.");
                            break;
                }
                Console.Write("\n Press ENTER to Continue. ");
                Console.ReadLine();
            }
            Console.WriteLine("\n Cannot continue... Bye");
        }
    }
}

Name

Email *

Message *

Whoom We Are

Need to Enter Information

What We Do

Need to Enter Information