Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Passing a collection as a collection of interfaces

I have simplified and reproduced my problem in the code below. The error that I am receiving is: Argument 1: cannot convert from 'System.Collections.ObjectModel.Collection' to 'System.Collections.ObjectModel.Collection

Some background for the design decisions that led to this issue: I have created a web service for dealing with "IFruit", but due to the nature of SOAP and endpoint bindings as I understand them I have created methods with explicit implementations of IFruit. On the business layer, creating a separate method for each specific implementation of fruit would firstly cause a lot of duplicate code, and secondly couple the business and service layer tightly enough that accepting new IFruit types in the web service later would also require changing my code in the business layer (adding another copy of redundant code). This design is one I have successfully implemented using Java in the past, but C# interfaces seem to be just different enough to throw me. Please advise.

public interface IFruit{
    public string TakeBite();
}

public Apple : IFruit{
    public string TakeBite(){
        return "tasty bite of apple";
    }
}

public void EatFruit(Collection<IFruit> fruits){
    foreach(var fruit in fruits){
        Console.WriteLine(fruit.TakeBite());
    }
}

public void EatApples(Collection<Apple> apples){
    this.EatFruit(apples);
}
like image 497
YouGotCSharpInMyJava Avatar asked Jul 06 '12 17:07

YouGotCSharpInMyJava


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

How old is the letter C?

The letter c was applied by French orthographists in the 12th century to represent the sound ts in English, and this sound developed into the simpler sibilant s.


1 Answers

Try changing your EatFruit method to accept IEnumerable<IFruit>

public void EatFruit(IEnumerable<IFruit> fruits)
{
    foreach (var fruit in fruits)
    {
        Console.WriteLine(fruit.TakeBite());
    }
}

The interface IEnumerable<out T> supports covariance since it is marked with the out modifier. Collection<T> is not marked with such a modifier.

like image 94
Kevin Aenmey Avatar answered Oct 16 '22 01:10

Kevin Aenmey