如何创建列表对象 [英] How to create a list object
本文介绍了如何创建列表对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建一个列表对象.在该列表对象中,我想添加两个值,即名称,描述.名称将相同,但描述将是多个.因此必须创建一个字符串数组进行描述.我想要输入描述的数量.
问候,\
Basha
Hi,
i want to create a list object. in that list object i want add two values i.e. name, description. the name will be same but the description will be multiple. so have to create a string array for description. and i want the count of the description entered.
Regards,\
Basha
推荐答案
最好是在该列表中创建所需对象的类. 然后将您的列表定义为:
Best would be to create a class of the objects you want in that list
and then define your List as:
List<yourclass> Listname = new List<yourclass>();
using System;
using System.Collections.Generic;
using System.Linq;
namespace PrintPrice
{
class Price
{
static void Main()
{
List<int> list = new List<int>();
list.Add(1);
list.Add(2);
// Count with the Count property.
int c = list.Count;
Console.WriteLine(c);
// Count with the extension method. This is different!
c = list.Count(); // BE CAREFUL!
Console.WriteLine(c);
Console.ReadKey();
}
}
}
public class MyObject
{
public string Name { get; set; }
public string Description { get; set; }
public MyObject():this("", "")
{
}
public MyObject(string name, string desc)
{
this.Name = name;
this.Description = desc;
}
}
public class MyObjectCollection() : List<MyObject>
{
}
用法:
Usage:
MyObjectCollection objects = new MyO bjectCollection();
objects.Add(new MyObject("John", "Outlaw Programmer"));
// or
objects.Add(new MyObject() { Name="John", Description="Outlawprogrammer" } );
// or
MyObject obj = new MyObject("John", "Outlaw Programmer");
objects.Add(obj);
// or
MyObject obj = new MyObject(){Name="John", Description="Outlaw Programmer"};
objects.Add(obj);
这不是火箭手术...
It ain''t rocket surgery...
这篇关于如何创建列表对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文