C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
C# Dictionary InitializerC# Dictionary initializer is a feature which is used to initialize dictionary elements. Dictionary is a collection of elements. It stores elements in key and value pair. Dictionary initializer uses curly braces ({}) to enclose the key and value pair. Let's see an example, in which we are initializing value for each key. C# Dictionary Initializer Example 1
using System;
using System.Collections.Generic;
namespace CSharpFeatures
{
class DictionaryInitializer
{
public static void Main(string[] args)
{
DictionaryOutput: { Key = 1 Value = Irfan }
{ Key = 2 Value = Ravi }
{ Key = 3 Value = Peter }
In this example, we are storing student data into the dictionary. We are using dictionary initializer to store student data. See, the following example. C# Dictionary Initializer Example 2
using System;
using System.Collections.Generic;
namespace CSharpFeatures
{
class Student
{
public int ID { get; set; }
public string Name { get; set; }
public string Email { get; set; }
}
class DictionaryInitializer
{
public static void Main(string[] args)
{
DictionaryOutput: Key = 1 Value = {101, Rahul Kumar, [email protected]}
Key = 2 Value = {102, Peter, [email protected]}
Key = 3 Value = {103, Irfan, [email protected]}
Next TopicC# Pattern Matching
|