C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C# Expert!

Join 300,394 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,597 people online right now. Registration is fast and FREE... Join Now!




Csharp inherit question, thanks!

 

Csharp inherit question, thanks!, C# question

userpingz

11 Apr, 2009 - 04:07 AM
Post #1

New D.I.C Head
*

Joined: 11 Apr, 2009
Posts: 1

What happens if i inherit multiple interfaces and they have conficting method names?
tongue.gif

User is offlineProfile CardPM
+Quote Post


JackOfAllTrades

RE: Csharp Inherit Question, Thanks!

11 Apr, 2009 - 05:05 AM
Post #2

I exist to Google your problems.
Group Icon

Joined: 23 Aug, 2008
Posts: 4,948



Thanked: 424 times
Dream Kudos: 50
Expert In: Being annoyed with lazy people.

My Contributions
Not in the right place at all. Moving to C#.
User is offlineProfile CardPM
+Quote Post

PsychoCoder

RE: Csharp Inherit Question, Thanks!

11 Apr, 2009 - 07:41 AM
Post #3

Dyslexics Untie!
Group Icon

Joined: 26 Jul, 2007
Posts: 14,714



Thanked: 501 times
Dream Kudos: 11450
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

My Contributions
C# does not support multiple inheritance so this will not be an issue.
User is offlineProfile CardPM
+Quote Post

SigurdSuhm

RE: Csharp Inherit Question, Thanks!

11 Apr, 2009 - 10:23 AM
Post #4

D.I.C Head
**

Joined: 5 Aug, 2008
Posts: 101



Thanked: 13 times
My Contributions
QUOTE(PsychoCoder @ 11 Apr, 2009 - 07:41 AM) *

C# does not support multiple inheritance so this will not be an issue.


This was regarding interfaces in which case C# does support a kind of multiple inheritance as I'm sure you know Psycho. smile.gif

When having conflicting method names I believe you simply access them through the name of the interface name. At least that's how the compiler sets it up while making the class. So probably something like this. Should be easy to test.

CODE

MyClass myClass = new MyClass();
myClass.IMyInterface1.SomeMethod();
myClass.IMyInterface2.SomeMethod();

User is offlineProfile CardPM
+Quote Post

dkirkland

RE: Csharp Inherit Question, Thanks!

17 Jul, 2009 - 11:56 AM
Post #5

New D.I.C Head
Group Icon

Joined: 13 Apr, 2007
Posts: 17


Dream Kudos: 25
My Contributions
In C# you cannot have multiple implementation conflicts because (as stated in previous posts) it is impossible to inherit more than one class. But, when implementing multiple interfaces on a class, you can decide that a method is 'bound' to a specific interface.

This is an interesting concept which allows you to restrict a method so that it can only be used via a specific interface. This is known as explicit interface implementation.

When implementing an interface on a class you have 2 options:
1. Implicit method declaration.
CODE
public void DoSomething()

2. Explicit method declaration.
CODE
void ISomeInterface.DoSomething()


I hope the following example will help explain the concept further...

CODE
using NUnit.Framework;

namespace DreamInCode.Tests.Answers
{
    [TestFixture]
    public class InheritanceConflictTests
    {
        [Test]
        public void should_demonstrate_implicit_and_explicit_method_declarations()
        {
            InheritanceConflict viaClass = new InheritanceConflict();
            IFoo viaIFoo = new InheritanceConflict();
            IBar viaIBar = new InheritanceConflict();


            Assert.AreEqual("A()", viaClass.A());
            Assert.AreEqual("B()", viaClass.B());
            // method C() is not visible via the class

            Assert.AreEqual("A()", viaIFoo.A()); // no explicit implementation - will use class method
            Assert.AreEqual("B()", viaIFoo.B()); // no explicit implementation - will use class method

            Assert.AreEqual("IBar.B()", viaIBar.B()); // explicit implementation - will use explicit interface method
            Assert.AreEqual("IBar.C()", viaIBar.C()); // explicit implementation - will use explicit interface method
        }
    }

    public interface IFoo
    {
        string A();
        string B();
    }

    public interface IBar
    {
        string B();
        string C();
    }

    public class InheritanceConflict : IFoo, IBar
    {
        public string A() { return "A()"; }

        public string B() { return "B()"; }

        string IBar.B() { return "IBar.B()"; }

        string IBar.C() { return "IBar.C()"; }
    }
}


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 09:57PM

Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month