“我不需要一个解决方案,说我要创建自己的课程。我绝对不会重新发明已经存在的数据类型!” List<string,string,string>
不是已经存在的数据类型。您过去可能做过类似的事情;
// Another simple way would be to create a class which has a constructor to hold the three strings
public class PairedValues
{
// These are just simple ways of creating a getter and setter in c#
public string value1 { get; set; }
public string value2 { get; set; }
public string value3 { get; set; }
// A constructor which sets all your getters and setters
public PairedValues(string Value1, string Value2, string Value3)
{
value1 = Value1;
value2 = Value2;
value3 = Value3;
}
}
利用类;
// Simply initialize a list of your new class
List<PairedValues> pairedValues = new List<PairedValues>();
// add you object to the list anonymously
pairedValues.Add(new PairedValues("string1","string2","string3"));
pairedValues.Add(new PairedValues("string1", "string2", "string3"));
// Accessing the values
foreach (PairedValues pair in pairedValues)
{
string value1 = pair.value1;
string value2 = pair.value2;
string value3 = pair.value3;
}
在 Windows 窗体中使用它的示例;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
// Another simple way would be to create a class which has a constructor to hold the three strings
public class PairedValues
{
public string value1 { get; set; }
public string value2 { get; set; }
public string value3 { get; set; }
public PairedValues(string Value1, string Value2, string Value3)
{
value1 = Value1;
value2 = Value2;
value3 = Value3;
}
}
public Form1()
{
InitializeComponent();
// Simply initialize a list of your new class
List<PairedValues> pairedValues = new List<PairedValues>();
pairedValues.Add(new PairedValues("string1", "string2", "string3"));
pairedValues.Add(new PairedValues("string1", "string2", "string3"));
// Accessing the values
foreach (PairedValues pair in pairedValues)
{
string value1 = pair.value1;
string value2 = pair.value2;
string value3 = pair.value3;
}
}
}
}