Add Extension Method to Dictionary - CSharp Custom Type

CSharp examples for Custom Type:Extension Methods

Description

Add Extension Method to Dictionary

Demo Code

using System.Collections.Generic;
using System.ComponentModel;
static class PersonDictionaryExtensions
{
    public static void Add(this Dictionary<string, Person> dictionary, Person person)
    {//from   www .j  ava  2s  .  c  om
        dictionary.Add(person.Name, person);
    }
}
class SpecializedAddExtensionMethod
{
    static void Main()
    {
        Dictionary<string, Person> dictionary = new Dictionary<string, Person>
      {
         { new Person { Name = "Jon" } },
         { new Person { Name = "Holly" } }
      };
    }
}

internal class Person
{
    public string Name { get; set; }
}

Related Tutorials