I have a static class "Extras" in my project that I use to put methods that don't belong in any other class, such as converters. I want to add a function called DictionaryFromArrays() that I use to create a Dictionary based on an two arrays passed as parameters: one filled with keys, and another filled with values. However, I'm new to generics, and using
public static Dictionary<T, U> DictionaryFromArrays(T[] keys, U[] values)
clearly doesn't work.
I know that this is obviously not the correct way to use generics, but how would one create a function that has that effect?
您应该为方法声明通用类型( DictionaryFromArrays<T, U>
):
public static Dictionary<T, U> DictionaryFromArrays<T, U>(T[] keys, U[] values)
You can do it like this:
public static Dictionary<T, U> DictionaryFromArrays<T,U>(T[] keys, U[] values)
{
var dictionary = new Dictionary<T, U>();
if (keys.Length == values.Length)
{
for (int i = 0; i < keys.Length; i++)
{
dictionary.Add(keys[i], values[i]);
}
}
else
{
/* throw exception */
}
return dictionary;
}
You are almost there:
public static Dictionary<T, U> DictionaryFromArrays<T,U>(T[] keys, U[] values)
{return ....;}
You need to specify types in the signature, not just in arguments/return value.
Note: this pattern is heavily used in LINQ's Enumerable
- static extension methods in static non-generic class.
The generic type was missing: <TKey, TValue>
before the round brackets of the method.
Here is a possible implementation which should work:
public static Dictionary<K, V> DictionaryFromArrays<K, V>(K[] keys, V[] values, bool skipDuplicates)
{
if (keys == null) throw new ArgumentNullException("keys");
if (values == null) throw new ArgumentNullException("values");
if(keys.Length != values.Length) throw new ArgumentException("Keys and Values must have the same length!");
if (!skipDuplicates)
return keys.Zip(values, (k, v) => new KeyValuePair<K, V>(k, v))
.ToDictionary(kv => kv.Key, kv => kv.Value);
else
{
Dictionary<K, V> dict = new Dictionary<K,V>();
for (int i = 0; i < keys.Length; i++)
{
K key = keys[i];
if (!dict.ContainsKey(key))
dict.Add(key, values[i]);
}
return dict;
}
}
You can use LINQ Select with index version.
public static Dictionary<T, U> DictionaryFromArrays<T,U>(T[] keys, U[] values)
{
return keys
.Select((k, idx) => new {key = k, value = values[idx]})
.ToDictionary(x => key, y => value);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.