Solved

covariance and contravariance

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

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 74

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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 …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
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…

747 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

18 Experts available now in Live!

Get 1:1 Help Now