■ Dictionary 클래스 : 이중 키 딕셔너리 사용하기

------------------------------------------------------------------------------------------------------------------------


TestProject.zip


DualKeyDictionary.cs

 

 

using System;

using System.Collections.Generic;

using System.Linq;

 

namespace TestProject

{

    /// <summary>

    /// 이중 키 딕셔너리

    /// </summary>

    /// <typeparam name="TKey1">첫번쨰 키 타입</typeparam>

    /// <typeparam name="TKey2">두번째 키 타입</typeparam>

    /// <typeparam name="TValue">값 타입</typeparam>

    public class DualKeyDictionary<TKey1, TKey2, TValue> : Dictionary<TKey1, Dictionary<TKey2, TValue>>

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 인덱서 - this[key1, key2]

 

        /// <summary>

        /// 인덱서

        /// </summary>

        /// <param name="key1">첫번째 키</param>

        /// <param name="key2">두번째 키</param>

        /// <returns></returns>

        public TValue this[TKey1 key1, TKey2 key2]

        {

            get

            {

                if(!ContainsKey(key1) || !this[key1].ContainsKey(key2))

                {

                    throw new ArgumentOutOfRangeException();

                }

 

                return base[key1][key2];

            }

            set

            {

                if(!ContainsKey(key1))

                {

                    this[key1] = new Dictionary<TKey2, TValue>();

                }

 

                this[key1][key2] = value;

            }

        }

 

        #endregion

        #region 값 열거형 - Values

 

        /// <summary>

        /// 값 열거형

        /// </summary>

        public new IEnumerable<TValue> Values

        {

            get

            {

                return from baseDictionary in base.Values

                       from baseKey        in baseDictionary.Keys

                       select baseDictionary[baseKey];

            }

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 추가하기 - Add(key1, key2, value)

 

        /// <summary>

        /// 추가하기

        /// </summary>

        /// <param name="key1">첫번째 키</param>

        /// <param name="key2">두번째 키</param>

        /// <param name="value"></param>

        public void Add(TKey1 key1, TKey2 key2, TValue value)

        {

            if(!ContainsKey(key1))

            {

                this[key1] = new Dictionary<TKey2, TValue>();

            }

 

            this[key1][key2] = value;

        }

 

        #endregion

        #region 키 포함 여부 구하기 - ContainsKey(key1, key2)

 

        /// <summary>

        /// 키 포함 여부 구하기

        /// </summary>

        /// <param name="key1">첫번째 키</param>

        /// <param name="key2">두번쨰 키</param>

        /// <returns>키 포함 여부</returns>

        public bool ContainsKey(TKey1 key1, TKey2 key2)

        {

            return base.ContainsKey(key1) && this[key1].ContainsKey(key2);

        }

 

        #endregion

    }

}

 

 

Program.cs

 

 

using System;

 

namespace TestProject

{

    /// <summary>

    /// 프로그램

    /// </summary>

    class Program

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Public

 

        #region 프로그램 시작하기 - Main()

 

        /// <summary>

        /// 프로그램 시작하기

        /// </summary>

        private static void Main()

        {

            DualKeyDictionary<string, string, int> dictionary = new DualKeyDictionary<string, string, int>();

 

            dictionary.Add("남자", "홍길동", 80);

            dictionary.Add("남자", "김철수", 50);

            dictionary.Add("여자", "이영희", 90);

 

            foreach(int value in dictionary.Values)

            {

                Console.WriteLine(value);

            }

        }

 

        #endregion

    }

}

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요