Share via


C# generic method in Interface implementation in class

Question

Tuesday, June 14, 2011 5:52 AM

Hi,

Say, we have a interface IDataOperations with 2 methods of generic signature.

class A and B implements this interface... Now problem is

it gives error as

Cannot implicitly convert type 'System.Collections.Generic.List<MySp.A>' to 'System.Collections.Generic.List<A>'    
Cannot implicitly convert type 'System.Collections.Generic.List<MySp.B>' to 'System.Collections.Generic.List<B>'    

coz it takes class name as generic identifier...

how can i make this work?

namespace MySp
{
    public interface IDataOperation
    {

        List<T> SelectAll<T>();
    }
    
    class A : IDataOperation {

        public List<A> SelectAll<A>();
        {
             return new List<A>();
        }
    }
    
    class B : IDataOperation {

        public List<B> SelectAll<B>();
        {
             return new List<B>();
        }
    }
}

All replies (4)

Tuesday, June 14, 2011 8:40 AM ✅Answered

This is what worked

namespace MySP
{

        public interface IDataOperation<T>
        {
            List<T> SelectAll();
            T Select();
        }

        public class A : IDataOperation<A> 
        {
            public A() { }

            public List<A> get()
            {
                List<A> ff = new List<A>();
                ff.Add(new A());
                return ff;
            }

            public List<A> SelectAll() 
            {
                return get();
            }
        }

}

It seems like i am still not able to use generic method for i/p parameters i mean like this T Select<T>()...

type can be specified at interface level only i mean like IDataOperation<T>

No Generic types for arguments for method can be used in interface.

well, i found way to serve my purpose, but

what if i want something like this

interface IdataOperation<T>

{

      T Select<U>(U ID);

}

class A : IdataOperation<A>

{

      A Select<long>(long ID)

     {

         return .........A;

     }

}


Friday, June 17, 2011 3:15 AM ✅Answered

Hi,

Instead, You can try to use this class A:

public class A : IDataOperation<A>
        {
            public A() { }

            public List<A> get()
            {
                List<A> ff = new List<A>();
                ff.Add(new A());
                return ff;
            }

            public List<A> SelectAll<U>()
            {
                return get();
            }
        } 

Hope this can help you.


Tuesday, June 14, 2011 6:28 AM

public interface IDataOperation<T>
where  T:class
{

        List<T> SelectAll<T>();
    }

class A : IDataOperation<A> {

        public List<A> SelectAll<A>();
        {
             return new List<A>();
        }
    }

Tuesday, June 14, 2011 6:40 AM

Hi,

I tried it...

namespace MySP
{

        public interface IDataOperation<T>
        {
            List<T> SelectAll<T>();
            //T Select<T>();
        }

        public class A : IDataOperation<A> 
        {
            public A() { }

            public List<A> get()
            {
                List<A> ff = new List<A>();
                ff.Add(new A());
                return ff;
            }

            public List<A> SelectAll<A>() 
            {
                return get();
            }
        }

}

Getting error :
   Cannot implicitly convert type 'System.Collections.Generic.List<MySP.A>' to 'System.Collections.Generic.List<A>'