79

I'm drawing a blank on this one and can't seem to find any previous example that I wrote. I'm trying to implement a generic interface with a class. When I implement the interface I think something isn't working right because Visual Studio continually produces errors saying that I'm not implmenting all of the methods in the Generic Interface.

Here's a stub of what I'm working with:

public interface IOurTemplate<T, U>
{
    IEnumerable<T> List<T>() where T : class;
    T Get<T, U>(U id)
        where T : class
        where U : class;
}

So what should my class look like?

1
  • 1
    Wrong question title. There are generic methods in normal classes and interfaces, and there are generic interfaces with methods. Commented Mar 14, 2014 at 6:13

3 Answers 3

122

You should rework your interface, like so:

public interface IOurTemplate<T, U>
        where T : class
        where U : class
{
    IEnumerable<T> List();
    T Get(U id);
}

Then, you can implement it as a generic class:

public class OurClass<T,U> : IOurTemplate<T,U>
        where T : class
        where U : class
{
    IEnumerable<T> List()
    {
        yield return default(T); // put implementation here
    }

    T Get(U id)
    {

        return default(T); // put implementation here
    }
}

Or, you can implement it concretely:

public class OurClass : IOurTemplate<string,MyClass>
{
    IEnumerable<string> List()
    {
        yield return "Some String"; // put implementation here
    }

    string Get(MyClass id)
    {

        return id.Name; // put implementation here
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

Perfect. I knew I had to specify the generic vars but couldn't remember where (no pun intended). Thanks Reed!
The desired outcome may not be possible but IMO this answer does not have generic methods, but rather a generic class with dependent methods. Technically it seem that generic methods would have their own template parameters.
You are right. Wrong question title, but good answer for the question.
You can use Activator to create the instance of T like this: T obj = Activator.CreateInstance<T>();
@ReedCopsey, does class in where T : class denote ANY class? This keyword is normally used for creating classes, so why is it used here? Why not write where T : object?
|
15

I think you probably want to redefine your interface like this:

public interface IOurTemplate<T, U>
    where T : class
    where U : class
{
    IEnumerable<T> List();
    T Get(U id);
}

I think you want the methods to use (re-use) the generic parameters of the generic interface in which they're declared; and that you probably don't want to make them generic methods with their own (distinct from the interface's) generic parameters.

Given the interface as I redefined it, you can define a class like this:

class Foo : IOurTemplate<Bar, Baz>
{
    public IEnumerable<Bar> List() { ... etc... }
    public Bar Get(Baz id) { ... etc... }
}

Or define a generic class like this:

class Foo<T, U> : IOurTemplate<T, U>
    where T : class
    where U : class
{
    public IEnumerable<T> List() { ... etc... }
    public T Get(U id) { ... etc... }
}

1 Comment

You can also implement this as a generic class, ie: class Foo<T,U> : IOurTemplate<T,U> -- I'm not sure which option Jason was after.
2

-- Edit

The other answers are better, but note you can have VS implement the interface for you, if you are confused as to how it should look.

Process described below.

Well, Visual Studio tells me it should look like this:

class X : IOurTemplate<string, string>
{
    #region IOurTemplate<string,string> Members

    IEnumerable<T> IOurTemplate<string, string>.List<T>()
    {
        throw new NotImplementedException();
    }

    T IOurTemplate<string, string>.Get<T, U>(U id)
    {
        throw new NotImplementedException();
    }

    #endregion
}

Note that all I did was write interface, then click on it, and wait for the little icon to popup to have VS generate the implementation for me :)

2 Comments

See: although you specialized T and U in the interface (they're both string), the methods themselves are generic with their own separate/distinct generic parameters ... which probably isn't what the OP intended.
I agree, I think your advice is good, I am was just showing how it is possible to get VS to implement the interface for you, if you're confused about how it looks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.