투케이2K

40. (C#/NET) [Mac Os] [문법] Dictionary 딕셔너리 사용해 key , value 형식 객체 생성 및 Keys 전체 키 사이즈 및 데이터 확인 본문

C샵 (NET)

40. (C#/NET) [Mac Os] [문법] Dictionary 딕셔너리 사용해 key , value 형식 객체 생성 및 Keys 전체 키 사이즈 및 데이터 확인

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

[개발 환경 설정]

개발 언어 : 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", "world");


            // [Keys 사용해 딕셔너리에 저장된 전체 key 확인]
            List<string> keysList = new List<string>(dictionary.Keys);

            int keySize = dictionary.Keys.Count;


            // [로그 출력 수행]
            Console.WriteLine($"");
            Console.WriteLine($"------------------------------------------");
            Console.WriteLine($"[Main] : [Log]");
            Console.WriteLine($"------------------------------------------");
            Console.WriteLine($"keysList : {listValue(keysList)}");
            Console.WriteLine($"------------------------------------------");
            Console.WriteLine($"keySize : {keySize}");
            Console.WriteLine($"------------------------------------------");
            Console.WriteLine($"");

        }





        // [배열 요소 출력 print]
        public static string listValue(List<string> myList) {
            string returnData = "[";
            if (myList != null){
                for (int i = 0; i < myList.Count; i++) {
                
                    if (i != myList.Count-1){
                        returnData += myList[i] + ", ";
                    }
                    else {
                        returnData += myList[i];
                    }

                }
            }
            returnData += "]";

            return returnData;

        }

    }

}

[결과 출력]

 

 

반응형
Comments