使用具有 return 值 C# 的方法创建列表
Create a list with methods with a return value C#
我正在制作一个小程序,想要分析语料库中德语名词的性别。为此,我创建了一些将字符串作为 return 值的方法。例如,我想检查语料库中名词前面是否有定冠,是否包含“der”、“die”或“das”。深化它 return 的不同价值观。如下所示:
private string definitearticle()
{
// code, where gender is the string which we return - if we can't determine
// the gender it returns "Cannot determine"
return gender;
}
private string indefinitearticle()
{
// code, where gender is the string which we return - if we can't determine
// the gender it returns "Cannot determine"
return gender;
}
我想一个一个地循环每个方法,直到我不再像下面的 return 值那样得到“无法确定”以及指向方法的指针列表(如 Storing a list of methods in C#):
var methods = new List<(System.Action check, string caption)>()
{
(definitearticle, "Definite article"),
(indefinitearticle, "Indefinite article"),
};
foreach (var method in methods)
{
gender = method.check.Invoke();
if (gender != "Cannot determine")
{
// store the outcome that was returned to a list
break;
}
}
问题是我不知道如何将 List
与具有 return value
(在本例中为 string
)的 methods
一起使用。有人可以帮我吗?
如果您需要将方法存储为具有 return 值的类型,您应该使用 Func<TResult>
而不是 Action
。它的工作方式与操作相同,只是您可以额外输入 return 类型作为最后一个通用类型。因此,例如,如果你有一个方法,它有两个参数,类型为 int
和 bool
,return 类型为 string
,你会说
var myReturnMethod = new Func<int, bool, string>((intVal, boolVal) => intVal.ToString() + boolVal.ToString());
对于您的情况,只需将您的方法变量初始化替换为以下代码:
var methods = new List<(Func<string> check, string caption)>
{
(definitearticle, "Definite article"),
(indefinitearticle, "Indefinite article"),
};
编辑:
根据 OP 的要求,如果您只希望它包含 collection 个方法,不带标题,那么您可以通过以下方式初始化方法变量:
var methods = new List<Func<string>>
{
definitearticle,
indefinitearticle,
};
有关函数的更多信息,请访问 the official microsoft docs。
我正在制作一个小程序,想要分析语料库中德语名词的性别。为此,我创建了一些将字符串作为 return 值的方法。例如,我想检查语料库中名词前面是否有定冠,是否包含“der”、“die”或“das”。深化它 return 的不同价值观。如下所示:
private string definitearticle()
{
// code, where gender is the string which we return - if we can't determine
// the gender it returns "Cannot determine"
return gender;
}
private string indefinitearticle()
{
// code, where gender is the string which we return - if we can't determine
// the gender it returns "Cannot determine"
return gender;
}
我想一个一个地循环每个方法,直到我不再像下面的 return 值那样得到“无法确定”以及指向方法的指针列表(如 Storing a list of methods in C#):
var methods = new List<(System.Action check, string caption)>()
{
(definitearticle, "Definite article"),
(indefinitearticle, "Indefinite article"),
};
foreach (var method in methods)
{
gender = method.check.Invoke();
if (gender != "Cannot determine")
{
// store the outcome that was returned to a list
break;
}
}
问题是我不知道如何将 List
与具有 return value
(在本例中为 string
)的 methods
一起使用。有人可以帮我吗?
如果您需要将方法存储为具有 return 值的类型,您应该使用 Func<TResult>
而不是 Action
。它的工作方式与操作相同,只是您可以额外输入 return 类型作为最后一个通用类型。因此,例如,如果你有一个方法,它有两个参数,类型为 int
和 bool
,return 类型为 string
,你会说
var myReturnMethod = new Func<int, bool, string>((intVal, boolVal) => intVal.ToString() + boolVal.ToString());
对于您的情况,只需将您的方法变量初始化替换为以下代码:
var methods = new List<(Func<string> check, string caption)>
{
(definitearticle, "Definite article"),
(indefinitearticle, "Indefinite article"),
};
编辑:
根据 OP 的要求,如果您只希望它包含 collection 个方法,不带标题,那么您可以通过以下方式初始化方法变量:
var methods = new List<Func<string>>
{
definitearticle,
indefinitearticle,
};
有关函数的更多信息,请访问 the official microsoft docs。