Saturday, April 30, 2011

SAMPLE c#.NET PROGRAMS/CODING BASED ON CONECPTS


C# Programs :

Abstract Class

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

namespace Abstract_class
{

    abstract class baseA
    {
        protected int a, b;
        public abstract void display();
    }

    class derived : baseA
    {
        public override void display()
        {
            a = 100;
            b = 200;

            Console.Write("{0},{1}", a, b);
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            derived obj = new derived();
            obj.display();
            Console.Read();
        }
    }
}

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

namespace @virtual
{
    class baseclass
    {
        public virtual void display()
        {
            Console.WriteLine("base class virtual");
        }
    }
    class derived : baseclass
    {
        public override void display()
        {
            Console.WriteLine("direvd virtual class");
          
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            //baseclass objbaseclass;
            //derived derivedobj = new derived();

            //objbaseclass = derivedobj;
            //derivedobj.display();

            derived obj = new derived();
            obj.display();

            baseclass obj1 = new baseclass();
            obj1.display();
            Console.Read();
        }
    }
}


ArrayList

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

namespace Arraylistexample
{

    class Program
    {
        static void Main(string[] args)
        {
            ArrayList objarraylist = new ArrayList();
            objarraylist.Add ('a');
            objarraylist.Add('b');
            objarraylist.Add('c');
            objarraylist.Add('x');
            objarraylist.Add('s');

            for (int i = 0; i<objarraylist.Count; i++)
                Console.WriteLine("{0}", objarraylist[i]);

            //Removing

            Console.WriteLine("After removing the element from     arraylist");
            objarraylist.Remove('b');

            for (int i = 0; i< objarraylist.Count; i++)
                Console.WriteLine("{0}", objarraylist[i]);
           
            //Sorting

            Console.WriteLine(" after sorting");
            objarraylist.Sort();
           
           
            for (int i = 0; i < objarraylist.Count; i++)
                Console.WriteLine("{0}", objarraylist[i]);

            //Capacity
            Console.WriteLine("capacity{0}", objarraylist.Capacity);


            Console.Read();

        }
    }
}



Call-By-Value

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

namespace call_by_value
{
    class callbyvalue
    {
        int a, b;
        public void show(int p, int q)
        {
            a = p;
            b = q;
            a = 100;
            b = 200;

        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            callbyvalue obj = new callbyvalue();
            int a = 10;
            int b = 20;
            obj.show(a, b);
            Console.WriteLine("{0},{1}", a, b);
            Console.Read();
          

        }
    }
}

Call-By-Reference

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

namespace call_by_reference
{
    class callbyreference
    {
        public void swap(ref int a, ref int b)
        {
            int temp = a;
            a = b;
            b = temp;

        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            callbyreference obj = new callbyreference();
            int a = 10;
            int b = 20;
            Console.WriteLine("before swapa={0},b={1}", a, b);
            obj.swap(ref a, ref b);
            Console.WriteLine("After swapa={0},b={1}", a, b);
            Console.Read();
        }
    }
}




Out-Parameters

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

namespace out_parameters
{
    class outparameters
    {
       
            public void getvalues(out  int a, out  int b)
            {
                a = 10;
                b = 20;
                int temp = a;
                a = b;
                b = temp;

            }
        }
   
    class Program
    {
        static void Main(string[] args)
        {
            outparameters  obj = new outparameters ();
            int p, q;
            obj.getvalues(out p, out q);
            Console.WriteLine("value of p={0},{1}", p, q);
            Console.Read();
        }
    }
   
}

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

namespace Enumaratons
{
    enum color { red, black, green, orange }
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("red={0}", Convert.ToInt32(color.red));
            Console.WriteLine("orange={0}",Convert.ToInt32(color.orange));
            Console.Read();
        }
    }
}
Exception Handlings

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

namespace Exception_Handlings
{
    class exceptionhandling
    {
        public static void display()
        {
            int[] num={10,20,30,40,50,60};
            int[] denom= {10,2,3,4};
   

        try
        {
            for(int i=0;i<num.Length;i++)
                Console.WriteLine("{0}",num[i]/denom[i]);

        }
        catch(indexeoutofrangeexception)
        {
             Console.WriteLine("indexeoutofrange");
        }

        catch(dividedbyzero)
        {
            Console.WriteLine("dividedbyzero");
        }

        catch(Exception ex)
        {
            string msg = ex.Message;
            Console.WriteLine("{0}", msg);

        }
        finally
        {
             Console.WriteLine("finally blocked");
        }
        }

               
         
}
    class Program
    {
        static void Main(string[] args)
        {
            exceptionhandling.display();
            Console.Read();
        }
    }
}
Generics

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

namespace Generics
{
  
    class Program
    {
        static void Main(string[] args)
        {
            object[] objgenerics = new object[10];

            objgenerics[0] = 1;
            objgenerics[1] = 2;
            objgenerics[2] = 3;
            objgenerics[3] = 4;

          
            objgenerics[4] = 45.02;
            objgenerics[5] = 58.32;
            objgenerics[6] = 25.32;

            objgenerics[7] = "prasad";
            objgenerics[8] = "lakshmi";
            objgenerics[9] = "lakshmip";

            for (int i = 0; i < 10; i++)
                Console.WriteLine("{0}", objgenerics[i]);
            Console.Read();
        }
    }
}

