Solved

Putting derived type in interface implementation

Posted on 2011-02-22
6
317 Views
Last Modified: 2013-12-17
Suppose I have code like below. I'm getting errors that the List<string> cannot implement the interface member because of different matching type (error CS0738). Any way to overcome this? I sometimes need to implement it as List, sometimes as HashSet etc. and I wouldn't like to have two properties for the same thing, one returning ICollection and the HashSet.

Thanks in advance
public interface MyInterface<T> {
  ICollection<T> Entries {get;}
}

public class MyClass : MyInterface<string> {
  public List<string> Entries {get; private set;}
}

Open in new window

0
Comment
Question by:bovlk
6 Comments
 
LVL 10

Expert Comment

by:John Claes
ID: 34952620
Bovlk

we're using the wrong interfaces

List inherits from IList and ICollection
http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx

an Collection inherits from IList and ICollection

The issue HashSet is not inheriting 1 of them
http://msdn.microsoft.com/en-us/library/bb495294.aspx#Y114

So this is not going to worjk using the ICOllection Interface as return Type.

Regards
poor beggar
0
 

Author Comment

by:bovlk
ID: 34952807
Both List<T> and HashSet<T> inherit ICollection<T>.
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 250 total points
ID: 34953122
You can't do it quite like that because the compiler isn't smart enough to implicitly upcast from ICollection<T> to List<T>. You have to expose the property as ICollection, but there is nothing to stop you using List<> as the underlying type:
    public class MyClass : MyInterface<string>
    {
        public ICollection<string> GetEntries { get { return wibble; } }

        private List<string> wibble;
    }

Open in new window

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 62

Expert Comment

by:Fernando Soto
ID: 34953411
Hi bovlk;

I tested your code and it seems to work for me. Can you post the actual code and at what line the compiler gives the error.

Fernando
0
 

Author Comment

by:bovlk
ID: 34955562
FernandoSoto: I created a new console app in VS 2008, .NET 3.5 and it does not compile with the following message: 'ConsoleApplication17.MyClass' does not implement interface member 'ConsoleApplication17.MyInterface<string>.Entries'. 'ConsoleApplication17.MyClass.Entries' cannot implement 'ConsoleApplication17.MyInterface<string>.Entries' because it does not have the matching return type of 'System.Collections.Generic.ICollection<string>'.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication17
{
    public interface MyInterface<T>
    {
        ICollection<T> Entries { get; }
    }

    public class MyClass : MyInterface<string>
    {
        public List<string> Entries { get; private set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}

Open in new window

0
 
LVL 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 250 total points
ID: 34956079
Hi bovlk;

I am sorry. When I created a test program I had modified your slightly. The issue with the last post is that in your interface you have the type for Entries as ICollection and in your class you have it set to List. Change List to ICollection in your class. The code snippet below makes the MyClass more generic so that you can pass in the Type at run time. My code has a List<string> passed into MyClass and another instance using a HashSet<int> using the same class def.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {

        public interface MyInterface<T>
        {
            ICollection<T> Entries { get; }
        }

        public class MyClass<T> : MyInterface<T>
        {
            public MyClass(ICollection<T> myList)
            {
                Entries = myList;
            }

            public ICollection<T> Entries { get; private set; }
        }

        static void Main(string[] args)
        {
            List<string> testList = new List<string>() {"First Element", "Second Element", "Third Element"};
            MyClass<string> mc1 = new MyClass<String>(testList);
            List<string> testList2 = mc1.Entries as List<string>;

            foreach (string listMC in testList2)
            {
                Console.WriteLine(listMC);
            }

            HashSet<int> testHashSet1 = new HashSet<int>() { 10, 123, 56, 74, 999 };
            MyClass<int> mc2 = new MyClass<int>(testHashSet1);
            HashSet<int> testHashSet2 = mc2.Entries as HashSet<int>;

            Console.WriteLine("\n");

            foreach (int hashSetMC in testHashSet2)
            {
                Console.WriteLine(hashSetMC.ToString());
            }

            Console.ReadLine();

        }
    }
}

Open in new window


Fernando
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now