투케이2K

38. (C#/NET) [Mac Os] [문법] Dictionary 딕셔너리 사용해 key , value 형식 객체 생성 및 Remove 특정 요소 삭제 본문

C샵 (NET)

38. (C#/NET) [Mac Os] [문법] Dictionary 딕셔너리 사용해 key , value 형식 객체 생성 및 Remove 특정 요소 삭제

투케이2K 2024. 3. 15. 11:02
반응형

[개발 환경 설정]

개발 언어 : C# / NET

 

[소스 코드]

using System;
using System.Collections; 

namespace testProject {

    class Program {

        static void Main(string[] args){
            
            /*
            -------------------------------------------
            [요약 설명]
            -------------------------------------------
            1. Dictionary : C# 에서 KEY 와 VALUE를 사용해서 자료를 저장할 수 있습니다
            -------------------------------------------
            2. Dictionary 을 Generic 타입으로 Key 와 Value 지정 시 데이터 타입을 명시해야합니다
            -------------------------------------------
            */


            // [객체 생성]
            Dictionary<string, string> dictionary = new Dictionary<string, string>();


            // [Add 사용해 데이터 삽입]
            dictionary.Add("KEY_1", "hello");
            dictionary.Add("KEY_2", "twok");


            // [Remove 사용해 특정 key 제거]
            dictionary.Remove("KEY_1");


            // [Loop 를 돌면서 전체 데이터 확인]
            foreach (string key in dictionary.Keys){

                // [value 추출]
                string value = dictionary[key];

                // [로그 출력]
                Console.WriteLine($"");
                Console.WriteLine($"------------------------------------------");
                Console.WriteLine($"[Main] : [Log]");
                Console.WriteLine($"------------------------------------------");
                Console.WriteLine($"key : {key}");
                Console.WriteLine($"------------------------------------------");
                Console.WriteLine($"value : {value}");
                Console.WriteLine($"------------------------------------------");
                Console.WriteLine($"");

            }

        }
    }

}
 

[결과 출력]

 

 

반응형
Comments