
- 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
Getting the Values in a SortedList object in C#
To get the values in a 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(1, "One"); list.Add(2, "Two"); list.Add(3, "Three"); list.Add(4, "Four"); list.Add(5, "Five"); ICollection col1 = list.Values; Console.WriteLine("Values..."); foreach(string s in col1) Console.WriteLine(s); ICollection col2 = list.Keys; Console.WriteLine("Keys..."); foreach(int s in col2) Console.WriteLine(s); } }
Output
This will produce the following output −
Values... One Two Three Four Five Keys... 1 2 3 4 5
Example
Let us see another 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"); 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); } }
Output
This will produce the following output −
Values... Jacob Sam Tom John Tim Mark Gary Nathan Shaun David Keys... A B C D E F G H I J
- Related Articles
- Getting the list of Values of a SortedList object in C#
- Getting the keys in a SortedList object C#
- Getting the list of keys of a SortedList object in C#
- Getting index of the specified value in a SortedList object in C#
- Getting the index of the specified key in a SortedList object in C#
- Getting the key at the specified index of a SortedList object in C#
- Getting the value at the specified index of a SortedList object in C#
- Search in a SortedList object in C#
- Check if a SortedList object is synchronized in C#
- Creating a synchronized wrapper for a SortedList object in C#
- Check whether a SortedList object contains a specific key in C#?
- Check if a SortedList object contains a specific value in C#
- Check if a SortedList object has a fixed size in C#
- What is the Values property of SortedList class in C#?
- Copying the SortedList elements to an Array Object in C#

Advertisements