相关疑难解决方法(0)

是否可以创建构造函数扩展方法?怎么样?

是否可以添加构造函数扩展方法?我想添加一个List <T>构造函数来接收给定部分填充的缓冲区中的特定字节数(没有复制相关字节的开销等等):

...
public static List<T>(this List<T> l, T[] a, int n)
{
    for (int i = 0; i < n; i++)
       l.Add(a[i]);
}
...
Run Code Online (Sandbox Code Playgroud)

所以用法是:

List<byte> some_list = new List<byte>(my_byte_array,number_of_bytes);
Run Code Online (Sandbox Code Playgroud)

我已经添加了AddRange扩展方法:

public static void AddRange<T>(this List<T> l, T[] a, int n)
{
   for (int i = 0; i < n; i++)
       l.Add(a[i]);
}
Run Code Online (Sandbox Code Playgroud)

我也想把它作为构造函数.可能吗 ?如果是 - 怎么样?

c# extension-methods constructor

74
推荐指数
4
解决办法
3万
查看次数

标签 统计

c# ×1

constructor ×1

extension-methods ×1