Solved

covariance and contravariance

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

831 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