Interfaces

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

namespace Interfaces
{
    interface base1
    {
        void show();
        void display();

    }
    interface base2
    {
        void getvalues();
    }
    interface derivedinterface : base1, base2
    {
        void derivedinterface();
    }

    class derived : derivedinterface
    {
        public void show()
        {
            Console.WriteLine("derived class");

        }
        public void display()
        {
            Console.WriteLine("display methods in derived");

        }
        public void getvalues()
        {
            Console.WriteLine("get details method");

        }

        public void derivedinterface()
        {
            Console.WriteLine("derived interface");

        }

          
    }
    class Program
    {
        static void Main(string[] args)
        {
            derived obj = new derived();
            obj.show();
            obj.display();
            obj.getvalues();
            obj.derivedinterface();
            Console.Read();
        }
    }
}

New Keyword

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

namespace new_keyword
{
    public class baseA
    {
        protected int i=200;

    }

    public class derived : baseA
    {
        new int i = 100;
        public void show()
        {
            Console.WriteLine("{0}", i);
            Console.WriteLine("{0}", base.i);
       
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            derived obj = new derived();
            obj.show();
            Console.Read();
        }
    }
}

Partial Classes

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

namespace partial_class
{
    partial class student
    {
        int sno;
        string sname;

        public void getdetails()
        {
            sno = 100;
            sname = "prasad";
            Console.WriteLine("sno={0},sname={1}", sno, sname);
        }
    }

     partial class student
    {
        float marks;

        public void getmarks()
        {
            marks = 90;

            Console.WriteLine("marks={0}", marks);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            student obj = new student();
            obj.getdetails();
            obj.getmarks();

            Console.Read();
        }
    }
}

Sealed Class

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

namespace Sealed_class
{
    sealed class bankaccount
    {
       
        public void display()
        {
        }
    }
    class personalacc : bankaccount
    {
        public void show()
        {
            Console.WriteLine("prasad");
        }
       
    }


    class Program
    {
        static void Main(string[] args)
        {
            personalacc objbank = new personalacc();
            objbank.show();
            Console.Read();
           
           
        }
    }
}

(i)Static(No Need Create Object)
using System;
using System.Collections.Generic;
using System.Text;

namespace no_need_to_object_in_static
{
    class student
    {
        static int sno;
        static string name;
        static float marks;
        //string address;



        public static void displaystatic()
        {
            sno = 1;
            name = "prasad";
            marks = 100;

            Console.WriteLine("{0},{1},{2}", sno, name, marks);

        }

        //public void show()
        //{

        //}
    }

    class Program
    {
        static void Main(string[] args)
        {
            //student s1 = new student();
            //s1.
           
            student.displaystatic();
            Console.Read();
        }
    }
}

(ii)Static(Normal)

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

namespace normal
{
    class student
    {
        int no;

        public void display()
        {
            no = no + 1;
            Console.WriteLine("{0}", no);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            student s1 = new student();
             student s2 = new student();
            s1.display();
            s2.display();

            Console.Read();
        }
    }
}

(iii)Static(Constructor)

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

namespace static_constructor
{
    class employee
    {

        static int empno;
        static string empname;

        static employee()
        {
            empno = 1;
            empname = "prasad";


            Console.WriteLine("empno=(0),empname={1}",empno,empname);

        }

        public employee()
        {
            empno = 2;
            empname = "nan";

            Console.WriteLine("empno=(0),empname={1}",empno,empname);



        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            employee emp = new employee();

            Console.Read();

        }
    }
}

String(Immutable)
using System;
using System.Collections.Generic;
using System.Text;

namespace String_immutable_
{
    class Program
    {
        static void Main(string[] args)
        {
            string msg = "midwayinfotech pvt.ltd";
            string submsg = msg.Substring(3, 10);
            string submsg1 = msg.Substring(5, 6);

            Console.WriteLine("{0}", submsg);
            Console.WriteLine("{0}", submsg1);

            Console.Read();

        }
    }
}

Constructors

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

namespace constructor
{
    class student
    {
        int sno;
        string sname;
        float marks;
        string address;

        public student()
        {
            sno = 1;
            sname = "prasad";
            marks = 100;
            address = "Bangalore";

        }
        public void display()
        {
            Console.WriteLine("sno={1}\n sname={1}\n marks={2}\n address={3}\n", sno, sname, marks, address);
            Console.Read();
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            student st=new student ();
            st.display();
        }
    }
}

Parameterised Constructors

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

namespace parameterisedconstructors
{

    public class circle
    {
        float radius;
        //const float pi = 3.14f;
        readonly float pi;
        float area;

        public circle(float r, float pivalue)
        {
            radius = r;
           pi = pivalue;
         
        }
        public circle(float r)
        {
            radius = r;
            //pi = 3.14f;

        }
        public void calculatearea()
        {
            area = pi * radius * radius;
        }
        public void display()
        {
            Console.WriteLine("area {0}", area);
           Console .ReadLine  ();
        }
    }

