

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C# Program to access first element in a Dictionary
The following is our Dictionary with some elements −
Dictionary<int, string> d = new Dictionary<int, string>() { {1,"Electronics"}, {2, "Clothing"}, {3,"Toys"}, {4,"Footwear"}, {5, "Accessories"} };
Now to display the first element, set the key like this.
d[1];
The above displays the first element.
Example
using System; using System.Collections.Generic; public class Program { public static void Main() { Dictionary<int, string> d = new Dictionary<int, string>() { {1,"Electronics"}, {2, "Clothing"}, {3,"Toys"}, {4,"Footwear"}, {5, "Accessories"} }; foreach (KeyValuePair<int, string> ele in d) { Console.WriteLine("Key = {0}, Value = {1}", ele.Key, ele.Value); } Console.WriteLine("First element: "+d[1]); } }
Output
Key = 1, Value = Electronics Key = 2, Value = Clothing Key = 3, Value = Toys Key = 4, Value = Footwear Key = 5, Value = Accessories First element: Electronics
- Related Questions & Answers
- How to optimize Python dictionary access code?
- How to access Python dictionary in simple way?
- Search Element in a Dictionary using Javascript
- How to access nested Python dictionary items via a list of keys?
- First element and last element in a JavaScript array?
- What is the basic syntax to access Python Dictionary Elements?
- C# Program to display the first element from an array
- C# Program to find a key in Dictionary
- Python - How to access the last element in a Pandas series?
- Double the first element and move zero to end in C++ Program
- How to access index of an element in jQuery?
- How to access element with nth index in jQuery?
- How to access an array element in C language?
- Java Program to access character of a string
- Java Program to Access elements from a LinkedList
Advertisements