Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

covariance and contravariance

Posted on 2013-06-26
3
Medium Priority
?
189 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1500 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

704 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