Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# generics and interfaces and simple OO

Tags:

c#

oop

generics

My C# skills are low, but I can't understand why the following fails:

public interface IQuotable {}
public class Order : IQuotable {}
public class Proxy {
  public void GetQuotes(IList<IQuotable> list) { ... }
}

Then the code is as follows:

List<Order> orders = new List<Orders>();
orders.Add(new Order());
orders.Add(new Order());

Proxy proxy = new Proxy();
proxy.GetQuotes(orders); // produces compile error

Am I simply doing something wrong and not seeing it? Since Order implements Quotable, a list of order would go in as IList of quoatables. I have something like in Java and it works, so I'm pretty sure its my lack of C# knowledge.

like image 618
Daniil Avatar asked Jan 11 '13 16:01

Daniil


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.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

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.


2 Answers

You can't convert from a List<Order> to an IList<IQuotable>. They're just not compatible. After all, you can add any kind of IQuotable to an IList<IQuotable> - but you can only add an Order (or subtype) to a List<Order>.

Three options:

  • If you're using .NET 4 or higher, you can use covariance if you change your proxy method to:

    public void GetQuotes(IEnumerable<IQuotable> list)
    

    This only work if you only need to iterate over the list, of course.

  • You could make GetQuotes generic with a constraint:

    public void GetQuotes<T>(IList<T> list) where T : IQuotable
    
  • You could build a List<IQuotable> to start with:

    List<IQuotable> orders = new List<IQuotable>();
    orders.Add(new Order());
    orders.Add(new Order());
    
like image 188
Jon Skeet Avatar answered Sep 22 '22 16:09

Jon Skeet


IList is not covariant. You can't cast a List<Order> to an IList<Quotable>.

You can change the signature of GetQuotes to:

public void GetQuotes(IEnumerable<IQuotable> quotes)

Then, materialize a list (if you need its features), through:

var list = quotes.ToList();
like image 33
Mir Avatar answered Sep 21 '22 16:09

Mir