Solved

Putting derived type in interface implementation

Posted on 2011-02-22
6
333 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
LVL 63

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 63

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

623 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