2019-10-16 18일차 Dictionary 컬렉션에 데이터추가하기 (수동으로)

Console Programming/C# Console 2019. 10. 16. 11:27

Program.cs

using System;
namespace Syntax01
{
    class Program
    {
        static void Main(string[] args)
        {
            new App();

            Console.ReadKey();
        }
    }
}

App.cs

using System;
using System.Collections.Generic;

namespace Syntax01
{
    public class App
    {
        public App()
        {
            Console.WriteLine("Hello World!");
                       
            //컬렉션 선언, 인스턴스 생성, 키와 값을 추가
            Dictionary<int, ItemData> dicItemdData = new Dictionary<int, ItemData>()
            {
                {100, new ItemData(100, "장검", 0, "sword_00")},
                {200, new ItemData(200, "가죽옷", 0, "leather_clothes_00")},
                {300, new ItemData(300, "체력 물약", 0, "health_potion_00")},
                {400, new ItemData(400, "금괴", 0, "gold_bar_00")}
            };
            //ItemData를 var itemData1 같은 변수에 받아놨다면 딕셔너리에 추가할때 dicItemdData.Add(itemData1.id, itemData1)로 추가할 수 있다


            //검색
            var foundItemData = dicItemdData[200];
            Console.WriteLine($"name:{foundItemData.name}  type:{foundItemData.type}   icon_name:{foundItemData.icon_name}");

            //제거
            dicItemdData.Remove(300);

            //출력

            foreach (KeyValuePair<int, ItemData> pair in dicItemdData)
            {
                Console.WriteLine($"pair.Key:{pair.Key}   pair.Value:{pair.Value}    {{id:{pair.Value.id}   name:{pair.Value.name}   type:{pair.Value.type}   icon_name: { pair.Value.icon_name}}}");
            }
        }
    }
}

ItemData.cs

namespace Syntax01
{
    public class ItemData
    {
        //data
        //id
        //name
        //type
        //icon_name
        public int id;
        public string name;
        public int type;
        public string icon_name;

        public ItemData(int id, string name, int type, string icon_name)
        {
            this.id = id;
            this.name = name;
            this.type = type;
            this.icon_name = icon_name;
        }
    }
}

 

 

: