Sunday 20 December 2015

One of the best examples of interface in C#

In this given example i am going to explain how we could implement interface with member variables and methods both.

This Interface example will demonstrate how set and get value from interface and implemented class in C#.


       
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  class _Employee : IEmployee
    {
        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; } }

        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;
                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;


                default:

                    break;

            }
        }

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


            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;
                default:
                    break;
            }

            goto start;


            Console.ReadLine();

        }
    }
}