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

标签 c# class data-structures struct

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

例如,我的结构:

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 structs are evil

基本上,因为每个 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的类上放置一个静态factory方法,它将把每个新的MyData将对象放入列表中。

public class MyData
{
    private static List<MyData> allMyDatas = new List<MyData>();
    public static IEnumerable<MyData> 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

附言:MyData 之外的任何内容都不能新建 MyData 的实例,这一点很重要。如果可以,那么就会有一个您在 AllInstances 中找不到的 MyData 实例。将构造函数声明为私有(private)意味着如果 MyData 外部的代码尝试类似 var someData = new MyData

的操作,则会生成编译器错误

关于c# - 如何搜索特定的结构值?也许更好的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8847612/

相关文章:

c# - DataTemplate 无法解析 DataType 前缀数据

c# - 如何对密码进行加密? (csharp/dotnet - azure sql 数据库)

html - 使用类样式作为内联样式?

c - 如何在C中构造 "extend"?

java - 查找最大并发进程数的时间间隔

c# - 在 Autofac 中注册异步工厂

python - 在 python 应用程序中存储类实例的正确方法

php - 一个类可以在php中是静态的吗

java - 在算法的复杂性中取什么作为n

c# - "Request format is invalid"