How to implement explicit interface in C#

1 Answer

0 votes
using System;

namespace ConsoleApplication_C_Sharp
{
    interface ISize
    {
        float Height();
        float Width();
    }
    class Program : ISize
    {
        float height;
        float width;

        public Program(float height, float width)
        {
            this.height = height;
            this.width = width;
        }
        // Explicit interface implementation
        float ISize.Height()
        {
            return height;
        }
        // Explicit interface implementation
        float ISize.Width()
        {
            return width;
        }
        static void Main(string[] args)
        {
            Program obj = new Program(3.14f, 5.18f);

            ISize myDimensions = (ISize)obj;
            Console.WriteLine("Height = {0}", myDimensions.Height());
            Console.WriteLine("Width = {0}", myDimensions.Width());
        }
    }
}


/*
run:

Height = 3.14
Width = 5.18

*/

 



answered Apr 27, 2017 by avibootz
edited Apr 27, 2017 by avibootz

Related questions

1 answer 171 views
1 answer 193 views
193 views asked Jan 7, 2016 by avibootz
1 answer 121 views
121 views asked Sep 11, 2020 by avibootz
1 answer 207 views
207 views asked Sep 11, 2020 by avibootz
2 answers 237 views
237 views asked Apr 25, 2017 by avibootz
1 answer 160 views
160 views asked Dec 21, 2016 by avibootz
...