Home > Software design >  How can I call a method inherited from an interface directly from a class?
How can I call a method inherited from an interface directly from a class?

Time:12-20

using System;

interface ISample{
  abstract void SampleMethod();
}

class SampleClass: ISample{
  void ISample.SampleMethod(){
    Console.WriteLine("SampleMethod was called.");
  }
}

class Program{
  public static void Main (string[] args){
    SampleClass smpcls = new SampleClass();
    smpcls.ISample.SampleMethod();
  }
}

This code works seamlessly. But I must call "ISample" interface from "smpcls" which is the instance of "SampleClass". How can I call "SampleMethod" directly from a instance of "SampleClass"?

For example:

...
SampleClass smpcls = new SampleClass();
smpcls.SampleMethod() //I would like to call it like this.
smpcls.ISample.SampleMethod() //Not like this
...

CodePudding user response:

why don't use this

void Main()
{
    SampleClass smpcls = new SampleClass();
    smpcls.SampleMethod();
}

public interface ISample
{
    public void SampleMethod();
}

public class SampleClass : ISample
{
    public void SampleMethod()
    {
        Console.WriteLine("SampleMethod was called.");
    }
}

or if have c# 8 maybe you mean this

SampleClass smpcls = new SampleClass();
smpcls.AsISampleMethod();

//or if you don't want to create an extra method

smpcls.AsISample.SampleMethod();


public interface ISample
{
    void SampleMethod()
    {
        Console.WriteLine("SampleMethod was called.");
    }
}
public class SampleClass : ISample
{
    public ISample AsISample => (ISample)this;

    public void AsISampleMethod()
    {
        AsISample.SampleMethod();
    }
}

but the interface works almost the same as an abstract class

CodePudding user response:

SampleClass explicitly implements ISample.SampleMethod, which is not what you want. Simply change it to

class SampleClass: ISample{
  void SampleMethod(){
    Console.WriteLine("SampleMethod was called.");
  }
}

CodePudding user response:

You must cast it as the interface to access a explicitly implemented interface method

ISample smpcls = new SampleClass();
smpcls.SampleMethod();

CodePudding user response:

You have to know that when implementing an interface's members there are two ways:

  1. implement interface

  2. implement interface explicitely

In your example you have selected implement interface explicitely.

void ISample.SampleMethod()

If you use this option, then you are telling the class that the method void ISample.SampleMethod() belongs to the interface and NOT to the class.

Why and when can you use explicit option? Well, suppose you have another method in your class which is called exactly SampleMethod() Now, if you implement ISample then you will have a clash with this method as ISample contains a method with the same name. So, to sovle this you use the explicit option on the interface method.

public class SampleClass : ISample
{
    void ISample.SampleMethod()
    {
        Console.WriteLine("interface method");
    }

    public void SampleMethod()
    {
        Console.WriteLine("class method");
    }
}

In your calling code, this is how you call each method:

           SampleClass smpcls = new SampleClass();
            ((ISample)smpcls).SampleMethod();

As you can see: ((ISample)smpcls).SampleMethod(); this belongs to the interface as you have to cast it to it.

The output from both methods will be as follows:

//class method
//interface method

If you do not have clashing methods, then use the option "implement interface" which will declare the code without the interface name:

public class SampleClass : ISample
{
    public void SampleMethod()
    {
        Console.WriteLine("now this method belongs to the class");
    }
}
  • Related