
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
Copying the SortedList elements to an Array Object in C#
To copy the SortedList elements to an Array Object, the code is as follows −
Example
using System; using System.Collections; public class Demo { public static void Main(String[] args){ SortedList list = new SortedList(); list.Add("1", "AB"); list.Add("2", "CD"); list.Add("3", "EF"); list.Add("4", "GH"); list.Add("5", "IJ"); list.Add("6", "JK"); list.Add("7", "KL"); list.Add("8", "LM"); Console.WriteLine("SortedList elements..."); foreach(DictionaryEntry d in list){ Console.WriteLine("Key = "+d.Key + ", Value = " + d.Value); } Console.WriteLine("
Copied to Array object..."); DictionaryEntry[] dictArr = new DictionaryEntry[10]; list.CopyTo(dictArr, 2); for (int i = 0; i < dictArr.Length; i++) { Console.WriteLine("Key = "+ dictArr[i].Key + ", Value = " + dictArr[i].Value); } } }
Output
This will produce the following output −
SortedList elements... Key = 1, Value = AB Key = 2, Value = CD Key = 3, Value = EF Key = 4, Value = GH Key = 5, Value = IJ Key = 6, Value = JK Key = 7, Value = KL Key = 8, Value = LM Copied to Array object... Key = , Value = Key = , Value = Key = 1, Value = AB Key = 2, Value = CD Key = 3, Value = EF Key = 4, Value = GH Key = 5, Value = IJ Key = 6, Value = JK Key = 7, Value = KL Key = 8, Value = LM
Example
Let us now see another example −
using System; using System.Collections; public class Demo { public static void Main(String[] args){ SortedList list = new SortedList(); list.Add("1", "Katie"); list.Add("2", "Andy"); list.Add("3", "Mark"); list.Add("4", "Gary"); list.Add("5", "Sam"); Console.WriteLine("SortedList elements..."); foreach(DictionaryEntry d in list){ Console.WriteLine("Key = "+d.Key + ", Value = " + d.Value); } Console.WriteLine("
Copied to Array object..."); DictionaryEntry[] dictArr = new DictionaryEntry[5]; list.CopyTo(dictArr, 0); for (int i = 0; i < dictArr.Length; i++) { Console.WriteLine("Key = "+ dictArr[i].Key + ", Value = " + dictArr[i].Value); } } }
Output
This will produce the following output −
SortedList elements... Key = 1, Value = Katie Key = 2, Value = Andy Key = 3, Value = Mark Key = 4, Value = Gary Key = 5, Value = Sam Copied to Array object... Key = 1, Value = Katie Key = 2, Value = Andy Key = 3, Value = Mark Key = 4, Value = Gary Key = 5, Value = Sam
- Related Articles
- Copying the Collection elements to an array in C#
- Copying BitArray elements to an Array in C#
- Copying the Hashtable elements to an Array Instance in C#
- Copying the Queue elements to one-dimensional array in C#
- Copying the elements of ArrayList to a new array in C#
- Copying the HybridDictionary entries to an Array Instance in C#
- Setting the capacity to the actual number of elements in a SortedList object in C#?
- Getting the keys in a SortedList object C#
- Search in a SortedList object in C#
- Getting the Values in a SortedList object in C#
- Get the number of elements contained in SortedList in C#
- Remove all elements from a SortedList in C#
- Copy all elements in Java TreeSet to an Object Array
- How to create a shallow copy of SortedList Object in C#?
- Check if a SortedList object is synchronized in C#

Advertisements