
- 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
How to create a shallow copy of SortedList Object in C#?
To create a shallow copy of SortedList 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("A", "Jacob"); list.Add("B", "Sam"); list.Add("C", "Tom"); list.Add("D", "John"); list.Add("E", "Tim"); list.Add("F", "Mark"); list.Add("G", "Gary"); list.Add("H", "Nathan"); list.Add("I", "Shaun"); list.Add("J", "David"); Console.WriteLine("SortedList elements..."); foreach(DictionaryEntry d in list){ Console.WriteLine(d.Key + " " + d.Value); } ICollection col1 = list.Values; Console.WriteLine("
Values..."); foreach(string s in col1) Console.WriteLine(s); ICollection col2 = list.Keys; Console.WriteLine("
Keys..."); foreach(string s in col2) Console.WriteLine(s); SortedList list2 = (SortedList)list.Clone(); Console.WriteLine("
Resultant SortedList...cloned from above list"); foreach(DictionaryEntry d in list){ Console.WriteLine(d.Key + " " + d.Value); } } }
Output
This will produce the following output −
SortedList elements... A Jacob B Sam C Tom D John E Tim F Mark G Gary H Nathan I Shaun J David Values... Jacob Sam Tom John Tim Mark Gary Nathan Shaun David Keys... A B C D E F G H I J Resultant SortedList...cloned from above list A Jacob B Sam C Tom D John E Tim F Mark G Gary H Nathan I Shaun J David
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("One", "IT"); list.Add("Two ", "Operations"); list.Add("Three", "Marketing"); list.Add("Four", "Purchase"); list.Add("Five", "Sales"); list.Add("Six", "Finance"); Console.WriteLine("SortedList elements..."); foreach(DictionaryEntry d in list){ Console.WriteLine(d.Key + " " + d.Value); } Console.WriteLine("
List of values...SortedList"); IList col = list.GetValueList(); foreach(string res in col) { Console.WriteLine(res); } Console.WriteLine("
SortedList is read-only? = "+list.IsReadOnly); SortedList list2 = (SortedList)list.Clone(); Console.WriteLine("
Resultant SortedList...cloned from above list"); foreach(DictionaryEntry d in list){ Console.WriteLine(d.Key + " " + d.Value); } } }
Output
This will produce the following output −
SortedList elements... Five Sales Four Purchase One IT Six Finance Three Marketing Two Operations List of values...SortedList Sales Purchase IT Finance Marketing Operations SortedList is read-only? = False Resultant SortedList...cloned from above list Five Sales Four Purchase One IT Six Finance Three Marketing Two Operations
- Related Articles
- How to create a shallow copy of Hashtable in C#?
- How to create a shallow copy of ArrayList in C#?
- How to create a shallow copy of the BitArray in C#?
- How to create a SortedList in C#?
- How to create a copy of an object in PHP?
- How to shallow copy the objects in JavaScript?
- Deep Copy and Shallow Copy in Java
- Return a shallow copy of IdentityHashMap in Java
- What is Shallow Copy and how it is different from Deep Copy in C#?
- Copy - Shallow and deep copy operations in Python
- How do you make a shallow copy of a list in Java?
- Search in a SortedList object in C#
- Create a copy of a TimeZone object in Java
- Getting the keys in a SortedList object C#
- Getting the list of keys of a SortedList object in C#

Advertisements