• Status: Solved
  • Priority: High
  • Security: Public
  • Views: 51
  • Last Modified:

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

0
deleyd
Asked:
deleyd
  • 2
1 Solution
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now