Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
| Search | Today's Posts | Mark Forums Read
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 25th, 2009, 08:19 AM
Authorized User
 
Join Date: Apr 2007
Location: Delhi, Delhi, India.
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Default Two interfaces with Same function name

I'm executing following program, which is having two interfaces with same function name. So can anybody let me know actually which one or which interface's function is being executed or called

interface intf1
{
void get();
}
interface intf2
{
void get();
}
class A : intf1,intf2
{
public void get()
{
Console.WriteLine("Hello");

}
}



static void Main(string[] args)
{
A e = new A();
e.get();
Console.ReadLine();
}
 
Old March 25th, 2009, 08:29 AM
Friend of Wrox
 
Join Date: Mar 2007
Location: Hampshire, United Kingdom.
Posts: 432
Thanks: 0
Thanked 1 Time in 1 Post
Cool Why not try rather than ask? :)

I strongly recommend getting "Snippet Compiler" - it rocks at figuring this stuff out for yourself!

Code:
interface IOne
    {
        string GetString();
    }
    interface ITwo
    {
        string GetString();
    }
    
    class CrazyClass : IOne, ITwo
    {
        public string GetString()
        {
            return "some string";
        }
        
        string IOne.GetString()
        {
            return "string for IOne";
        }
        
        string ITwo.GetString()
        {
            return "string for ITwo";
        }
    }
    
    public static void RunSnippet()
    {
        CrazyClass c= new CrazyClass();
        IOne c1 = new CrazyClass();
        ITwo c2 = new CrazyClass();
        WL(c.GetString());
        WL(c1.GetString());
        WL(c1.GetString());
    }
See for yourself!
__________________
Rob
http://cantgrokwontgrok.blogspot.com
 
Old March 25th, 2009, 08:38 AM
Friend of Wrox
Points: 894, Level: 11
Points: 894, Level: 11 Points: 894, Level: 11 Points: 894, Level: 11
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2005
Location: London, , United Kingdom.
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
Default

They both are! Because interfaces just say what the method signature (name+parameters+return type) should look like, without any actual logic, it doesn't really matter which one is "executed". The important bit is that A.get() is run.

If one interface returned something different to the other, there is obviously more of a need to know which one to use. As this is impossible to work out however, the code would not compile.

HTH
Phil
 
Old March 25th, 2009, 08:48 AM
Friend of Wrox
 
Join Date: Mar 2007
Location: Hampshire, United Kingdom.
Posts: 432
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
Originally Posted by philip_cole View Post
They both are! Because interfaces just say what the method signature (name+parameters+return type) should look like, without any actual logic, it doesn't really matter which one is "executed". The important bit is that A.get() is run.

If one interface returned something different to the other, there is obviously more of a need to know which one to use. As this is impossible to work out however, the code would not compile.

HTH
Phil
Sorry, but this is technically incorrect.. It's not a case of they "both are", rather the ones that satisfy the interface are run.

e.g.

Implicit implementation

public string Get () { ... }

Explicit implementation

string InterfaceA.Get() { ... }
string InterfaceB.Get() { ... }

If implicit, there is only one method to call.

If explicit, then it will call the appropriate type
e.g. InterfaceA mc = new MyClass()
.. Will call the explicit InterfaceA.Get - which can be very different to InterfaceB..

Sorry to split hairs on this, but I think it is important to be clear on the actual behaviour, this kinda stuff can really slip people up! :)
In short
__________________
Rob
http://cantgrokwontgrok.blogspot.com
 
Old March 25th, 2009, 09:32 AM
Friend of Wrox
Points: 894, Level: 11
Points: 894, Level: 11 Points: 894, Level: 11 Points: 894, Level: 11
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2005
Location: London, , United Kingdom.
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
Default

Yes you are quite correct Rob, I didn't cross my mind to mention explicit implementation and was just basing it on the implicit code Manoj provided.
In his example, both interfaces have the same method signature, and class A does not provide explicit implementations for both. As you say, with implicit implementation as in this case there is only one method to call, as it satisfies both interfaces, hence my saying "they both are".
 
Old March 25th, 2009, 09:52 AM
Friend of Wrox
 
Join Date: Mar 2007
Location: Hampshire, United Kingdom.
Posts: 432
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
Originally Posted by philip_cole View Post
Yes you are quite correct Rob, I didn't cross my mind to mention explicit implementation and was just basing it on the implicit code Manoj provided.
In his example, both interfaces have the same method signature, and class A does not provide explicit implementations for both. As you say, with implicit implementation as in this case there is only one method to call, as it satisfies both interfaces, hence my saying "they both are".
OK cool, it may have just been me misinterpreting what you had said, just thought it might be a good idea to raise it just in case there were any grey areas
__________________
Rob
http://cantgrokwontgrok.blogspot.com




Similar Threads
Thread Thread Starter Forum Replies Last Post
returning interfaces mike72 BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 0 April 17th, 2008 05:53 AM
interfaces malli_kv2 Java Basics 1 April 23rd, 2007 05:21 AM
when implementing two interfaces ratheesh_param ASP.NET 1.x and 2.0 Application Design 5 November 10th, 2006 01:52 PM
[CH04] Interfaces Norm 2782 BOOK: Professional PHP 5 ISBN: 978-0-7645-7282-1 3 June 16th, 2005 04:56 AM
Polymorphism with Interfaces digby_dog VB.NET 2002/2003 Basics 2 May 11th, 2005 12:50 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.