        class Program
    {
        static void Main(string[] args)
        {
            circle c = new circle(10f, 5.88f);
            c.calculatearea();
            c.display();
            circle c1 = new circle(15f);
            c1.calculatearea();
            c1.display();


        }
    }
}
Rectangle(Ex:using constructors)
using System;
using System.Collections.Generic;
using System.Text;

namespace rectangle
{
    public class rectangle
    {
        float length;
        float width;
        float area;
        float circmference;
        public rectangle()
        {
            length = 15.0f;
            width = 20.3f;
        }
        public void  circumference()
        {
            length = 15.0f;
            width = 20.3f;
        }
        public void calculatearea()
        {
            area = length * width;
        }
        public void calculatecirc()
        {
            circmference = 2 * (length + width);
        }
        public void display()
        {
            Console.WriteLine("area {0}", area);
           

        }
        public void displaycirc()
        {
            Console.WriteLine("cire{0}", circmference);
          
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            rectangle r = new rectangle();
            r.calculatearea();
            r.display();
            r.calculatecirc();
            r.displaycirc();
            Console.Read();
        }
    }
}
Centigrade & fahrenheat
using System;
using System.Collections.Generic;
using System.Text;


namespace centegrade__foreingnheat
{
    public class Candf
    {
        float c = 0.0f, f = 0.0f;
        public void Calculatecentigrade(float fh)
        {
            this.f = fh;
            c = (f - 32) * 5 / 9;
            Console.Write("foreignheat is {0}--->cantigrade is {1}\n", f, c);
           
        }
        public void calculateforeignheat(float ca)
        {
            this.c = ca;
            f = c * 9 / 5 + 32;
            Console.Write("cantigrade is {0}--->foreignheat is {1}\n", c, f);
          
        }

    }
    class Program
    {
        static void Main(string[] args)
        {
            float fh,ca;
            Candf cf = new Candf();
            Console.Write("enter foreignheat ");
            fh=float.Parse(Console.ReadLine());
            cf.Calculatecentigrade(fh);
            Console.Write("Enter centigrade ");
            ca = float.Parse(Console.ReadLine());
            cf.calculateforeignheat(ca);
            Console.Read();
          
        }
    }
}

Function Overloading

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

namespace functionoverloading
{
    class student
    {
        int sno;
        string name;
        float m1, m2, m3;
        float total;
        float average;
        string result;

        public student()
        {
            sno=0;
            m1=0;
            m2=0;
            m3=0;
            name=string.Empty ;
         
         }
        public float calculate (float subject1,float subject2, float subject3)
        {
            this.m1=subject1 ;
            this .m2=subject2 ;
            this.m3=subject3 ;
            this.total=m1+m2+m3;
            return total ;

        }
        public float calculate(float sum)
        {
            average =sum/3;
            return average ;
       }
        public void calculate(float avg, int snumber, string sname)
        {
            sno = snumber;
            name = sname;
            average = avg;

            if (average <=34)
                result ="fail";
            else if (average >=35 && average <=49)
                result ="third class";
            else if (average >=50 && average <=59)
                result ="second class";
            else if (average >=60 && average <=69)
                result ="first class";
            else
                result="distinction";

        }
        public void show()
        {
            Console .Write ("{0},{1},{2},{3},{4},{5},{6},{7}", sno ,name ,m1 ,m2 ,m3 ,total ,average ,result );
            Console .Read ();

        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            student s = new student();
            float sum = s.calculate(56,41,23);
            float average = s.calculate(sum);
            s.calculate(average,101,"prasad");
            s.show();
            Console.Read();


            //student s1 = new student();
            //sum = s1.calculate(12, 56, 99);
            //average = s1.calculate(sum);
            //s1.calculate(average, 102, "raju");
            //s1.show();
            //Console.Read();
            //student s2 = new student();
            //sum = s2.calculate(99, 56, 88);
            //average = s2.calculate(sum);
            //s2.calculate(average, 103, "kumar");
            //s2.show();
        }
    }
}

Get & Set
with AreaExamples
using System;
using System.Collections.Generic;
using System.Text;

namespace area_with_get_and_set
{
    class circle
    {
        float area;
        float radius;
        const float pi = 3.14f;

        public float _Pi
        {
            get { return pi; }
        }



        public void calculate()
        {
            area = pi * radius * radius;
        }
        public void display()
        {
            Console.Write("Radius={0}", radius);
            Console.WriteLine("area={0}", area);
        }



        public float _radius
        {
            set
            {
                radius = value;
            }
            get
            {
                return radius;
            }
        }
      
    }
   
    class Program
    {
        static void Main(string[] args)
        {
            circle  ar = new circle ();
            ar._radius = 24.0f;
         

            float radius = ar._radius;
           

            ar.calculate();
            ar.display();

            float pivalue = ar._Pi;
            Console.Write("pi Value{0}", pivalue);
           
            Console.Read();
        }
    }
}






No comments:

Post a Comment