csc /t:library strconcat.cs
与using System.Collections.Generic;
{我}收到错误
strconcat.cs(9,17): error CS0305: Using the generic type
'System.Collections.Generic.List<T>' requires '1' type arguments
mscorlib.dll: (Location of symbol related to previous error)
.cs代码取自here:使用公共语言运行时 我在msdn上检查了description但是到目前为止无法编译
using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.IO;
using Microsoft.SqlServer.Server;
[Serializable]
[SqlUserDefinedAggregate(Format.UserDefined, MaxByteSize=8000)]
public struct strconcat : IBinarySerialize{
private List values;
public void Init() {
this.values = new List();
}
public void Accumulate(SqlString value) {
this.values.Add(value.Value);
}
public void Merge(strconcat value) {
this.values.AddRange(value.values.ToArray());
}
public SqlString Terminate() {
return new SqlString(string.Join(", ", this.values.ToArray()));
}
public void Read(BinaryReader r) {
int itemCount = r.ReadInt32();
this.values = new List(itemCount);
for (int i = 0; i <= itemCount - 1; i++) {
this.values.Add(r.ReadString());
}
}
public void Write(BinaryWriter w) {
w.Write(this.values.Count);
foreach (string s in this.values) {
w.Write(s);
}
}
}
我正在使用c:\Windows\Microsoft.NET\Framework\v2.0.50727
以及c:\Windows\Microsoft.NET\Framework64\v2.0.50727>
运行Windows 7 x64
怎么编译?对不起,我刚开始使用c# - 我在这里搜索了其他一些问题,这些建议对我没有任何进展(
答案 0 :(得分:1)
与CS0305相对应的文章中解释错误 - 类型参数的数量不匹配。
在您的情况下,如果需要一个类型参数,则调用new List()
,其中包括new List<string>()
和相应的字段定义private List<string> values;
。
注意:如果您出于某些奇怪的原因希望非通用版本使用名为ArrayList
的相应类,但通用List<T>
更容易使用。
答案 1 :(得分:1)
问题如前所述,您尚未指定列表中存储的类型。更改此部分如下
private List<string> values;
public void Init()
{
this.values = new List<string>();
}
C#中的通用类型需要指定用于代替<T>
的类型。
答案 2 :(得分:0)
System.Collections.Generic.List需要一个类型参数,在这种情况下它似乎是SqlString,所以更改代码的以下部分如下:
private List<SqlString> values;
public void Init() {
this.values = new List<SqlString>();
}