Solved

covariance and contravariance

Posted on 2013-06-26
3
185 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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 …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

792 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