方法签名说明必需

时间:2018-09-03 10:40:11

标签: c#

我正在尝试了解一种复杂的方法(对我来说)。

这是该方法的签名:

public static List<T> GetAll<R, T>(RestClient client, RestRequest request) where R : new()

现在,我了解到它将返回一个通用类型,并且需要一个RestClient和RestRequest对象作为参数。

但是我不明白什么:

<R, T>

where R : new()

位实际上意味着什么?

有人可以详细说明吗?

1 个答案:

答案 0 :(得分:7)

这些是Generic type constraints

本质上,这个签名说:

public static List<T> GetAll<R, T>(RestClient client, RestRequest request) where R : new()

public-在大会之外可以访问

static-非实例,静态(aka类)方法

List<T>-返回System.Collections.Generic.List<T>-一个类似数组的集合,其中的项类型为T

GetAll<R, T>-GetAll是方法名称; R,T->我想象RequestTypeT,其中t是ResponseType

(RestClient client, RestRequest request)只是方法的参数

where R : new()-该方法仅对类型R有效,其中R具有公共无参数构造函数(例如,您可以在new R()处键入内容)

用途可能是:

List<string> GetAll<object, string>(RestClient client, RestRequest request);

这不是一个很好的签名,因为不清楚作者为什么需要R

相关问题