¡¡Character ¤È Item ¥¯¥é¥¹¤ò»È¤Ã¤ÆÎã¤ò¼¨¤·¤Þ¤¹¡£
¡¡¤Þ¤º¡¢¥²¡¼¥àÆâ¤Ç¥¥ã¥é¥¯¥¿¡¼¤Î¥ê¥¹¥È¤ò´ÉÍý¤·¡¢¤½¤ì¤é¤Î¥¥ã¥é¥¯¥¿¡¼¤¬»ý¤Ã¤Æ¤¤¤ë¥¢¥¤¥Æ¥à¤Î¥ê¥¹¥È¤ò°·¤¦¥·¥Á¥å¥¨¡¼¥·¥ç¥ó¤ò¹Í¤¨¤Æ¤ß¤Þ¤·¤ç¤¦¡£
¡¡°Ê²¼¤Î¤è¤¦¤Ê¥¯¥é¥¹¹½Â¤¤òÁÛÄꤷ¤Þ¤¹¡£
public class Item
{
public string Name { get; set; }
public int Value { get; set; }
}
public class Character
{
public string Name { get; set; }
public List<Item> Items { get; set; }
}
¡¡¤³¤³¤Ç¤Ï¡¢Character¤Î¥ê¥¹¥È¤«¤é¡¢Ì¾Á°¤¬ÆÃÄê¤Îʸ»úÎó¤Ë°ìÃפ¹¤ëºÇ½é¤ÎCharacter¤òõ¤¹½èÍý¤ò¹Í¤¨¤Þ¤¹¡£
¡¡¤Þ¤º¤Ï½é´ü²½¤ò¹Ô¤¤¡¢List ¤Î½àÈ÷¤òÀ°¤¨¤Þ¤¹¡£
// ¥¢¥¤¥Æ¥à¤ÎºîÀ®
Item sword = new Item { Name = "Sword", Value = 100 };
Item shield = new Item { Name = "Shield", Value = 150 };
Item potion = new Item { Name = "Potion", Value = 50 };
// ¥¥ã¥é¥¯¥¿¡¼¤ÎºîÀ®¤È¥¢¥¤¥Æ¥à¤Î³ä¤êÅö¤Æ
Character john = new Character
{
Name = "John",
Items = new List<Item> { sword, shield }
};
Character mary = new Character
{
Name = "Mary",
Items = new List<Item> { potion }
};
Character tom = new Character
{
Name = "Tom",
Items = new List<Item> { sword, potion }
};
// ¥¥ã¥é¥¯¥¿¡¼¤ò¥ê¥¹¥È¤Ë³ÊǼ
List<Character> characters = new List<Character> { john, mary, tom };
¡¡¤³¤Î¥µ¥ó¥×¥ë¥³¡¼¥É¤Ç¤Ï¡¢3¤Ä¤Î¥¥ã¥é¥¯¥¿¡¼¡ÊJohn, Mary, Tom¡Ë¤òºîÀ®¤·¡¢¤½¤ì¤¾¤ì¤Ë°Û¤Ê¤ë¥¢¥¤¥Æ¥à¤ò³ä¤êÅö¤Æ¤Æ¤¤¤Þ¤¹¡£
¤½¤Î¸å¡¢ºîÀ®¤·¤¿¥¥ã¥é¥¯¥¿¡¼¤òcharacters¤È¤¤¤¦Ì¾Á°¤Î¥ê¥¹¥È¤Ë³ÊǼ¤·¤Þ¤¹¡£
¡¡¤³¤ì¤Ç¡¢Find ¥á¥½¥Ã¥É¤ò»È¤Ã¤ÆÆÃÄê¤Î¥¥ã¥é¥¯¥¿¡¼¤ä¤½¤Î½ê»ý¥¢¥¤¥Æ¥à¤ò¸¡º÷¤¹¤ë½àÈ÷¤¬¤Ç¤¤Þ¤·¤¿¡£