Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- List<Room> rooms = ...
- string roomType = "lounge";
- bool exists = rooms.Exists(delegate(Room room) { return room.Type == roomType; });
- bool exists = rooms.Exists(room => room.Type == roomType);
- bool exists = rooms.Any(room => room.Type == roomType);
- if (!rooms.Any (r => r.RoomType == typeToFind /*kitchen, ...*/))
- //add it or whatever
- if(rooms.ContainsKey(room.GetType()))
- {
- // Can't add a second room of the same type
- ...
- }
- else
- {
- rooms.Add(room.GetType(), room);
- }
- void AddRoom(Room r, IList<Room> rooms, IDictionary<string, bool> roomTypes)
- {
- if (!roomTypes.Contains(r.RoomType))
- {
- rooms.Add(r);
- roomTypes.Add(r.RoomType, true);
- }
- }
- Dictionary<Type, List<Room>> rooms = new Dictionary<Type, List<Room>>;
- void Main(){
- KitchenRoom kr = new KitchenRoom();
- DummyRoom dr = new DummyRoom();
- RoomType1 rt1 = new RoomType1();
- ...
- AddRoom(kr);
- AddRoom(dr);
- AddRoom(rt1);
- ...
- }
- void AddRoom(Room r){
- Type roomtype = r.GetType();
- if(!rooms.ContainsKey(roomtype){ //If the type is new, then add it with an empty list
- rooms.Add(roomtype, new List<Room>);
- }
- //And of course add the room.
- rooms[roomtype].Add(r);
- }
Add Comment
Please, Sign In to add comment