Jump to content

  • Log In with Google      Sign In   
  • Create Account


List<> remove


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 duesjov   Members   -  Reputation: 266

Like
0Likes
Like

Posted 23 October 2013 - 01:01 AM

Hello All

 

need some c# help

 

I have a list<GameState>gameStates

 

Gamestate has a Guid Id.

 

if i were to remove a gamestate from the list using remove()

 

what do i have to do?

 

can i just do something like this.

gameStates.Remove(gameState)

or do i need to use a lamba expression

and check if Id is equal to the gamestate i want to remove?

 

 

Regards Morten



Sponsor:

#2 c-Row   Members   -  Reputation: 290

Like
0Likes
Like

Posted 23 October 2013 - 02:47 AM

Remove should work with specific items.

 

http://msdn.microsoft.com/en-us/library/cd666k3e%28v=vs.80%29.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-5



#3 IndyOfComo   Members   -  Reputation: 541

Like
0Likes
Like

Posted 23 October 2013 - 07:50 AM

Would it be good advice though to pick different names?  gameState and gameStates are pretty easy to get confused.


Here is my technical background info.

#4 Melkon   Members   -  Reputation: 465

Like
1Likes
Like

Posted 23 October 2013 - 08:53 AM

Would it be good advice though to pick different names?  gameState and gameStates are pretty easy to get confused.

 

It isn't.

GameState is uppercase, so it's a class (at least you can assume it's not an object), while gameStates is lower case, so you can assume it's an object. I think it's pretty hard to get confused in that situation.


Edited by Melkon, 23 October 2013 - 08:54 AM.


#5 Eck   Members   -  Reputation: 1831

Like
0Likes
Like

Posted 23 October 2013 - 08:54 AM

Remove will work with specific items, but you should make sure to keep Reference types and Value types straight or you may not get the behavior you're expecting.

 

Let's say GameState is a Value type (C# struct). gameStateList.Remove(gameState) will remove an entry from the list if the contained gameState has an equivalent Id (and other equivalent members).

 

Now, let's say GameState is a Reference type (C# class). gameStateList.Remove(gameState) will remove an entry from the list if the contained gameState is referencing the same object.

 

In C++ terms, the struct version of GameState will get removed from the list if it's contents are the same. But in the class version of GameState, it will only be removed if the pointers are the same.

 

Depending on how you do things, you might never notice an issue. Considering GameState as a Class... If you new'ed up single instances of gameState's and used these everywhere in your game you'd probably be fine for a long time. But then if you had SaveGame logic, when you serialize your GameState's out and then load them back in, the reference types would no longer be the same reference even though they have the same guid Id internally.

 

Here's a little example:

using System;
using System.Collections.Generic;
 
namespace GameDevExample
{
class Program
{
    // Value type Game State
    public struct GameStateStruct
    {
        public Guid Id { get; set; }
    }
 
    // Reference Type Game State
    public class GameStateClass
    {
        public Guid Id { get; set; }
    }
 
    static void Main(string[] args)
    {
        // We'll use the same guid for all GameStates.
        Guid id = new Guid();
 
        // GameStateStruct is a Value type, so List.Remove will take the item out of the list if the contained item's members are equal.
        List<GameStateStruct> gameStateStructList = new List<GameStateStruct>();
        GameStateStruct gameStateStruct1;
        GameStateStruct gameStateStruct2;
 
        gameStateStruct1 = new GameStateStruct();
        gameStateStruct1.Id = id;
        gameStateStructList.Add(gameStateStruct1);
        Console.WriteLine(string.Format("Struct List contains {0} items.", gameStateStructList.Count));
 
        gameStateStruct2 = new GameStateStruct();
        gameStateStruct2.Id = id; // Assign the same Id to struct 2 as we did for struct 1
        gameStateStructList.Remove(gameStateStruct2); // And this WILL remove the item from the list.
        Console.WriteLine(string.Format("Struct List contains {0} items.", gameStateStructList.Count));
 
 
        // GameStateClass is a Reference type, so List.Remove will take the item out of the list if the contained item references the same object
        List<GameStateClass> gameStateClassList = new List<GameStateClass>();
        GameStateClass gameStateClass1;
        GameStateClass gameStateClass2;
 
        gameStateClass1 = new GameStateClass();
        gameStateClass1.Id = id;
        gameStateClassList.Add(gameStateClass1);
        Console.WriteLine(string.Format("Class List contains {0} items.", gameStateClassList.Count));
 
        gameStateClass2 = new GameStateClass();
        gameStateClass2.Id = id; // Assign the same Id to Class 2 as we did for Class 1
        gameStateClassList.Remove(gameStateClass2); // And this WILL NOT remove the item from the list.
        Console.WriteLine(string.Format("Class List contains {0} items.", gameStateClassList.Count));
 
        Console.WriteLine("Hit enter to continue");
        Console.ReadLine();
    }
}
}

Edited by Eck, 23 October 2013 - 08:59 AM.


#6 Eck   Members   -  Reputation: 1831

Like
1Likes
Like

Posted 23 October 2013 - 08:59 AM

Boo, it ripped out my summary.

 

In summary, you can use List.Remove so long as the == comparison returns true.

  • Value types are == when all of their members are ==
  • Reference types are == when they both reference the same object

And to really ground the concept, consider what would happen if you had a Reference type member in a Value type object.

 

- Eck



#7 IndyOfComo   Members   -  Reputation: 541

Like
0Likes
Like

Posted 23 October 2013 - 01:02 PM

 

can i just do something like this.

gameStates.Remove(gameState)

 

 

 

Would it be good advice though to pick different names?  gameState and gameStates are pretty easy to get confused.

 

It isn't.

GameState is uppercase, 

 

 

No, it's not.


Edited by IndyOfComo, 23 October 2013 - 01:04 PM.

Here is my technical background info.

#8 Melkon   Members   -  Reputation: 465

Like
0Likes
Like

Posted 23 October 2013 - 01:32 PM

 

 

 

Would it be good advice though to pick different names?  gameState and gameStates are pretty easy to get confused.

 

It isn't.

GameState is uppercase, 

 

 

No, it's not.

 

 

Okay, i thought you say different thing.

Well you just don't understand what he wrote. He have a container, and the remove get a GameState object... gameState probably won't have the name gameState (what is he have?), but that's doesn't really matter. It's obvious what happen, the names are fine.


Edited by Melkon, 23 October 2013 - 02:00 PM.


#9 duesjov   Members   -  Reputation: 266

Like
1Likes
Like

Posted 23 October 2013 - 02:29 PM

Boo, it ripped out my summary.

 

In summary, you can use List.Remove so long as the == comparison returns true.

  • Value types are == when all of their members are ==
  • Reference types are == when they both reference the same object

And to really ground the concept, consider what would happen if you had a Reference type member in a Value type object.

 

- Eck

Guess the reference type, of the value type objects,

should point to the same reference type 

for them to be equal?

 

 

Thanks for all the answers.

 

Been really helpfull.

 

Best regards Morten



#10 Eck   Members   -  Reputation: 1831

Like
0Likes
Like

Posted 23 October 2013 - 07:37 PM

 

Boo, it ripped out my summary.

 

In summary, you can use List.Remove so long as the == comparison returns true.

  • Value types are == when all of their members are ==
  • Reference types are == when they both reference the same object

And to really ground the concept, consider what would happen if you had a Reference type member in a Value type object.

 

- Eck

Guess the reference type, of the value type objects,

should point to the same reference type 

for them to be equal?

 

Exactly correct. :) 

 

- Eck






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS