ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [C#] Dictionary Append
    C#/기초 2023. 7. 5. 11:39

     

     

    1. 사용 List<T>.ForEach() 방법

    아이디어는 두 번째 사전을 List  KeyValuePair<K,V> 그런 다음 각 항목을 다음을 사용하여 첫 번째 사전에 삽입합니다. ForEach() 방법.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    using System;
    using System.Linq;
    using System.Collections.Generic;
     
    public static class Extensions
    {
        public static void Append<K, V>(this Dictionary<K, V> first, Dictionary<K, V> second)
        {
            List<KeyValuePair<K, V>> pairs = second.ToList();
            pairs.ForEach(pair => first.Add(pair.Key, pair.Value));
        }
    }
     
    public class Example
    {
        public static void Main()
        {
            Dictionary<string, string> first = new Dictionary<string, string>
            {
                { "key1", "value1" },
                { "key2", "value2" }
            };
     
            Dictionary<string, string> second = new Dictionary<string, string>
            {
                { "key3", "value3" },
                { "key4", "value4" }
            };
     
            first.Append(second);
            Console.WriteLine(String.Join(Environment.NewLine, first));
        }
    }
     
    /*
        결과:
     
        [key1, value1]
        [key2, value2]
        [key3, value3]
        [key4, value4]
    */

    다운로드  코드 실행

     
    위의 코드는 ArgumentException 중복 키를 추가하려고 할 때.

    사전에 이미 있는 키 값을 덮어쓰려면 다음을 사용할 수 있습니다. dict[key] = value 대신 구문 Add() 방법. 이것은 아래에 설명되어 있습니다.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    using System;
    using System.Linq;
    using System.Collections.Generic;
     
    public static class Extensions
    {
        public static void Append<K, V>(this Dictionary<K, V> first, Dictionary<K, V> second)
        {
            second.ToList()
                .ForEach(pair => first[pair.Key] = pair.Value);
        }
    }
     
    public class Example
    {
        public static void Main()
        {
            Dictionary<string, string> first = new Dictionary<string, string>
            {
                { "key1", "value1" },
                { "key2", "value2" }
            };
     
            Dictionary<string, string> second = new Dictionary<string, string>
            {
                { "key2", "value" },
                { "key3", "value3" }
            };
     
            first.Append(second);
            Console.WriteLine(String.Join(Environment.NewLine, first));
        }
    }
     
    /*
        결과:
     
        [key1, value1]
        [key2, value]
        [key3, value3]
    */

    다운로드  코드 실행

    2. 사용 foreach 고리

    또한 일반 foreach 루프를 사용하여 사전을 반복하고 모든 두 번째 사전 항목을 첫 번째 사전에 추가 할 수 있습니다. 이 솔루션은 중복 키를 사용할 때 dict[key] 대신 속성 Add() 방법.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    using System;
    using System.Collections.Generic;
     
    public static class Extensions
    {
        public static void Append<K, V>(this Dictionary<K, V> first, Dictionary<K, V> second)
        {
            foreach (KeyValuePair<K, V> item in second) {
                first[item.Key] = item.Value;
            }
        }
    }
     
    public class Example
    {
        public static void Main()
        {
            Dictionary<string, string> first = new Dictionary<string, string>
            {
                { "key1", "value1" },
                { "key2", "value2" }
            };
     
            Dictionary<string, string> second = new Dictionary<string, string>
            {
                { "key2", "value" },
                { "key3", "value3" }
            };
     
            first.Append(second);
            Console.WriteLine(String.Join(Environment.NewLine, first));
        }
    }
     
    /*
        결과:
     
        [key1, value1]
        [key2, value]
        [key3, value3]
    */

     

     

     

     


    https://www.techiedelight.com/ko/add-contents-of-dictionary-to-another-dictionary-csharp/

     

    C#의 다른 사전에 사전의 내용 추가

    이 게시물은 C#에서 사전의 내용을 다른 사전에 추가하는 방법에 대해 설명합니다. 솔루션은 주어진 사전에 있는 키-값 쌍을 소스 사전에 추가해야 합니다. 1. 사용 List .ForEach() 방법 아이디어는

    www.techiedelight.com


     

    728x90

    'C# > 기초' 카테고리의 다른 글

    [C#] Attribute  (0) 2023.07.31
    [C#] XML 파일  (0) 2023.07.05
    [C#] Nuget  (0) 2023.06.26
    [C#] 레지스트리 사용법  (0) 2023.06.01
    [C#] 파일 경로, Path, SpecialFolder  (0) 2023.05.31

    댓글

Designed by Tistory.