c collections arraylist
C # 컬렉션은 데이터를 저장하고 수정하는 특수 클래스입니다. 이 자습서에서는 예제를 통해 ArrayList, HashTable 및 SortedList와 같은 C # 컬렉션에 대해 학습합니다.
동적 메모리 할당, 비 순차적 데이터 평가 등과 같은 다양한 용도로 사용됩니다.
이러한 모든 클래스는 다른 모든 데이터 유형의 기본 클래스 인 객체 클래스를 사용합니다.
학습 내용 :
C # 컬렉션 및 그 사용법
C # ArrayList
ArrayList는 C # 컬렉션의 일부입니다. 주어진 데이터 유형의 데이터를 포함하는 데 사용됩니다. C #의 배열과 유사하지만 명확한 크기가 없습니다. 더 많은 요소가 추가되면 크기가 자동으로 증가합니다.
ArrayList를 초기화하는 방법?
ArrayList는“ArrayList”키워드를 사용하여 초기화 할 수 있습니다.
ArrayList arrList = new ArrayList();
ArrayList에 요소를 추가하는 방법?
Add () 메서드를 사용하여 배열 목록에 단일 항목을 추가하고 AddRange () 메서드를 사용하여 다른 컬렉션의 여러 요소 또는 여러 요소를 추가 할 수 있습니다.
예:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Collections; namespace ConsoleApp1 { class Program { static void Main(string() args) { ArrayList arrList = new ArrayList(); arrList.Add(7896); arrList.Add('Seven'); Console.ReadLine(); } } }
여기에서 ArrayList.Add () 메서드를 사용했음을 알 수 있습니다. 보시다시피 동일한 배열 목록에 정수와 문자열을 모두 추가했습니다. 이것은 배열 목록이 제네릭이 아닌 데이터 유형이기 때문에 가능합니다. 즉, 주어진 데이터 유형의 요소를 포함 할 수 있습니다.
ArrayList에서 요소에 액세스하는 방법?
배열 목록 요소는 인덱스를 사용하여 배열과 유사하게 액세스 할 수 있습니다. 그러나 제네릭이 아닌 유형이므로 먼저 적절한 데이터 유형으로 캐스트하거나 배열 목록에서 값에 액세스하는 동안 var 키워드를 사용해야합니다.
예:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Collections; namespace ConsoleApp1 { class Program { static void Main(string() args) { ArrayList arrList = new ArrayList(); arrList.Add(7896); arrList.Add('Seven'); //casted and retrieved data int first_index = (int)arrList(0); string second_index = (string)arrList(1); Console.WriteLine('The first index value is : ' + first_index); Console.WriteLine('The second index value is : ' + second_index); Console.ReadLine(); } } }
위의 예에서는 배열 목록의 인덱스를 사용하여 데이터를 검색 한 다음 적절한 데이터 유형으로 캐스트했습니다. 그런 다음 결과가 콘솔에 출력으로 인쇄되었습니다.
위 프로그램의 출력은 다음과 같습니다.
산출
첫 번째 인덱스 값 : 7896
두 번째 인덱스 값 : Seven
ArrayList에 요소를 삽입하는 방법?
지정된 지점 또는 인덱스에서 ArrayList의 요소를 삽입합니다. Insert () 메서드가 사용됩니다.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Collections; namespace ConsoleApp1 { class Program { static void Main(string() args) { ArrayList arrList = new ArrayList(); arrList.Add(7896); arrList.Add('Seven'); //casted and retrieved data int first_index = (int)arrList(0); string second_index = (string)arrList(1); Console.WriteLine('The first index value is : ' + first_index); Console.WriteLine('The second index value is : ' + second_index); arrList.Insert(1, 'Eight'); second_index = (string)arrList(1); Console.WriteLine('The second index value is : ' + second_index); Console.ReadLine(); } } }
따라서 insert () 메서드를 사용하여 인덱스 1에 새 문자열을 삽입했습니다. 따라서 위의 프로그램을 실행하면 다음과 같은 출력이 표시됩니다.
산출
첫 번째 인덱스 값 : 7896
두 번째 인덱스 값 : Seven
두 번째 인덱스 값 : 8
ArrayList에서 요소를 제거하는 방법?
Remove () 메서드를 사용하여 ArrayList에서 요소를 제거 할 수 있습니다. Remove 메서드는 매개 변수, 즉 배열에서 제거해야하는 값을받습니다.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Collections; namespace ConsoleApp1 { class Program { static void Main(string() args) { ArrayList arrList = new ArrayList(); arrList.Add(7896); arrList.Add('Seven'); //casted and retrieved data int first_index = (int)arrList(0); string second_index = (string)arrList(1); Console.WriteLine('The first index value is : ' + first_index); Console.WriteLine('The second index value is : ' + second_index); arrList.Insert(1, 'Eight'); second_index = (string)arrList(1); Console.WriteLine('The second index value is : ' + second_index); arrList.Remove(7896); var data = arrList(0); Console.WriteLine('The value at 0 index is : ' + data); Console.ReadLine(); } } }
remove 메소드는 목록에서 주어진 값을 제거합니다. 지정된 인덱스에서 값이 제거되면 후속 값이 한 인덱스 위로 이동하여 간격을 채 웁니다. 인덱스 0 개를 제거하면 인덱스 1의 값이 이동하여 0에서 빈 공간을 채 웁니다.
다음 프로그램의 출력은 다음과 같습니다.
산출
첫 번째 인덱스 값 : 7896
두 번째 인덱스 값 : Seven
두 번째 인덱스 값 : 8
0 인덱스의 값은 다음과 같습니다.
색인을 사용하여 목록 요소를 제거하는 방법?
사용자는 RemoveAt () 메서드를 사용하여 특정 인덱스에서 목록 요소를 제거 할 수 있습니다. RemoveAt ()은 요소를 제거해야하는 인덱스 번호가있는 단일 매개 변수를 허용합니다. Remove 메서드와 마찬가지로 중간에서 요소를 제거하면 다음 요소를 밀어 한 단계 위로 이동하여 간격을 채 웁니다.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Collections; namespace ConsoleApp1 { class Program { static void Main(string() args) { ArrayList arrList = new ArrayList(); arrList.Add(7896); arrList.Add('Seven'); //casted and retrieved data int first_index = (int)arrList(0); string second_index = (string)arrList(1); Console.WriteLine('The first index value is : ' + first_index); Console.WriteLine('The second index value is : ' + second_index); arrList.Insert(1, 'Eight'); second_index = (string)arrList(1); Console.WriteLine('The second index value is : ' + second_index); arrList.RemoveAt(1); var data = arrList(1); Console.WriteLine('The value at 1 index is : ' + data); Console.ReadLine(); } } }
위의 예에서는 RemoveAt을 사용하여 인덱스 1을 제거했습니다. 따라서 인덱스 2의 요소는 인덱스 1로 이동하여 간격을 대체해야합니다.
다음 프로그램의 출력은 다음과 같습니다.
산출
첫 번째 인덱스 값 : 7896
두 번째 인덱스 값 : Seven
두 번째 인덱스 값 : 여덟
1 인덱스의 값 : Seven
ArrayList를 정렬하고 반전하는 방법?
ArrayList는 정렬 및 역방향 작업을위한 두 가지 방법을 제공합니다. 조건은 하나뿐입니다. 즉, 배열 목록 내의 모든 요소는 비교기와 비교하기 위해 동일한 데이터 유형을 가져야합니다. 그렇지 않으면 런타임 오류가 발생합니다.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Collections; namespace ConsoleApp1 { class Program { static void Main(string() args) { ArrayList arrList = new ArrayList(); arrList.Add(7); arrList.Add(4); arrList.Add(5); arrList.Add(1); arrList.Add(3); Console.WriteLine('Original Array List'); foreach (var v in arrList) { Console.Write(v + ' '); } //sorting an array list Console.WriteLine(); Console.WriteLine('Sorted Array List'); arrList.Sort(); foreach (var srt in arrList) { Console.Write(srt + ' '); } //Reversing an array list Console.WriteLine(); Console.WriteLine('Reversed Array List'); arrList.Reverse(); foreach (var rvrs in arrList) { Console.Write(rvrs + ' '); } Console.ReadLine(); } } }
위의 프로그램에서는 먼저 정수 데이터 유형의 배열 목록을 만든 다음 정렬 방법을 사용하여 목록을 정렬하고 반대 방법을 사용하여 정렬 된 목록을 반전했습니다.
따라서 다음 목록의 출력은 다음과 같습니다.
산출
원래 어레이 목록
7 4 5 1 3
정렬 된 배열 목록
1 34 5 7
역 배열 목록
7 5 4 3 1
C # HashTable
C #의 System.Collections 네임 스페이스에는 Dictionary와 매우 유사한 Hashtable이 포함되어 있습니다. Hashtable은 키-값 쌍의 형태로 데이터를 저장합니다.
내부적으로 해시 코드를 해시 키에 할당하여 내부적으로 수행하고 데이터에 액세스 할 때마다 해시 코드를 해시 키와 일치시켜 데이터를 검색합니다. 테이블의 각 항목에는 키-값 쌍이 있습니다.
HashTable을 초기화하는 방법?
HashTable은 키워드 HashTable을 사용한 다음 인스턴스를 생성하여 초기화 할 수 있습니다.
Hashtable hashtbl = new Hashtable();
HashTable에 요소를 추가하는 방법?
Add () 메서드를 사용하여 HashTable에 요소를 추가 할 수 있습니다. 사용자가 키와 값으로 항목을 추가 할 수 있습니다. 키 또는 값의 데이터 유형을 설명하는 것은 중요하지 않습니다. HashTable에 요소를 추가하는 동안 키는 값이 null 일 수있는 null을 포함 할 수 없음을 기억해야합니다.
HashTable.Add (키, 값);
예:
class Program { static void Main(string() args) { Hashtable hashtbl = new Hashtable(); hashtbl.Add('Number',1); hashtbl.Add('Car', 'Ferrari'); hashtbl.Add(8, 'eight'); } }
보시다시피 키와 값으로 다른 데이터 유형을 추가했습니다.
HashTable에있는 요소에 액세스하는 방법?
인덱서를 사용하여 Hashtable에서 키 값을 검색 할 수 있습니다. 그러나 인덱서는 테이블에서 값에 액세스하고 검색하기위한 키도 필요합니다.
값을 가져 오기 위해 위 프로그램에 작은 코드 스 니펫을 추가해 보겠습니다.
class Program { static void Main(string() args) { Hashtable hashtbl = new Hashtable(); hashtbl.Add('Number',1); hashtbl.Add('Car', 'Ferrari'); hashtbl.Add(8, 'eight'); int value1 = (int)hashtbl('Number'); String value2 = (string)hashtbl('Car'); String value3 = (string)hashtbl(8); Console.WriteLine(value1); Console.WriteLine(value2); Console.WriteLine(value3); Console.ReadLine(); } }
이제 이것을 실행하면 다음 출력이 생성됩니다.
버블 정렬 알고리즘 C ++
1
페라리
여덟
위의 프로그램에서는 발생할 수있는 컴파일 오류를 제거하기 위해 주어진 데이터 유형에 대한 모든 키의 값을 캐스팅했습니다. 이는 제네릭이 아닌 컬렉션이기 때문에 Hashtable은 콘텐츠의 데이터 유형 (예 : 키 및 값)에 대한 정보를 포함하지 않기 때문입니다.
따라서 데이터 컴파일러를 직접 가져 오려고하면 데이터 유형에 대해 혼란스럽고 오류가 발생합니다.
Hashtable에서 요소를 제거하는 방법?
Hashtable에서 특정 키-값 쌍을 제거하려고한다고 가정 해 보겠습니다. 이를 위해 컬렉션에서 제공하는 Remove () 메서드를 사용해야합니다. Remove 메서드는 Hashtable에서 주어진 키-값 쌍을 영구적으로 삭제합니다.
제거 (키);
아이디어를 얻기 위해 위의 프로그램에 Remove 메서드를 추가해 보겠습니다.
class Program { static void Main(string() args) { Hashtable hashtbl = new Hashtable(); hashtbl.Add('Number',1); hashtbl.Add('Car', 'Ferrari'); hashtbl.Add(8, 'eight'); int value1 = (int)hashtbl('Number'); String value2 = (string)hashtbl('Car'); String value3 = (string)hashtbl(8); Console.WriteLine(value1); Console.WriteLine(value2); Console.WriteLine(value3); //now we remove a key hashtbl.Remove(8); //Lets try to find the remove key now String valueRemoved = (string)hashtbl(8); Console.WriteLine('The value of the given key is :'+valueRemoved); Console.ReadLine(); } }
위 프로그램의 출력은 다음과 같습니다.
1
페라리
여덟
주어진 키의 값은 다음과 같습니다.
아니요, Hashtable에서 키를 제거하면 값이 콘솔에 인쇄됩니다. 따라서 valueRemoved의 문자열 값은 null입니다.
해시 테이블에서 모든 것을 제거하려면 C #에서 Clear ()라는 또 다른 메서드를 제공합니다. Remove 메서드는 한 번에 하나의 키-값 쌍을 삭제하는 반면 Clear 메서드는 해시 맵에서 모든 것을 삭제합니다.
이를 위해 아래 프로그램을 살펴 보겠습니다.
class Program { static void Main(string() args) { Hashtable hashtbl = new Hashtable(); hashtbl.Add('Number',1); hashtbl.Add('Car', 'Ferrari'); hashtbl.Add(8, 'eight'); int value1 = (int)hashtbl('Number'); String value2 = (string)hashtbl('Car'); String value3 = (string)hashtbl(8); Console.WriteLine(value1); Console.WriteLine(value2); Console.WriteLine(value3); //now we remove a key hashtbl.Remove(8); //Lets try to find the remove key now String valueRemoved = (string)hashtbl(8); Console.WriteLine('The value of the given key is :'+valueRemoved); // clearing all data from the HashTable hashtbl.Clear(); Console.ReadLine(); } }
위의 프로그램은 해시 테이블에서 모든 요소를 제거하고 빈 상태로 렌더링합니다.
Hashtable에서 제공하는 다른 중요한 메서드는 ContainsKey () 및 ContainsValue ()입니다. 이 두 메서드 모두 키 또는 값인 하나의 인수를 허용하고 부울 값을 반환합니다. 따라서이 메소드로 전달 된 매개 변수가 해시 테이블에 있으면 true 값을 반환하고 존재하지 않으면 false를 반환합니다.
C # SortedList
이름에서 알 수 있듯이 SortedList는 오름차순으로 정렬 된 데이터를 포함합니다. 유사한 키-값 쌍을 포함하므로 Hashtable과 유사합니다. SortedList에 삽입되거나 추가 된 모든 키는 자동으로 오름차순으로 정렬됩니다.
SortedList를 초기화하는 방법?
SortedList는 SortedList 키워드를 사용하고 이에 대한 개체 인스턴스를 생성하여 초기화 할 수 있습니다.
SortedList sortedList = new SortedList();
그런 다음 SortedList 속성 및 메서드를 사용하여 작업을 수행하는 데 개체를 사용할 수 있습니다.
SortedList에 요소를 추가하는 방법?
Add () 메서드를 사용하여 SortedList에 요소를 추가 할 수 있습니다. SortedList를 사용하려면 키와 값을 추가해야합니다. 원하는 순서로 키와 값을 추가 할 수 있으며 정렬 된 목록은 추가 된 모든 요소를 오름차순으로 정렬합니다.
class Program { static void Main(string() args) { SortedList sortedList = new SortedList(); sortedList.Add(2, 'Two Pineapples'); sortedList.Add(4, 'Four Apples'); sortedList.Add(3, 'Three Lemons'); sortedList.Add(5, 'Five Mangoes'); sortedList.Add(1, 'One Tree'); Console.ReadLine(); } }
위의 프로그램에서 정수를 키로 추가 한 다음 문자열을 값으로 추가 한 것을 볼 수 있습니다. 원하는대로 원하는 순서대로 데이터 유형을 추가 할 수 있습니다. SortedList는 오름차순으로 정렬합니다.
HashTable과 마찬가지로 키는 null 일 수 없으며 모든 키는 비교를 위해 동일한 데이터 유형을 가져야합니다. 그렇지 않으면 컴파일 오류가 발생합니다.
정렬 됨 목록은 요소를 추가 할 때마다 요소를 정렬합니다. 따라서 정렬이 완료된 후 요소를 추가하더라도 다시 정렬하여 해당 인덱스에 요소를 추가합니다.
SortedList에서 요소에 액세스하는 방법?
정렬 된 목록의 값은 키를 사용하여 액세스 할 수 있습니다.
이전 예제에서 설명한 SortedList에서 값을 가져 오는 간단한 코드를 추가해 보겠습니다.
class Program { static void Main(string() args) { SortedList sortedList = new SortedList(); sortedList.Add(2, 'Two Pineapples'); sortedList.Add(4, 'Four Apples'); sortedList.Add(3, 'Three Lemons'); sortedList.Add(5, 'Five Mangoes'); sortedList.Add(1, 'One Tree'); string i = (string)sortedList(1); string j = (string)sortedList(5); string k = (string)sortedList(3); Console.WriteLine(i); Console.WriteLine(j); Console.WriteLine(k); Console.ReadLine(); } }
위 코드 스 니펫의 출력은 다음과 같습니다.
한 나무
망고 5 개
레몬 3 개
위의 코드 스 니펫에서 값의 데이터 유형으로 발생할 수있는 컴파일 오류를 제거하기 위해 주어진 데이터 유형에 대한 모든 키의 값을 캐스팅해야합니다. 이는 일부 키에 다른 데이터 유형이 포함되어 있어도 컴파일 오류가 발생하지 않도록하기 위해 수행됩니다.
지정된 키가 SortedList에 있는지 확인하는 방법은 무엇입니까?
두 가지 내장 방법이 있습니다. 포함 () 과 ContainsKey () sortedList 내에 특정 키가 있는지 확인하는 데 도움이됩니다. ConstainsValue () 컬렉션 내에 특정 값이 있는지 여부를 확인하는 데 사용되는 또 다른 방법입니다.
이러한 방법에 대해 자세히 알아보기 위해 간단한 프로그램을 살펴 보겠습니다.
class Program { static void Main(string() args) { SortedList sortedList = new SortedList(); sortedList.Add(2, 'Two Pineapples'); sortedList.Add(4, 'Four Apples'); sortedList.Add(3, 'Three Lemons'); sortedList.Add(5, 'Five Mangoes'); sortedList.Add(1, 'One Tree'); //boolean value for key 5 bool key = sortedList.ContainsKey(5); //boolean value for vlaue 'One Tree' bool val = sortedList.ContainsValue('One Tree'); //Boolean value for unavailable key bool unKey = sortedList.ContainsKey(25); //Boolean value for unavailable value bool unVal = sortedList.ContainsValue('some randome value'); Console.WriteLine('The sorted list contains 5 key :' + key); Console.WriteLine('The sorted list contains One Tree value :' + val); Console.WriteLine('The sorted list contains 25 key :' +unKey); Console.WriteLine('The sorted list contains some random value:' + unVal); Console.ReadLine(); } }
위 프로그램의 출력은 다음과 같습니다.
정렬 된 목록에는 5 개의 키가 있습니다. True
정렬 된 목록에는 One Tree 값이 포함됩니다. True
정렬 된 목록에는 25 개의 키가 있습니다. False
정렬 된 목록에는 임의의 값이 포함되어 있습니다. False
위의 프로그램에서 값이나 키가 Sorted List 내에 있으면 참 값이 반환되고 없으면 거짓 값이 반환된다는 것을 분명히 알 수 있습니다.
SortedList에서 요소를 제거하는 방법?
정렬 된 목록은 Remove () 및 RemoveAt () 메서드를 제공하여 정렬 된 목록 내에있는 모든 요소를 삭제합니다. Remove는 키 이름이있는 단일 인수를 허용하고 RemoveAt도 색인이있는 단일 인수를 허용합니다.
이 두 메서드 모두 인수를 기반으로 정렬 된 목록에있는 모든 요소를 제거합니다.
더 명확하게 이해하기 위해 간단한 코드를 살펴 보겠습니다.
class Program { static void Main(string() args) { SortedList sortedList = new SortedList(); sortedList.Add(2, 'Two Pineapples'); sortedList.Add(4, 'Four Apples'); sortedList.Add(3, 'Three Lemons'); sortedList.Add(5, 'Five Mangoes'); sortedList.Add(1, 'One Tree'); //The Remove() method accepts key as argument and removes both the key and the value sortedList.Remove(1); //Now we will check if the key is present bool rmvKey = sortedList.ContainsKey(1); Console.WriteLine('The presence if the key is: ' + rmvKey); //The RemoveAt() method acceots index as argument and remove any key and value present at that index sortedList.RemoveAt(3); Console.ReadLine(); } }
위 프로그램의 출력은 다음과 같습니다.
키가 다음과 같은 경우 존재 : False
위의 프로그램에서 먼저 Remove 메서드를 사용하여 키를 사용하는 키-값 쌍을 제거했습니다. 그러면 인수에 제공된 키와 일치하는 모든 키-값 쌍이 제거됩니다. 그런 다음 ContainsKey 메서드를 사용하여 제거 된 키가 정렬 목록에 더 이상 존재하지 않는지 확인했습니다.
다음 줄에서는 인덱스를 사용하여 요소를 제거하는 RemoveAt 메서드를 사용했습니다. 따라서 앞에서 설명한 것처럼 특정 요소가 인덱스에서 제거되면 다른 요소가 위로 이동하여 정렬 된 목록 구조를 재 배열하고 유지합니다.
결론
컬렉션은 데이터를 저장하고 작동하기 위해 C # 프로그래밍 언어로 제공되는 전용 클래스입니다. 특정 작업을 수행하는 데 사용됩니다. 즉, 주어진 데이터 세트에 대해 동적 목록, 반전, 정렬 등을 생성합니다.
이 자습서에서는 일부 측면에서 배열과 유사하지만 미리 정의 된 크기가없는 ArrayList에 대해 배웠습니다. 또한 키-값 쌍으로 데이터를 저장하는 HashTable에 대해서도 배웠습니다. 키를 사용하여 모든 값을 검색 할 수 있습니다.
HashTable과 유사한 정렬 된 목록에 대해서도 배웠지 만 그 안에있는 모든 데이터를 키에 따라 오름차순으로 자동 정렬합니다.
정렬 된 목록 내부의 데이터는 항상 오름차순입니다. 즉, 중간에서 특정 요소를 제거하거나 정렬 된 목록에 새 요소를 추가하더라도 모든 데이터가 오름차순으로 자동 정렬됩니다.
샘플 코드
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using SeleniumFrameWork.FrameworkEssentials; using SeleniumFrameWork.FrameWorkSupportModules; namespace ConsoleApp1 { class Program { static void Main(string() args) { /* Array List Code */ ArrayList arrList = new ArrayList(); arrList.Add(7); arrList.Add(4); arrList.Add(5); arrList.Add(1); arrList.Add(3); Console.WriteLine('Original Array List'); foreach (var v in arrList) { Console.Write(v + ' '); } //sorting an array list Console.WriteLine(); Console.WriteLine('Sorted Array List'); arrList.Sort(); foreach (var srt in arrList) { Console.Write(srt + ' '); } //Reversing an array list Console.WriteLine(); Console.WriteLine('Reversed Array List'); arrList.Reverse(); foreach (var rvrs in arrList) { Console.Write(rvrs + ' '); } /* HashTable Code */ Hashtable hashtbl = new Hashtable(); hashtbl.Add('Number', 1); hashtbl.Add('Car', 'Ferrari'); hashtbl.Add(8, 'eight'); int value1 = (int)hashtbl('Number'); String value2 = (string)hashtbl('Car'); String value3 = (string)hashtbl(8); Console.WriteLine(value1); Console.WriteLine(value2); Console.WriteLine(value3); //now we remove a key hashtbl.Remove(8); //Lets try to find the remove key now String valueRemoved = (string)hashtbl(8); Console.WriteLine('The value of the given key is :' + valueRemoved); // clearing all data from the HashTable hashtbl.Clear(); /* Sorted List Code */ SortedList sortedList = new SortedList(); sortedList.Add(2, 'Two Pineapples'); sortedList.Add(4, 'Four Apples'); sortedList.Add(3, 'Three Lemons'); sortedList.Add(5, 'Five Mangoes'); sortedList.Add(1, 'One Tree'); string i = (string)sortedList(1); string j = (string)sortedList(5); string k = (string)sortedList(3); Console.WriteLine(i); Console.WriteLine(j); Console.WriteLine(k); //boolean value for key 5 bool key = sortedList.ContainsKey(5); //boolean value for vlaue 'One Tree' bool val = sortedList.ContainsValue('One Tree'); //Boolean value for unavailable key bool unKey = sortedList.ContainsKey(25); //Boolean value for unavailable value bool unVal = sortedList.ContainsValue('some randome value'); Console.WriteLine('The sorted list contains 5 key :' + key); Console.WriteLine('The sorted list contains One Tree value :' + val); Console.WriteLine('The sorted list contains 25 key :' +unKey); Console.WriteLine('The sorted list contains some randome value:' + unVal); //The Remove() method accepts key as argument and removes both the key and the value sortedList.Remove(1); //Now we will check if the key is present bool rmvKey = sortedList.ContainsKey(1); Console.WriteLine('The presence if the key is: ' + rmvKey); //The RemoveAt() method acceots index as argument and remove any key and value present at that index sortedList.RemoveAt(3); Console.ReadLine(); } } }