none
question about List<t>

    Question

  • I am using Visual Studio 2017, and I am writing a WPF program.  

    (for ease of understanding, I am showing Cards and CurrentCard as declared locally, but it is actually declared outside the method.)

    I have this code. 

    List<Card> Cards = new List<Card>();

    Card CurrentCard = new Card();

    ... add info to Card.

    Cards.Add( CurrentCard ); CurrentCard = null;


    The last line of code has no affect on the first and only entry in the collection of cards and though the value CurrentCard is set to null, the value in Cards[0] retains its information.

    However, if I were to alter the variable CurrentCard in a less dramatic way, then it is changed in the List.  

    My question is, why doesn't Cards[0] get set to null. 

    The line that sets the CardId to 12, does change the value in Cards[0].

    List<Card> Cards = new List<Card>();
    
    Card CurrentCard = new Card();
    
    ... add info to Card.
    
     Cards.Add( CurrentCard );         
     CurrentCard.CardId = 12

    I am confused on this and if someone could explain why it works this way, I would appreciate it.  It doesnt match how I thought things work.

     




    • Edited by danielsn Monday, April 10, 2017 9:06 PM
    Monday, April 10, 2017 9:03 PM

All replies

  • CurrentCard is a pointer that points to an instantiated object of type Card.  The line Cards.Add (CurrentCard ); isn't adding the object but another pointer of the instantiated object to the List.  This is why when you set CurrentCard to null it doesn't change the pointer that was stored in the List.  All it's doing is changing where the pointer is pointing.

    Similarly, CurrentCard.CardId = 12; changes the value of Cards[0] because both pointers are pointing to the same instantiated object.

    Monday, April 10, 2017 11:27 PM
  • I agree this sort of thing can be confusing. A diagram might help.

    • Proposed as answer by Ryan Software Tuesday, April 11, 2017 12:00 AM
    Monday, April 10, 2017 11:45 PM
  • Hi danielsn,

    Thank you for posting here.

    >>My question is, why doesn't Cards[0] get set to null.

    For your question, you use the following code to add the CurrentCard to the list. And then you could get the Cards[0].

    List<Card> Cards = new List<Card>();
    Card CurrentCard = new Card();
    ... add info to Card.
     Cards.Add( CurrentCard );   


    When you set CurrentCard to null, it is the value of CurrentCard, not for the value in list. If you want the list show the null value, use the following code to add the null to the list.

            CurrentCard = null;
                Cards.Add(CurrentCard);

    >>I am confused on this and if someone could explain why it works this way, I would appreciate it.  It doesnt match how I thought things work.

    For the code, when you debug the program, you will find that the Cards[0] is showing the value of YourProjectName.Card. The YourProjectName.Card include CardId or other properties you set in the Card class.

    When you change the value of CurrentCard.CardId, it will change the value of the CardId in list Card[0]

    I hope this would be helpful.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, April 11, 2017 6:41 AM
    Moderator