如何搜索特定的结构值? 也许更好的方法?

我正在尝试找到我之前创建的具有特定值的结构。 一旦找到它,我想在该结构上设置变量。 我不知道该怎么做。 有更好的方法吗? 也许上课? 结构应该工作吗?

例如,我的结构:

public struct MyTest { public string device; public string status; public string revision; public string number; public string ledmo; } 

我的测试代码:

 MyTest thisTest=new MyTest(); thisTest.device=blah; thisTest.number=blah2; MyTest thisTest2=new MyTest(); thisTest2.device=blah5; thisTest2.number=blah6; //Another Part in my code. //Need to find the MyTest Structure that 'device' variable = the string 'blah' var Foundit=MyTest.find(device==blah); Foundit.revision=blah9999; 

我会使用一个类,因为Mutable结构是邪恶的

基本上,因为每个struct被复制,即使你找到了正确的结构,你也只会改变一个副本。 让我们说MyTest.find找到thisTest2会发生什么

 var Foundit = MyTest.Find(device==blah); // The line above has made a copy of thisTest2, that copy is in FoundIt Foundit.revision = "blah9999"; // You've changed revision in the copy of thisTest2, // therefore the contents of thisTest2 remain unchanged 

要使用类来执行此操作,您需要在列表或其他数据结构中保留您创建的类的每个实例,因此您知道可以查找它。

如果你这样做,你还需要在完成每个对象时告诉列表,否则它们将永远存在并且永远不会被垃圾收集。

在进一步讨论之前,您确定这是解决此问题的最佳方法吗?

无论如何,假设您的类是MyData ,您可以在此Create上放置一个静态工厂方法,它将每个新的MyData对象放入一个列表中。

 public class MyData { private static List allMyDatas = new List(); public static IEnumerable AllInstances { get {return allMyDatas;} } public string Device {get; set;} public string Status {get; set;} public string Revision {get; set;} public string Number {get; set;} public string Ledmo {get; set;} private MyData() // Private ctor ensures only a member { // function can create a new MyData } public static MyData Create() { var newData = new MyData(); allMyDatas.Add(newData); return newData; } public static void Delete(MyData itemToRemove) { allMyDatas.Remove(itemToRemove); } } 

无论你在哪里使用MyData,你都需要在完成它时Delete它。

你的代码变成了

 var thisTest = MyData.Create(); thisTest.Device = "blah"; thisTest.Number = "blah2"; var thisTest2 = MyData.Create(); thisTest2.Device = "blah5"; thisTest2.Number = "blah6"; //Another Part in my code. //Need to find the MyData Structure that 'device' variable = the string 'blah' var Foundit = MyData.AllInstances.FirstOrDefault(md => md.Device == "blah"); if(Foundit != null) Foundit.Revision = "blah9999"; 

现在更改FoundIt也会更改此thisTest

PS:重要的是MyData之外的任何内容都不能new一个MyData实例。 如果可以的话,那么在AllInstances中就找不到MyData的实例。 声明构造函数私有意味着如果MyData外部的代码尝试类似var someData = new MyData则会生成编译器错误

为了能够找到先前创建的对象的实例,需要将这些实例保存在某处。
一种解决方案是将它们放入列表中,然后搜索该列表:

 var list = new List(); MyTest thisTest=new MyTest(); thisTest.device=blah; thisTest.number=blah2; list.Add(thisTest); MyTest thisTest2=new MyTest(); thisTest2.device=blah5; thisTest2.number=blah6; list.Add(thisTest2); 

现在您可以使用LINQ进行搜索:

 var foundItems = list.Where(x => x.device == "blah"); foreach(var foundItem in foundItems) { foundItem.revision = "blah9999"; } 

请注意:
这仅适用于Binary Worrier在其评论中指出的使用类而不是结构的情况。

在这种情况下,由于动态字符串大小以及存在如此多字符串的事实,类会更好地工作。

使用测试代码,您应该在该类的某处存储List ,并将thisTestthisTest2添加到列表中。 您可以稍后使用FindAll或类似方法检索特定值(或特定device所有值)。

 List list = new List(); //add MyTests here... var foundIt = list.FindAll(x => x.device == "blah"); 

您可以使用列表和Linq。

 var test = new List(); //Add some items var foundIt = test.SingleOrDefault(test => test.device == "abc");//Maximum one if(foundIt != null)//Use a class for MyTest. foundIt.device = "123"