C# 如何在数组中存储对象列表?
我正在寻找一种在数组中存储对象列表的方法 前C# 如何在数组中存储对象列表?,c#,arrays,object,C#,Arrays,Object,我正在寻找一种在数组中存储对象列表的方法 前 int[]数组={obj1,obj2,obj3}; 然后能够访问数组中对象的信息,如: Console.WriteLine(数组[0].x); 我一直在网上查找并找到了一种方法,其中包括一个列表,但我不知道如何使用它或它是如何工作的。请查看IList界面: IList数组=新列表() { obj1、obj2、obj3 } 您可以通过数组访问元素。ElementAt(0).x查看IList界面: IList数组=新列表() { obj1、obj2
int[]数组={obj1,obj2,obj3};
然后能够访问数组中对象的信息,如:
Console.WriteLine(数组[0].x);
我一直在网上查找并找到了一种方法,其中包括一个列表,但我不知道如何使用它或它是如何工作的。请查看
IList
界面:
IList数组=新列表()
{
obj1、obj2、obj3
}
您可以通过
数组访问元素。ElementAt(0).x
查看IList
界面:
IList数组=新列表()
{
obj1、obj2、obj3
}
您可以通过
数组访问元素。ElementAt(0).x
您有不同的可能性来实现这一点:
public static void Main()
{
ClassA[] array = { new ClassA(), new ClassA(), new ClassA()};
for(var i = 0; i< array.Length; i++)
{
// https://docs.microsoft.com/de-de/dotnet/csharp/programming-guide/arrays/
Console.WriteLine(array[i].Value);
}
Console.WriteLine("");
foreach(var item in array)
{
// https://docs.microsoft.com/de-de/dotnet/csharp/programming-guide/arrays/using-foreach-with-arrays
Console.WriteLine(item.Value);
}
Console.WriteLine("");
var enumerable = array.AsEnumerable();
for(var i = 0; i< array.Length; i++)
{
// https://docs.microsoft.com/de-de/dotnet/api/system.linq.enumerable.elementat?view=net-5.0
Console.WriteLine(enumerable.ElementAtOrDefault(i)?.Value);
}
Console.WriteLine("");
// All these things also apply for the int[] array like in your question
int[] array2 = { 1, 2, 3};
for(var i = 0; i< array.Length; i++)
{
Console.WriteLine(array2[i]);
}
// Or use a List<T> instead
var list = new List<ClassA>();
list.Add(new ClassA());
list.Add(new ClassA());
list.Add(new ClassA());
for(var i = 0; i< array.Length; i++)
{
Console.WriteLine(enumerable.ElementAtOrDefault(i)?.Value);
}
}
class ClassA
{
public string Value { get; set; } = $"{Guid.NewGuid()}";
}
publicstaticvoidmain()
{
ClassA[]数组={new ClassA(),new ClassA(),new ClassA()};
for(var i=0;i
关于的工作示例您有不同的可能性来实现这一点:
public static void Main()
{
ClassA[] array = { new ClassA(), new ClassA(), new ClassA()};
for(var i = 0; i< array.Length; i++)
{
// https://docs.microsoft.com/de-de/dotnet/csharp/programming-guide/arrays/
Console.WriteLine(array[i].Value);
}
Console.WriteLine("");
foreach(var item in array)
{
// https://docs.microsoft.com/de-de/dotnet/csharp/programming-guide/arrays/using-foreach-with-arrays
Console.WriteLine(item.Value);
}
Console.WriteLine("");
var enumerable = array.AsEnumerable();
for(var i = 0; i< array.Length; i++)
{
// https://docs.microsoft.com/de-de/dotnet/api/system.linq.enumerable.elementat?view=net-5.0
Console.WriteLine(enumerable.ElementAtOrDefault(i)?.Value);
}
Console.WriteLine("");
// All these things also apply for the int[] array like in your question
int[] array2 = { 1, 2, 3};
for(var i = 0; i< array.Length; i++)
{
Console.WriteLine(array2[i]);
}
// Or use a List<T> instead
var list = new List<ClassA>();
list.Add(new ClassA());
list.Add(new ClassA());
list.Add(new ClassA());
for(var i = 0; i< array.Length; i++)
{
Console.WriteLine(enumerable.ElementAtOrDefault(i)?.Value);
}
}
class ClassA
{
public string Value { get; set; } = $"{Guid.NewGuid()}";
}
publicstaticvoidmain()
{
ClassA[]数组={new ClassA(),new ClassA(),new ClassA()};
for(var i=0;i
关于Wow的工作示例!非常感谢你。这太快了。我想把它标为答案,但你回答得太快了,我还不能回答。:)它应该是List array=new List(),假设myObject.x有效。您提到了IList,那么不要在代码中使用它。由于引入了
IReadOnlyList
,因此通常应优先避免加上IList
。array.ElementAt(0.x
也不是一个好建议<代码>数组[0]。x更简单、更快。虽然名称的选择不是最优的(因为数组实际上不是数组)。哇!非常感谢你。这太快了。我想把它标为答案,但你回答得太快了,我还不能回答。:)它应该是List array=new List(),假设myObject.x有效。您提到了IList,那么不要在代码中使用它。由于引入了IReadOnlyList
,因此通常应优先避免加上IList
。array.ElementAt(0.x
也不是一个好建议<代码>数组[0]。x更简单、更快。虽然名称的选择不是最优的(因为数组实际上不是数组)代码>井,MyObject[]数组=new[]{obj1,obj2,obj3}代码>