有人可以提供一個例子,說明如何將不同的函數存儲在字典中,並將int作爲鍵和函數作爲值。於是我可以伊斯利調用函數如下:如何使用uint作爲鍵存儲函數(C#)
functionsDictionary[123](string);
注意,在字典中的所有功能,將只需要一個輸入是字符串。並沒有回報。
有人可以提供一個例子,說明如何將不同的函數存儲在字典中,並將int作爲鍵和函數作爲值。於是我可以伊斯利調用函數如下:如何使用uint作爲鍵存儲函數(C#)
functionsDictionary[123](string);
注意,在字典中的所有功能,將只需要一個輸入是字符串。並沒有回報。
這聽起來像你後
Dictionary<int, Action<string>>
或可能(根據你的標題)
Dictionary<uint, Action<string>>
樣品:
using System;
using System.Collections.Generic;
class Test
{
static void Main()
{
var dictionary = new Dictionary<int, Action<string>>
{
{ 5, x => Console.WriteLine("Action for 5: {0}", x) },
{ 13, x => Console.WriteLine("Unlucky for some: {0}", x) }
};
dictionary[5]("Woot");
dictionary[13]("Not really");
// You can add later easily too
dictionary.Add(10, x => Console.WriteLine("Ten {0}", x));
dictionary[15] = x => Console.WriteLine("Fifteen {0}", x);
// Method group conversions work too
dictionary.Add(0, MethodTakingString);
}
static void MethodTakingString(string x)
{
}
}
如何使用dictionary.Add()添加函數? – 2011-02-26 07:51:53
@ user621033:編輯我的答案舉一個例子。 – 2011-02-26 07:54:41
如何添加已存在的函數名稱,如http://pastebin.com/TYfs2u1x – 2011-02-26 08:00:01
Dictionary<int, Action<string>> _functions = new Dictionary<int, Action<string>>();
_functions[123]("hello");
的http://計算器。 com/questions/3813261/how-to-store-delegates-in-a-list – Luis 2011-02-26 07:46:37
有點重複,有點不對。但我忘了提及,我想使用dictionary.add方法將函數添加到字典中。 List!=詞典:P – 2011-02-26 07:53:22