Interface for generic method with one parameter which is the same type as the class itself?

I need a generic interface with one method. The method accepts a parameter which is the same type as the class itself.

e.g.

int myResult = classA.DoSomething(anotherClassA);

Open in new window


public interface MyInterface
{
    int DoSomething(? what do I put here?);
}

Open in new window

deleydSoftware EngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
E.g.

public interface IMyInterface
{
    int DoSomething(IMyInterface myInterface);
}

Open in new window

0
ste5anSenior DeveloperCommented:
p.s. lookup generics. Using "generic" is a wrong term imho in this context. Use abstraction instead.
0
it_saigeDeveloperCommented:
In a nutshell, one way you can accomplish this is to use an interface which takes a generic to indicate what DoSomething effects:
using System;
using System.Collections.Generic;
using System.Linq;

namespace EE_Q29086804
{
    class Program
    {
        static void Main(string[] args)
        {
            var activator = new SomeActivator();
            Console.WriteLine($"Adding values [1, 2, 3] = {activator.DoSomething(new SomeTarget(new[] {1, 2, 3}))}");
            Console.ReadLine();
        }
    }

    interface ISomeInterface<T>
    {
        int DoSomething(T target);
    }

    class SomeActivator : ISomeInterface<SomeTarget>
    {
        public int DoSomething(SomeTarget target)
        {
            return target.Sum();
        }
    }

    class SomeTarget
    {
        List<int> values;

        public SomeTarget(IEnumerable<int> source)
        {
            values = source.ToList();
        }

        public int Sum()
        {
            return values.Sum();
        }
    }
}

Open in new window

Which produces the following output -Capture.PNGHowever in all honesty, this is not really a good design.  Even though the Interface accepts a generic parameter, it is restricted when the Interface is implemented because the class has to specify what the generic parameter represents.

Instead of this, you should use two interfaces, one which would specify your DoSomething contract and the other to use as the parameter container for the DoSomething signature; e.g. -
using System;
using System.Collections.Generic;
using System.Linq;

namespace EE_Q29086804
{
    class Program
    {
        static void Main(string[] args)
        {
            var activator = new SomeActivator();
            Console.WriteLine($"Adding values [1, 2, 3] = {activator.DoSomething(new SomeTarget(new[] {1, 2, 3}))}");
            Console.ReadLine();
        }
    }

    interface ISomeInterface
    {
        int DoSomething(ISummable target);
    }

    interface ISummable
    {
        int Sum();
    }

    class SomeActivator : ISomeInterface
    {
        public int DoSomething(ISummable target)
        {
            return target.Sum();
        }
    }

    class SomeTarget : ISummable
    {
        List<int> values;

        public SomeTarget(IEnumerable<int> source)
        {
            values = source.ToList();
        }

        public int Sum()
        {
            return values.Sum();
        }
    }
}

Open in new window

Which produces the same output as above.

-saige-
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.