You must Sign In to post a response.
  • Category: .NET

    Query in C# Interface concept

    Hi Team,

    I am learning programming skills and executing the following code


    interface first
    {
    void displayfirst();
    }
    interface second
    {
    void displaysecond();
    }

    class intimplement : first, second
    {
    public void displayfirst()
    {
    Console.WriteLine("I am first interface");
    }
    public void displaysecond()
    {
    Console.WriteLine("I am second interface");
    }
    }

    class program
    {
    static void Main(string[] args)
    {
    intimplement assign = new intimplement();
    first avalue = (first)assign;
    avalue.displayfirst();
    second bvalue = (second)assign;
    bvalue.displaysecond();
    }
    }

    when I execute the above code I am getting the expected result as

    I am first interface

    I am second interface

    However, if I remove public modifier from the class intimplement then I am getting following message

    intimplement.displaysecond()' cannot implement an interface member because it is not public.
    intimplement.displayfirst()' cannot implement an interface member because it is not public.

    By default interface members are public so why should I declare the method as Public while implementing interface.

    Please clarify. Thank you very much.
  • #765845
    Through the classes are public but Members on classes are private by default. When implementing an interface member in the class, it should be public. You have 2 basic options when implementing intefaces: implicit or explicit. Implicit implementation takes on the form of a public method or property, while explicit is in the form of a method or property prefaced with the IFoo. modifier that is otherwise not public.
    so the final answer is it should be public

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #765852
    Thank you. I am looking for free sql management studio 2012 or any version. Do you know any website or link which offers free sql management video.


Sign In to post your comments