By 22/07/2015 9 Comments

C# Interface Reference Variable

  • In C# a reference variable of an interface can be declared which can stores/refers to the object which implements that interface.
  • When we call methods through an interface reference variable the methods which are implemented by a class will be executed.
  • An interface reference variable knows/access only the methods which is declared inside it, it does not know/access the methods or variables which is declared inside the class.

Example:

using System;
namespace csharpBasic
{
    // An interface declaration.
    public interface IMarkSheet
    {
        double GetObtainMarks();
        double GetPercentage(double obtainMarks, double totalMarks);
        double GetTotalMarks
        {
            get;
        }

        double GetAspMarks();
        double GetJsMarks();
    }

    // A MarkSheet class implements IMarkSheet interface.
    class MarkSheet : IMarkSheet
    {
        double AspMarks;
        double JavascriptMarks;
        double TotalMarks = 200;

        // double type read-only property declaration.
        public double GetTotalMarks
        {
            get
            {
                return TotalMarks;
            }
        }

        // Parametrized constructor.
        public MarkSheet(double aspMarks, double javascriptMarks)
        {
            AspMarks = aspMarks;
            JavascriptMarks = javascriptMarks;
        }

        // double return type methods return each subject mark.
        public double GetAspMarks()
        {
            return AspMarks;
        }
        public double GetJsMarks()
        {
            return JavascriptMarks;
        }

        // Return type method returns obtain marks
        public double GetObtainMarks()
        {
            return AspMarks + JavascriptMarks;
        }

        // Return type method returns percentage of obtain marks.
        public double GetPercentage(double obtainMarks, double totalMarks)
        {
            return (obtainMarks / totalMarks) * 100.0;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {

            /* NOTE the following statement which is a MarkSheet object is being 
               assigned into an interface type reference variable. */
            IMarkSheet imarkSheet = new MarkSheet(80, 70);

            Console.WriteLine("Asp.NET marks: {0}", imarkSheet.GetAspMarks());
            Console.WriteLine("Javascript marks: {0}", imarkSheet.GetJsMarks());
            double obtainMarks = imarkSheet.GetObtainMarks();
            Console.WriteLine("Total Marks {0}", imarkSheet.GetTotalMarks);
            Console.WriteLine("Obtain marks: {0}", obtainMarks);
            Console.WriteLine("Pecentage: {0}", imarkSheet.GetPercentage(obtainMarks, imarkSheet.GetTotalMarks));
            Console.ReadKey();
        }
    }
    /*  The Output will be:
     
        Asp.NET marks: 80
        Javascript marks: 70
        Total Marks 200
        Obtain marks: 150
        Pecentage: 75
     */
}





Next Tutorial →


← Previous Tutorial


Posted in: C# Advance, C#.NET

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .