Menu Close

Blog

Slide 1

Microsoft Business Applications Blogposts, YouTube Videos and Podcasts

Helping Businesses with Technology

Slide 2

Microsoft Business Applications Blogposts, YouTube Videos and Podcasts

Helping Businesses with Technology

Slide 3

Microsoft Business Applications Blogposts, YouTube Videos and Podcasts

Helping Businesses with Technology

previous arrow
next arrow

POLYMORPHISM IN C# WITH SAMPLE EXAMPLE

Polymorphism is a primary pillars of object oriented programming

Polymorphism allow you to invoke derived class methods through a base class reference during runtime. 

In the base class the method is declared as virtual and in the derived class we override the same method with “override keyword”.

The virtual keyword indicates, the method can be overridden in any derived class. 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConsoleApplication4;

namespace ConsoleApplication4
{

    /// <summary>
    /// Polymorphism example
    /// </summary>
    public class Employee
    {
        public string FirstName = “FN”;
        public string LastName = “LN”;
     

        public virtual void PrintFullName()
        {
            Console.WriteLine(FirstName + ” ” + LastName);
        }

    }

    public class FullTimeEmployee : Employee
    {
        public override void PrintFullName()
        {
            Console.WriteLine(FirstName + ” ” + LastName + “- FullTimeEmployee”);
        }
     
    }
    public class PartTimeEmployee : Employee
    {
        public override void PrintFullName()
        {
            Console.WriteLine(FirstName + ” ” + LastName + ” – PartTimeEmployee”);
        }
    }
    public class TemporaryEmployee : Employee
    {
        public override void PrintFullName()
        {
            Console.WriteLine(FirstName + ” ” + LastName + ” – TemporaryEmployee”);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
           Employee[] employees = new Employee[4];

           employees[0] = new FullTimeEmployee();
           employees[1] = new PartTimeEmployee();
           employees[2] = new TemporaryEmployee();
           employees[3] = new Employee();
           foreach (Employee e in employees)
           {
               e.PrintFullName();
               Console.ReadLine();
           }      

        }

    }
}

Share this:

METHOD HIDING IN C# WITH SAMPLE EXAMPLE

## METHOD HIDING

## INVOKE BASE CLASS MEMBERS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConsoleApplication4;

namespace ConsoleApplication4
{
    public class Employee
    {
        public string FirstName;
        public string LastName;
        public string Email;

        public void PrintFullName()
        {
            Console.WriteLine(FirstName + ” ” + LastName);
        }
    }

    public class PartTimeEmployee : Employee
    {
        public new void PrintFullName()
        {
            Console.WriteLine(FirstName + ” ” + LastName  +  ” — Contractor”);
        }
    }

    public class FullTimeEmployee : Employee
    {

    }
    class Program
    {
        static void Main(string[] args)
        {
           Employee PTE = new PartTimeEmployee();
            PTE.FirstName = “PART TIME “;
            PTE.LastName  =  “EMPLOYEE”;
            PTE.PrintFullName();
            Console.ReadLine();

            FullTimeEmployee FTE = new FullTimeEmployee();
            FTE.FirstName = “FULL TIME”;
            FTE.LastName = “EMPLOYEE”;
            FTE.PrintFullName();
            Console.ReadLine();
        }

    }
}

Share this:

INHERITANCE IN C# WITH SAMPLE EXAMPLE

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConsoleApplication4;

namespace ConsoleApplication4
{
    /// <summary>
    /// Base class Employee( Inheritance example)
    /// </summary>
    public class Employee
    {
        public string FirstName;
        public string LastName;
        public string Email;
   
        public void PrintFullName()
        {
            Console.WriteLine(FirstName + ” ” + LastName);
        }
    }
    /// <summary>
    /// Derived class inheriting Employee
    /// </summary>
    public class FullTimeEmployee : Employee
    {

      public  float YearlySalary;
    }
    /// <summary>
    /// Derived class inherting Employee
    /// </summary>

    public class PartTimeEmployee :  Employee
    {
     public  float HourlyRate;

    }
    class Program
    {
        static void Main(string[] args)
        {
            FullTimeEmployee FTE = new FullTimeEmployee();
            FTE.FirstName = “Mitali”;
            FTE.LastName = “Gurram”;
            FTE.Email = “malla.gurram@gmail.com”;
            FTE.YearlySalary = 100000;
            FTE.PrintFullName();
            Console.ReadLine();

            PartTimeEmployee PTE = new PartTimeEmployee();
            PTE.FirstName = “GMR”;
            PTE.LastName = “IT SOLUTIONS”;
            PTE.HourlyRate = 90;
            PTE.PrintFullName();
            Console.ReadLine();
        }

    }
}

=============
DERIVED CLASS  CAN CONTROL BASE CLASS CONSTRUCTORS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConsoleApplication4;

namespace ConsoleApplication4
{
    public class ParentClass
    {     //parent constructor
        public ParentClass()
        {
            Console.WriteLine(” parentclass constructor called”);
            Console.ReadLine();
        }
        public ParentClass(string Message)
        {
            Console.WriteLine(Message);
            Console.ReadLine();
        }
    }

    public class ChildClass : ParentClass
    {    //childclass constructor
        public ChildClass() :  base(“Derived class controlling parent class”)
        {
            Console.WriteLine(“childclass constructor called”);
             Console.ReadLine();
        }
    }

   
    class Program
    {
        static void Main(string[] args)
        {
           ChildClass CC = new ChildClass();
           
        }

    }
}

Share this:

STATIC AND INSTANCE CLASS MEMBERS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication4
{

    class Circle
    {
       static float _PI;
        int _Radius;

        static Circle()
        {
            Circle._PI = 3.141F;
        }

        public Circle(int Radius)
        {
            this._Radius = Radius;
        }

        public float CalculateArea()
        {
            return Circle._PI * this._Radius * this._Radius;
        }

    }
    class Program
    {
        static void Main(string[] args)
        {
            Circle C1 = new Circle(5);
            float Area1 = C1.CalculateArea();
            Console.WriteLine(“Area = {0}”, Area1);
            Console.ReadLine();

            Circle C2 = new Circle(6);
            float Area2 = C2.CalculateArea();
            Console.WriteLine(“Area = {0}”, Area2);
            Console.ReadLine();
        }
    }
}

Share this:

METHODS

Methods can have attributes, access modifiers, return types, method names, parameters and methods..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
    class Program
    {

        static void Main(string[] args)
        {
            Program p = new Program();
            p.EvenNumbers();
            int Sum = p.Add(10, 20);
            Console.WriteLine(“Sum={0}”, Sum);
            Console.ReadLine();
        }

        public int Add(int F ,int L)
        {
            return F + L;
        }
 
        public void EvenNumbers()
        {
            int start = 0;

            while (start <= 20)
            {
                Console.WriteLine(start);
                Console.ReadLine();
                start = start + 2;
             
            }
        }
    }
}
         

================
OUTPUT PARAMETERS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            int Total = 0;
            int Product = 0;
            Calculate(10, 20, out Total, out Product);
            Console.WriteLine(“sum = {0} && product = {1}”,Total,Product);
            Console.ReadLine();
        }
        public static void Calculate(int FN, int LN, out int sum, out int product)
        {
            sum = FN + LN;
            product = FN * LN;
        }
    }
}
Share this: