[英]c# accessing class methods in List<List<T>>
I have a class that I wish to embed in a jagged list. 我有一类希望嵌入锯齿状列表中的课程。 I have created the list as
List<T>[]
but have been unable to insert or extract data from the list. 我已将列表创建为
List<T>[]
但是无法从列表中插入或提取数据。
In the code below, the line 在下面的代码行中
jaggedList[2].AddSomething("this is a string");
generates an error: 产生错误:
"Error CS1061 'List' does not contain a definition for 'AddSomething' and no extension method 'AddSomething' accepting a first argument of type 'List' could be found (are you missing a using directive or an assembly reference?)
“错误CS1061'列表'不包含'AddSomething'的定义,并且找不到找到接受类型为'List'的第一个参数的扩展方法'AddSomething'(您是否缺少using指令或程序集引用?)
The line 线
jaggedList[i].Finish();
produces a similar error message. 产生类似的错误消息。
There is also a problem with the otheFunctions(ref jaggedList[i], i);
otheFunctions(ref jaggedList[i], i);
还有一个问题otheFunctions(ref jaggedList[i], i);
line and the compiler error is: 行,编译器错误为:
CS1503 Argument 1: cannot convert from 'ref System.Collections.Generic.List' to 'ref ListList.MyList'
CS1503参数1:无法从“ ref System.Collections.Generic.List”转换为“ ref ListList.MyList”
but isn't List what jaggesList[I] is? 但不是列出jaggesList [I]是什么吗? I suspect that this is my problem but don't understand what exactly it is.
我怀疑这是我的问题,但不知道到底是什么。
class Program
{
static void Main(string[] args)
{
int dynamicallyEstablishedLength = 0;
dynamicallyEstablishedLength = 4;
List<MyList>[] jaggedList = new List<MyList>[dynamicallyEstablishedLength];
for (int i = 0; i < jaggedList.Length; i++)
jaggedList[i] = new List<MyList>(); //allocate space for each element
jaggedList[2].AddSomething("this is a string");
for (int i = 0; i < jaggedList.Length; i++)
otheFunctions(ref jaggedList[i], i);
for (int i = 0; i < jaggedList.Length; i++)
jaggedList[i].Finish();
}
void otheFunctions(ref MyList list, int i)
{
list.AddSomething("this is string" + i.ToString());
}
}
public class MyList
{
private List<string> list;
public MyList()
{
list = new List<string>();
}
public void AddSomething(string line, int trace = 0)
{
//do some processing on line
//then
list.Add(line);
}
public void Finish()
{
foreach (string s in list)
Trace.WriteLine(s);
}
}
the error is clear, because the instance List<MyList>[] jaggedList = new List<MyList>...
is of type List<>
not type MyList
. 该错误很明显,因为实例
List<MyList>[] jaggedList = new List<MyList>...
的类型为List<>
而不是MyList
。 so few ways to solve this, ie, create an extension method on the type List<>
: 解决这种问题的方法很少,即在类型
List<>
上创建扩展方法:
public static class ListExtensionMethods
{
...
static public void AddSomething<T>(this List<T> list, string line, int trace = 0)
{
//do some processing on line
//then
list.Add(line);
}
static public void Finish<T>(this List<T> list)
{
...
}
...
}
or, use the type MyList
instead of List<>
: 或者,使用类型
MyList
代替List<>
:
MyList[] jaggedList = new MyList[100];
...
jaggedList[0].AddSomething(...)
the second error, is a result of the first error. 第二个错误是第一个错误的结果。 please search c# extension methods for more info.
请搜索c#扩展方法以获取更多信息。
You need to understand that in this line jaggedList[2].AddSomething("this is a string");
您需要了解这一行
jaggedList[2].AddSomething("this is a string");
, jaggedList[2] is a List and you cannot access the methods of MyList directly from it. ,jaggedList [2]是一个列表,您不能直接从其中访问MyList的方法。 You can however try something like
jaggedList[2][1].AddSomething("this is a string");
但是,您可以尝试类似
jaggedList[2][1].AddSomething("this is a string");
mad.meesh had the answer. mad.meesh得到了答案。 Changing the code to
将代码更改为
MyList[] jaggedList = new MyList[dynamicallyEstablishedLength];
for (int i = 0; i < jaggedList.Length; i++)
jaggedList[i] = new MyList();
Solved all of my issues. 解决了我所有的问题。 Thanks for all of the comments.
感谢您的所有评论。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.