Sunday 27 December 2015

Best Example for Multiple Inheritance in c#

One Of The Best Example for Multiple Inheritance in C# is give here, Only you have copy and paste the given line of code in console application and have to press the f5 to run the code.



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

namespace Interface
{
    public interface IEmployee
    {
        int id { get; set; }
        string name { get; set; }
        int age { get; set; }

        void Print(string propertyname);
        void Set(string propertyname, object value);

    }

    public interface IBatch
    {
        int batchnumber { get; set; }
        bool Isvalid();
    }




  public  class _Employee : IEmployee , IBatch
    {
        private int _id = 0;
        private string _name = string.Empty;
        private int _age = 0;

        public int id { set { _id = value; } get { return this._id; } }
        public string name {  set { _name = value ; } get { return _name; }}
        public int age { set { _age = value ; } get { return _age; } }

        private int _batchnumber = 0;
        public int batchnumber { set { _batchnumber = value; } get { return _batchnumber; } }
        public bool Isvalid()
        {
            if (batchnumber > 1000)
            {
                return true;
            }
            else {
                return false;
            }
        }


        public void Print(string propertyname) {
            var value = "";
            switch (propertyname)
            {
                case "id":
                    {
                        value = _id.ToString();
                    }
                    break;
                case "name":
                    {
                        value = _name.ToString();
                    }
                    break;
                case "age":
                    {
                        value = _age.ToString();
                    }
                    break;
                case "batchnumber":
                    {
                        value = _batchnumber.ToString();
                    }
                    break;
                default:
                    break;
            }
            Console.WriteLine(value);

        }
        public void Set(string propertyname, object value) {
            switch (propertyname) {
                case "id":
                    {
                        try
                        {
                            var a = Convert.ToInt32((string)value);
                            this.id = a;
                            Console.WriteLine("ID:" + this.id);
                        }
                        catch(Exception exc) {
                            Console.WriteLine("Invalid value for id");
                        }
                    }
                    break;
                case "name":
                    {
                            var a =  (string)value;
                            this.name = a;
                            Console.WriteLine("NAME:" + this.name);
                    }
                    break;
                case "age":
                    {
                        try
                        {
                            var a = Convert.ToInt32((string)value);
                            this.age = a;
                            Console.WriteLine("AGE:" + this.age);
                        }
                        catch (Exception exc)
                        {
                            Console.WriteLine("Invalid value for age");
                        }
                    }
                    break;
                case "batchnumber":
                    {
                        try
                        {
                            var a = Convert.ToInt32((string)value);
                            this.batchnumber = a;
                            Console.WriteLine("BATCH NUMBER:" + this.batchnumber);
                        }
                        catch (Exception exc)
                        {
                            Console.WriteLine("Invalid value for batch number");
                        }
                    }
                    break;


                default:

                    break;

            }
        }

        static void Main(string[] args)
        {
            _Employee emp = new _Employee();
            emp.id = 1;
            emp.name = "raj";
            emp.age = 26;
            emp.batchnumber = 500;


            start:
     
            var option = Console.ReadLine();
            var arroptions = option.Split(':');

            switch (arroptions[0])
            {
                case "Print":
                    {
                        emp.Print(arroptions[1]);
                    }
                    break;
                case "Set":
                    {
                        emp.Set(arroptions[1], arroptions[2]);
                    }
                    break;
                case "Isvalid":
                    {
                       Console.WriteLine(emp.Isvalid());
                    }
                    break;
                default:
                    break;
            }



            goto start;


            Console.ReadLine();

        }
    }
}