gpt4 book ai didi

c# - 如何在字典中制作通用 Func

转载 作者:行者123 更新时间:2023-11-30 19:48:16 27 4
gpt4 key购买 nike

是否可以制作

public class MyClass : Dictionary<string, Func<string>>

作为

public class MyClass : Dictionary<string, Func<T>>

其实我有

public class MyClass : Dictionary<string, Func<string>>
{
public void MyFunction(string key)
{
if (this.ContainsKey(key))
this[key]();
else
this["no match"]();
}
}

我想将值设置为通用值。可能吗?

谢谢。

最佳答案

必须在某处指定类型,因此您还必须使您的类通用:

public class Myclass<T> : Dictionary<string, Func<T>> {

public T MyFunction(string key) {
if (this.ContainsKey(key)) {
return this[key]();
} else {
return this["no match"]();
}
}

}

关于c# - 如何在字典中制作通用 Func<T>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5700069/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com