C#List<>题 [英] C# List<> question

查看:113
本文介绍了C#List<>题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我是C#语言的新手,我现在已经进入了列表。
我在下面放了几行代码,这是我的问题:如果每个需要的对象我都会做想保留,我使用List<>然后每次我必须提供一个唯一的对象名称(参考)。因此,如果在可能的情况下使列表替换数组,那么C#程序如何创建唯一的名称来创建对象,然后将它们存储在List<>中。在运行时间?在该片段中没有包含一个学生类,它有一个方法()来输入名称,____,高度等每个学生。
运行此代码后,所有参考最后的对象,因此都是相同的。使用x来索引它的本质的数组[x]在运行时变成唯一的名称。下面我可以为每个对象使用s1,s2,s3但对现实世界不太实用。有人请说明在现实世界中如何做到这一点。




Im new to the C# language and I have now advanced to lists.
I placed a few lines of code below and here is my question:     If every needed object I make I want to keep and I use a List<>  then every time I must supply a unique object name(reference).  So if Lists were made to replace arrays when possible,  how then would C# program create unique names to create objects and then store them in a List<> at run-time?


 

Not included in the snippet is the student class that has a method() to enter the name, ____,height etc of each student.
After running this code of course all reference the last objetc and thus all are the same.  An array[x] by the very nature of using x to index it becomes a unique name at run time.  Below I could use s1,s2,s3 for each object but not very practical for real world.   Some one please explain how this is done in real world, please.


 

命名空间 ConsoleApplication1
{{/ span> class 程序 {{ 静态 void Main( string [] args)
{
列表 < 学生 > stdb = new 列表 < 学生 > ;();

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<student> stdb = new List<student>();

 

学生 st = new 学生 ();

           student st = new student();

 

st.enterstudent(

   st.enterstudent(

" John米勒" ,44,1,69,190); stdb.Add(st);
st.enterstudent(
" Robert Sanchez" ,38,1,70,160); stdb.Add(ST);
st.enterstudent(
"Jim Goalten" ,71,1,65,145); stdb.Add(ST);

"John Miller", 44, 1, 69, 190);                            stdb.Add(st);
   st.enterstudent(
"Robert Sanchez", 38, 1, 70, 160);                     stdb.Add(st);
   st.enterstudent(
"Jim Goalten", 71, 1, 65, 145);                           stdb.Add(st);

 


 

}
}
}

}
}
}

推荐答案

您的问题与Windows Live Messenger客户端开发没有关系,我也不太了解您要求的内容。
但是,无论如何在列表中的C#项目中没有引用名称,你使用索引。

例如:
Console.WriteLine(stdb [0] .whatever) ;
//这将输出列表中第一个学生结构/类的whavever属性。
Your question doesen't really relate to Windows Live Messenger client development, and I don't quite understand what your asking for either.


But anyway in C# items in lists aren't referenced by names, you use an index.

e.g.:
Console.WriteLine(stdb[0].whatever);
//This would output the whavever property of the first student structure/class in the list.


这篇关于C#List&lt;&gt;题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