Methods to create C# dictionaries, with static keys/values, in fluent notation I created these little utility methods to build fluent C# dictionaries. The value declarations shall be quick to write and easy to comprehend when somebody views the code. Usage conditions are as follows:


many keys point to the same values
group keys pointing to one value. Declare value only once for better visual comprehension.
only relatively few values (usually 10 to 20, rarely up to 100)
main application purpose: replace switch-case blocks in legacy code


The keys/values can also be added otherwise, such as from all constants or Resource content, and can be of any type useful to be declared in code.

In addition, I created an extension convenience method: simply .ToDictionary(), instead of constructor lambdas kv =&gt; kv.Key, kv =&gt; kv.Value

Is this good, or are there any problems or better ideas? What about performance when used frequently, compared to switch-case?

Usage/test:

class Program
{
    public const string RoadVehicles = "RoadVehicles";
    public const string RailVehicles = "RailVehicles";
    public const string Watercraft = "Watercraft";
    public const string Aircraft = "Aircraft";

    public static readonly IReadOnlyDictionary&lt;string, string&gt; GroupsForVehicles =
        FluentDictionaries.KeysToValue(
            RoadVehicles, "Car", "Truck", "Tractor", "Motorcycle")
        .KeysToValue(
            RailVehicles, "Locomotive", "Railcar", "Powercar", "Handcar")
        .KeysToValue(
            Watercraft, "Ship", "Sailboat", "Rowboat", "Submarine")
        .KeysToValue(
            Aircraft, "Jetplane", "Propellerplane", "Helicopter", "Glider", "Balloon")
        .ToDictionary();


    static void Main(string[] args)
    {
        foreach (var key in GroupsForVehicles.Keys.OrderBy(key =&gt; key))
        {
            Console.WriteLine(key + ": " + GroupsForVehicles[key]);
        }
        Console.ReadLine();
    }
}


The fluent methods:

public static class FluentDictionaries
{
    public static IEnumerable&lt;KeyValuePair&lt;TKey, TValue&gt;&gt; KeysToValue&lt;TKey, TValue&gt;(TValue value, params TKey[] keys)
    {
        return keys.Select(key =&gt;
            new KeyValuePair&lt;TKey, TValue&gt;(key, value));
    }

    public static IEnumerable&lt;KeyValuePair&lt;TKey, TValue&gt;&gt; KeysToValue&lt;TKey, TValue&gt;(
        this IEnumerable&lt;KeyValuePair&lt;TKey, TValue&gt;&gt; previous, TValue value, params TKey[] keys)
    {
        return previous.Concat(keys.Select(key =&gt; 
            new KeyValuePair&lt;TKey, TValue&gt;(key, value)));
    }

    public static Dictionary&lt;TKey, TValue&gt; ToDictionary&lt;TKey, TValue&gt;(this IEnumerable&lt;KeyValuePair&lt;TKey, TValue&gt;&gt; keyValuePairs)
    {
        return keyValuePairs.ToDictionary(kv =&gt; kv.Key, kv =&gt; kv.Value);
    }
}


Test output of the program above:

Balloon: Aircraft

Car: RoadVehicles

Glider: Aircraft

Handcar: RailVehicles

Helicopter: Aircraft

Jetplane: Aircraft

Locomotive: RailVehicles

Motorcycle: RoadVehicles

Powercar: RailVehicles

Propellerplane: Aircraft

Railcar: RailVehicles

Rowboat: Watercraft

Sailboat: Watercraft

Ship: Watercraft

Submarine: Watercraft

Tractor: RoadVehicles

Truck: RoadVehicles
This sounds like a good idea but I don't think it is, at least not with the current method names that I agree with @denis are very confusing.

The name KeysToValue should actually be AddValueWithKeys because this is the order of parameters and it better suggests that the first parameter is a value and not a key. KeysToValue sounds like a query that gets something or converts keys to values etc.



I also find the extension should extend the dictionary and not some arbitrary IEnumerable&lt;KeyValuePair&lt;TKey, TValue&gt;&gt; because if it will throw a duplicate key exception, it will be hard to find where it happened and it's of more use if extending a dictionary. If it however extends the IEnumerable then the name should be Concat.

The improved version could look like this:

public static Dictionary&lt;TKey, TValue&gt; AddValueWithKeys&lt;TKey, TValue&gt;(
    this Dictionary&lt;TKey, TValue&gt; dictionary, 
    TValue value, 
    params TKey[] keys)
{
    foreach (var key in keys) dictionary.Add(key, value);
    return dictionary;
}


and is much easier to use as it now requries only a single extension

public static readonly IReadOnlyDictionary&lt;string, string&gt; GroupsForVehicles =
    new Dictionary&lt;string, string&gt;()
        .AddValueWithKeys(RoadVehicles, "Car", "Truck", "Tractor", "Motorcycle");