.net - Making a List<string, string, string> in C# with three flexible elements in one datatype -
once have defined list<string, string, string> , wanted again forgot syntax , lost example code did that, surely know possible. added new element list way after have defined it: listxy.add("string1", "string2", string3");. search on internet , here brought not results needed. insist on solution "list", that's why ask question here. found workarounds below not satisfy me, since need simplicity of usual "list" - nothing else.
i not search solution tuples or own classes or static arrays, since have beginner difficulty handling complex structures or classes.
edit: found meanwhile dictionary<string, string> need 3 items in there. can't remember have used, know ready (from ms) contained in framework net 2, had 3 string elements , extend elements .add method on it. string array has static size cannot shrink or enlarge beginner code. , when define variable final count of elements cannot foreseen, that's problem.
if know syntax have forgotten , cannot find again may reply me right answer. search syntax.
"i not need solution saying me make own class. not re-invent existing data-type!" list<string,string,string> not existing data type. did in past;
// simple way create class has constructor hold 3 strings public class pairedvalues { // these simple ways of creating getter , setter in c# public string value1 { get; set; } public string value2 { get; set; } public string value3 { get; set; } // constructor sets getters , setters public pairedvalues(string value1, string value2, string value3) { value1 = value1; value2 = value2; value3 = value3; } } making use of class;
// initialize list of new class list<pairedvalues> pairedvalues = new list<pairedvalues>(); // add object list anonymously pairedvalues.add(new pairedvalues("string1","string2","string3")); pairedvalues.add(new pairedvalues("string1", "string2", "string3")); // accessing values foreach (pairedvalues pair in pairedvalues) { string value1 = pair.value1; string value2 = pair.value2; string value3 = pair.value3; } an example using in windows forms;
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 { // simple way create class has constructor hold 3 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(); // initialize list of new class list<pairedvalues> pairedvalues = new list<pairedvalues>(); pairedvalues.add(new pairedvalues("string1", "string2", "string3")); pairedvalues.add(new pairedvalues("string1", "string2", "string3")); // accessing values foreach (pairedvalues pair in pairedvalues) { string value1 = pair.value1; string value2 = pair.value2; string value3 = pair.value3; } } } }
Comments
Post a Comment