From a119ae3dd375353a41ed3c60aab43ff13563843f Mon Sep 17 00:00:00 2001 From: X1nto Date: Fri, 14 Jan 2022 20:36:42 +0400 Subject: [PATCH] replaced SmartDictionaryOf with Tuple version of DictionaryOf --- Sharplin/Dictionary/Builders.cs | 42 ++++++++++++++++++--------------- Tests/Dictionary/Builders.cs | 9 ++++--- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/Sharplin/Dictionary/Builders.cs b/Sharplin/Dictionary/Builders.cs index 9d61eb1..b04ea32 100644 --- a/Sharplin/Dictionary/Builders.cs +++ b/Sharplin/Dictionary/Builders.cs @@ -30,35 +30,39 @@ /// public static class Builders { - /// A dictionary containing the provided + /// Constructs a from the provided < + /// paramref name="pairs"/> + /// + /// + /// + /// var dic = DictionaryOf( + /// PairOf("number", 1), + /// PairOf("position": 2) + /// ); + /// + /// + /// A containing the provided + public static Dictionary DictionaryOf(params KeyValuePair[] pairs) where TKey : notnull => pairs.ToDictionary(pair => pair.Key, pair => pair.Value); - /// - /// Constructs a from the provided + /// Constructs a from the provided + /// /// /// /// - /// var dic = SmartDictionaryOf<string, int>( - /// "number", 1, - /// "position": 2 + /// var dic = DictionaryOf( + /// ("number", 1), + /// ("position": 2) /// ); /// /// - /// A of keys and values extracted from - /// Use this at the cost of your sanity. - /// - public static Dictionary SmartDictionaryOf(params object[] pairs) - where TKey : notnull - { - var dic = new Dictionary(); - for (int i = 0; i < pairs.Length; i += 2) { - dic.Add((TKey) pairs[i], (TValue) pairs[i + 1]); - } - return dic; - } - + /// A containing the provided + public static Dictionary DictionaryOf(params (TKey key, TValue value)[] pairs) + where TKey : notnull => + pairs.ToDictionary(pair => pair.Item1, pair => pair.Item2); + /// /// A of the provided and /// diff --git a/Tests/Dictionary/Builders.cs b/Tests/Dictionary/Builders.cs index 6523535..893a52c 100644 --- a/Tests/Dictionary/Builders.cs +++ b/Tests/Dictionary/Builders.cs @@ -1,6 +1,5 @@ namespace Tests.Dictionary; -using System; using System.Collections.Generic; using NUnit.Framework; using Sharplin.Dictionary; @@ -14,11 +13,11 @@ public class Builders }; [Test] - public void Test_SmartDictionaryOf() + public void Test_DictionaryOf_Tuple() { - var actual = SmartDictionaryOf( - "number", 1, - "money", 50 + var actual = DictionaryOf( + ("number", 1), + ("money", 50) ); Assert.AreEqual(Expected, actual); }