Solved

covariance and contravariance

Posted on 2013-06-26
3
183 Views
Last Modified: 2013-07-09
HI,

I have this interface and need to write a class which implement this interface ISimpleList<T>, using an array T[] internally to hold the elements of the list. Here is the interface:

 public interface ISimpleList<out T>
    {
        /// <summary>Adds an item to the ISimpleList<T>.</summary>
        /// <param name="item">The object to add to the ISimpleList<T>.</param>
        void Add(T item);


        /// <summary>Removes all items from the ISimpleList<T>.</summary>
        void Clear();

        /// <summary>Gets the number of elements contained in the ISimpleList<T>.</summary>
        int Count { get; }

        /// <summary>Gets or sets the element at the specified index.</summary>
        /// <param name="index">The zero-based index of the element to get or set.</param>
        /// <returns>The element at the specified index.</returns>
        T this[int index] { get; set; }

        /// <summary>Inserts an item to the ISimpleList<T> at the specified index.</summary>
        /// <param name="index">The zero-based index at which item should be inserted.</param>
        /// <param name="item">The object to insert into the ISimpleList<T>.</param>
        void Insert(int index, T item);

        /// <summary>Removes the ISimpleList<T> item at the specified index.</summary>
        /// <param name="index">The zero-based index of the item to remove.</param>
        void RemoveAt(int index);
    }

I'm not sure this covariance/Contravariance concept? Any help would grately appreciated.

Thanks in advance!!!
0
Comment
Question by:Neal18
  • 2
3 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39280048
Why are you concerned with co/contravariance?
0
 

Author Comment

by:Neal18
ID: 39280299
So how do I implement this?
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39282541
OK, I think I see now why you were concerned with co- and contravariance. You won't be able to use this class as written. The problem is because you have a mix of T being used in input (i.e. as parameter types) and output (i.e. as return types) contexts. You'd need to remove the out form the generic definition for the class to be usable; for the class to be either co- or contravariant you would have to modify one or more of the method definitions.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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 tutorial demonstrates a quick way of adding group price to multiple Magento products.

929 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