myList<int, string, string, string> my = new myList<int, string, string, string>();//mylist<mainkey,key1,key2,key3>,mainkey唯一,就算不唯一,下面的 my[mainkey].key1取值也默认取先添加的那个
my.add(100, "q", "w", "e");
my.add(101, "a", "s", "d");
my.add(102, "z", "x", "c");
int i =my.count;
string s = my[100].key1;//取到q值
foreach(xxx xx in my)
{
int i = xx.mainkey;
string n = xx.key1;
}
以上如何用c#实现?这个myList,xxx 的数据结构应该怎么写?
给你贴一个使用结构体实现的,用类实现的网上很多,你可以搜搜。
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
struct Point
{
public int x;
public string y;
public string z;
public string m;
}
static void Main(string[] args)
{
Point P = new Point();
P.x = 100;
P.y = "q";
P.z = "w";
P.m = "e";
Point P2 = new Point();
P2.x = 101;
P2.y = "a";
P2.z = "s";
P2.m = "d";
List<Point> ii=new List<Point>();
ii.Add(P);
ii.Add(P2);
foreach (Point pp in ii)
{
int i = pp.x;
string n = pp.y;
Console.WriteLine(i.ToString() + " " + n.ToString());
}
}
}
}
用类或结构体都可以实现。