Click here to Skip to main content
15,904,155 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Please do comparison between two set of code. And explain the difference.

What I have tried:

1st set code:

C#
interface MyInterface
{
    void myMethod();
}
class MyClass : MyInterface
{
    void MyInterface.myMethod()
    {
        Console.WriteLine("welcome to MCN IT SOLUTION");
        Console.ReadLine();
    }
}
static void Main(string[] args)
{
    MyClass cls = new MyClass();
    ((MyInterface)cls).myMethod();
}


2nd set code:

C#
interface MyInterface
        {
            void myMethod();
        }
        class MyClass : MyInterface
        {           
            public void myMethod()
            {
                Console.WriteLine("welcome to MCN IT SOLUTION");
                Console.ReadLine();
            }
        }
        static void Main(string[] args)
        {
            MyClass cls = new MyClass();
            cls.myMethod();
        }


My confusion is, if I am not using interface in 2nd set of code it is giving output.
Posted
Updated 25-Aug-21 6:31am
Comments
BillWoodruff 25-Aug-21 14:45pm    
If we do your homework, you will learn nothing !

Your first example uses an explicit interface implementation:
Explicit Interface Implementation - C# Programming Guide | Microsoft Docs[^]

This is normally reserved for cases where the class implements two interfaces, and both interfaces contain a member with a specific name, but the member type or signature is not compatible.

The obvious example here is IEnumerable<T>, which implements IEnumerable. Both interfaces have a member called GetEnumerator, but the return type is different, so at least one version has to use an explicit interface implementation.
C#
public class MySequence : IEnumerable<Foo>
{
    public IEnumerator<Foo> GetEnumerator() { ... }
    IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }
}

This can also be used if you want to have a different implementation of the same member from different interfaces. But this is usually a "code smell", and indicates that there's something wrong with your design.

NB: Neither of your examples represent the "best idea to implement (an) interface", since neither of them conform to the C# naming guidelines. Interfaces should start with the letter I, and method names should be PascalCase, not camelCase.
 
Share this answer
 
Because the cast in the first set of code is unnecessary: MyClass implements MyInterface, so all of the interface methods are available to anything that uses the class instance.

If that wasn't the case, then foreach loops wouldn't work, as they require that the collection implements the IEnumerable interface. If they do, then the foreach can call GetEnumberator and iterate through the collection without caring what type of collection it is.

You can think of an interface as a "class lite" and that'll work for a good while - when you have more experience you'll start to understand why that isn't really true but it's a good analogy to start from.
 
Share this answer
 
Comments
Member 12955507 25-Aug-21 11:20am    
1st OR 2nd set, which one is the best idea to implement interface ? I think the 2nd one is best approach. Isn't it?
OriginalGriff 25-Aug-21 11:52am    
Did you read what I said?
If something is unnecessary, is there any point in using it? Or does it just confuse the reader into wondering why it is necessary?
OriginalGriff 25-Aug-21 11:54am    
Remember: interface methods are abstract - they are implemented in the "derived class" so the definition of the method isn't down to the interface itself, it just provides the method signature that must be implemented in the derived class.
Richard Deeming 25-Aug-21 12:22pm    
"... the cast in the first set of code is unnecessary ..."

Are you sure? The first set of code is using an explicit interface implementation. :)
Richard Deeming 25-Aug-21 12:24pm    
"... foreach loops wouldn't work, as they require that the collection implements the IEnumerable interface ..."

Also not technically true - they require that the class exposes a GetEnumerator method which returns something that looks like an enumerator. IIRC, this was to allow efficient strongly-typed enumerators to work before generics were introduced. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900