Hot File

Create C# Interface Reference Variable

View: 235    Dowload: 0   Comment: 0   Post by: hanhga  
Author: none   Category: C / C++ / MFC   Fields: Other

0 point/0 review File has been tested

In C# a reference variable of an interface can be declared which can stores/refers to the object which implements that interface

Introduction

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
     */
}

Create C# Interface Reference Variable

Create C# Interface Reference Variable Posted on 08-09-2016  In C# a reference variable of an interface can be declared which can stores/refers to the object which implements that interface 5/10 235

Comment:

To comment you must be logged in members.

Files with category

  • How to Swap Two Numbers using Call by Reference in C

    View: 370    Download: 0   Comment: 0   Author: none  

    How to Swap Two Numbers using Call by Reference in C

    Category: C / C++ / MFC
    Fields: Other

    0/0 review
    How to swap two numbers using call by reference in C , C++. In the interviews interviewer generally asked the difference between call by reference and call by value

  • How to Print Fibonacci Series using Recursion in C, C++

    View: 376    Download: 0   Comment: 0   Author: none  

    How to Print Fibonacci Series using Recursion in C, C++

    Category: C / C++ / MFC
    Fields: Other

    4.5/1 review
    Write a program to print Fibonacci Series using recursion. Fibonacci series is a very important program in terms of interviews. To make this program little tough interviewer ask to write a fibonacci series code using recursion.

  • How to Implicitly Typed Arrays In C#

    View: 410    Download: 0   Comment: 0   Author: none  

    How to Implicitly Typed Arrays In C#

    Category: C / C++ / MFC
    Fields: Other

    0/1 review
    When we initialize these type of arrays with any data type, the compiler convert these arrays into that data type at compile time.

  • How to C# Static vs Instance Methods As Event Handlers

    View: 286    Download: 0   Comment: 0   Author: none  

    How to C# Static vs Instance Methods As Event Handlers

    Category: C / C++ / MFC
    Fields: Other

    0/0 review
    Both static and instance methods can be used as event handlers in C#

  • Build Pass By Reference To Method In C#

    View: 439    Download: 0   Comment: 0   Author: none  

    Build Pass By Reference To Method In C#

    Category: C / C++ / MFC
    Fields: Other

    0/3 review
    Like value types such as int, double, char etc. We can also pass a reference variable to the method in C# as its parameter which allows us to pass an object to the method and it is called pass by reference to method in C#.

  • How to C# Continue Statement

    View: 273    Download: 0   Comment: 0   Author: none  

    How to C# Continue Statement

    Category: C / C++ / MFC
    Fields: Other

    0/2 review
    continue statement skips or exits the current execution of a loop by given condition and keep execution continue for its next iteration unlike break statement it does not exit or terminate from loops, it terminates only its current execution

  • How to Single Dimensional Arrays In C#

    View: 212    Download: 0   Comment: 0   Author: none  

    How to Single Dimensional Arrays In C#

    Category: C / C++ / MFC
    Fields: Other

    0/1 review
    Single Dimensional Arrays in C# store each individual element at their single dimension, position or a specific location, that location is called an array index. Each index describes the position of each element within an array. Each element in an...

  • Build C# Multilevel Inheritance

    View: 236    Download: 0   Comment: 0   Author: none  

    Build C# Multilevel Inheritance

    Category: C / C++ / MFC
    Fields: Other

    2.5/2 review
    C# supports multilevel inheritance, suppose we have four classes A, B, C and D. Class A can inherit to B, B to C and C can inherit to D and so on now class D has all public members of class A, B and C include its own.

 
Newsletter Email

File suggestion for you

File top downloads

logo codetitle
Codetitle.com - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.com Develope by Vinagon .Ltd