Click here to Skip to main content
15,885,278 members
Home / Discussions / C#
   

C#

 
AnswerRe: Need help with co/contravariance and generic lists :s Pin
Kubajzz5-Sep-10 23:50
Kubajzz5-Sep-10 23:50 
GeneralRe: Need help with co/contravariance and generic lists :s [modified] Pin
Lee Reid6-Sep-10 0:41
Lee Reid6-Sep-10 0:41 
GeneralRe: Need help with co/contravariance and generic lists :s Pin
Kubajzz6-Sep-10 1:06
Kubajzz6-Sep-10 1:06 
GeneralRe: Need help with co/contravariance and generic lists :s Pin
Lee Reid6-Sep-10 2:02
Lee Reid6-Sep-10 2:02 
GeneralRe: Need help with co/contravariance and generic lists :s Pin
Kubajzz6-Sep-10 2:13
Kubajzz6-Sep-10 2:13 
AnswerRe: Need help with co/contravariance and generic lists :s Pin
DaveyM696-Sep-10 0:41
professionalDaveyM696-Sep-10 0:41 
GeneralRe: Need help with co/contravariance and generic lists :s Pin
Lee Reid6-Sep-10 1:12
Lee Reid6-Sep-10 1:12 
GeneralRe: Need help with co/contravariance and generic lists :s Pin
DaveyM696-Sep-10 1:31
professionalDaveyM696-Sep-10 1:31 
Sort of. The item references are copied so any changes to an element in listB will be reflected in listA.
Test Code:
C#
using System;
using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {
        Derived derived1 = new Derived(1);
        Derived derived2 = new Derived(2);
        List<Derived> listDerived = new List<Derived>(new Derived[] { derived1, derived2 });
        // Create Base list
        List<Base> listBase = DerivedConverter<Base, Derived>(listDerived);
        // Make change to derived list
        listDerived[0].ID = 0;
        for (int i = 0; i < listBase.Count; i++)
        {
            Console.WriteLine("{0} {1}",
                object.ReferenceEquals(listBase[i], listDerived[i]),
                listBase[i].ID);
        }
        Console.ReadKey();
    }
    public static List<TBase> DerivedConverter<TBase, TDerived>(List<TDerived> derivedList)
            where TDerived : TBase
    {
        return derivedList.ConvertAll<TBase>(
               new Converter<TDerived, TBase>(delegate(TDerived derived)
               {
                   return derived;
               }));
    }
}

public class Base
{
    public Base(int id)
    {
        ID = id;
    }
    public int ID { get; set; }
}

public class Derived : Base
{
    public Derived(int id)
        : base(id)
    { }
}

If you add or remove items to listB then that won't be reflected in listA unless you rebuild it dynamically.
Dave

If this helped, please vote & accept answer!


Binging is like googling, it just feels dirtier.
Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)

BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

GeneralRe: Need help with co/contravariance and generic lists :s Pin
DaveyM696-Sep-10 1:54
professionalDaveyM696-Sep-10 1:54 
GeneralRe: Need help with co/contravariance and generic lists :s Pin
Lee Reid6-Sep-10 2:08
Lee Reid6-Sep-10 2:08 
GeneralRe: Need help with co/contravariance and generic lists :s Pin
DaveyM696-Sep-10 2:13
professionalDaveyM696-Sep-10 2:13 
GeneralRe: Need help with co/contravariance and generic lists :s Pin
Pete O'Hanlon6-Sep-10 3:41
mvePete O'Hanlon6-Sep-10 3:41 
AnswerRe: Need help with co/contravariance and generic lists :s Pin
PIEBALDconsult6-Sep-10 7:32
mvePIEBALDconsult6-Sep-10 7:32 
QuestionHow to deploy an Outlook 2003 add-in application(C# and VS 2008)? Pin
milestanley5-Sep-10 22:57
milestanley5-Sep-10 22:57 
QuestionCould use some help please. Pin
pestman5-Sep-10 22:01
pestman5-Sep-10 22:01 
AnswerRe: Could use some help please. Pin
Kubajzz5-Sep-10 22:21
Kubajzz5-Sep-10 22:21 
GeneralRe: Could use some help please. Pin
pestman6-Sep-10 6:27
pestman6-Sep-10 6:27 
GeneralRe: Could use some help please. Pin
Kubajzz6-Sep-10 9:51
Kubajzz6-Sep-10 9:51 
Question"Cannot connect back" - Visual Studio 2008 Remote Debugger for C# apps Pin
Maxwell Chen5-Sep-10 20:19
Maxwell Chen5-Sep-10 20:19 
QuestionQuestion about RegEx Class Pin
Mazdak5-Sep-10 16:24
Mazdak5-Sep-10 16:24 
Answercross-post Pin
Luc Pattyn5-Sep-10 16:54
sitebuilderLuc Pattyn5-Sep-10 16:54 
AnswerRe: Question about RegEx Class [modified] Pin
PIEBALDconsult5-Sep-10 17:34
mvePIEBALDconsult5-Sep-10 17:34 
QuestionNot able to delete original jpeg file after it is copied with System.IO.File.Copy() Pin
Aseem Sharma5-Sep-10 2:39
Aseem Sharma5-Sep-10 2:39 
AnswerRe: Not able to delete original jpeg file after it is copied with System.IO.File.Copy() Pin
Not Active5-Sep-10 3:04
mentorNot Active5-Sep-10 3:04 
GeneralRe: Not able to delete original jpeg file after it is copied with System.IO.File.Copy() Pin
Aseem Sharma5-Sep-10 4:19
Aseem Sharma5-Sep-10 4:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.