0% found this document useful (0 votes)
54 views

C# Notes in MVC Quesions

This document discusses how to implement methods with the same name from multiple interfaces in a C# class. It shows that simply implementing the methods does not indicate which interface each method belongs to. To resolve this, the interface name can be specified when implementing the methods, such as void A.Hello(), to indicate that method Hello belongs to interface A. The document provides an example class that implements two interfaces with identical Hello() methods by specifying the interface names when implementing the methods.

Uploaded by

Andrew Webb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

C# Notes in MVC Quesions

This document discusses how to implement methods with the same name from multiple interfaces in a C# class. It shows that simply implementing the methods does not indicate which interface each method belongs to. To resolve this, the interface name can be specified when implementing the methods, such as void A.Hello(), to indicate that method Hello belongs to interface A. The document provides an example class that implements two interfaces with identical Hello() methods by specifying the interface names when implementing the methods.

Uploaded by

Andrew Webb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Inherit Multiple Interfaces With the Same

Method Name in C#
In this article we will see a situation that occurs when two interfaces have methods with the
same name and the same parameter(s) and one base class implements these interfaces. For
example, we create interfaces that have methods with the same name. After that we create
a class that inherits from multiple interfaces.
What is Interface?
Interfaces in C# are provided as a replacement of multiple inheritance. Interface contains all
abstract methods inherited by classes and structs, which must provide an implementation
for each interface member declared. The keyword interface creates an interface. They are
public by default.
Creating Interface with the Same Method Name
Now we create interfaces named A and B. The A and B interfaces contain functions with the
same name, Hello().
interface A
{
void Hello();
}
interface B
{
void Hello();
}
Now we will see how to implement these methods in my class.
using System;
interface A
{
void Hello();
}
interface B
{
void Hello();
}
class Test : A, B
{
public void Hello()
{
Console.WriteLine("Hello to all");
}
}

public class interfacetest


{
public static void Main()
{
Test Obj1 = new Test();
Obj1.Hello();
}
}
Now Press F5 to execute the preceding program.

The preceding output creates more confusion about which method of which interface is
implemented. All the methods defined in an interface should be implemented.
We need to tell the compiler which class function we want to implement. For such cases, we
need to use the name of the interface during the implementation of a function. Have a look
at the following example:
using System;
interface A
{
void Hello();
}
interface B
{
void Hello();
}
class Test : A, B
{
void A.Hello()
{
Console.WriteLine("Hello to all-A");
}
}
public class interfacetest
{
public static void Main()
{
A Obj1 = new Test();
Obj1.Hello();
}
}
In the preceding example to tell compiler which method of which interface is implemented in
class "Test" you can use the name of the interface during the function's implementation.
Now press F5 to execute the preceding program. It will show an error.

Now we implement the method in the Test class for interface B.


using System;
interface A
{
void Hello();
}
interface B
{
void Hello();
}
class Test : A, B

{
void A.Hello()
{
Console.WriteLine("Hello to all-A");
}

void B.Hello()
{
Console.WriteLine("Hello to all-B");
}

public class interfacetest


{
public static void Main()
{
A Obj1 = new Test();
Obj1.Hello();
B Obj2 = new Test();
Obj2.Hello();
}

Note: So in your example, you are calling the "Hello" method on the "Test" class. However,
Test implements both Hello functions. Therefore, it can be treated as both for one object. So
you need to assign an object of that class to a variable defined as that specific interface.
Now press F5 to execute it.

You might also like