更新 IEnumerable<> 中对象的属性

发布于 2024-08-03 23:15:03 字数 652 浏览 13 评论 0 原文

我正在开发一些应该用于特殊类型实验的软件。

实验使用:

1)“芯片”(基本上是已知尺寸的 XY 网格)。 2) 每个芯片都包含“电极”,通过芯片上的 X 和 Y 坐标以及唯一的 ID 来识别。每个电极还可以容纳或不容纳样品,由一个简单的布尔值表示(它是一个生物传感芯片)。

我有用 C# 表示该硬件的对象。

我现在需要在实验中使用硬件;

1)我有一个“实验”,它公开了一个持有“ExperimentStep”对象的IEnumerable。 2)“实验步骤”包含名称以及涉及的“电极”的有限列表。

一些实验步骤可以同时运行,并且可以改变电极的“HasSample”属性。因此,当我执行“ExperimentStep”时,最好随时了解初始“HasSample”属性是什么。

这就是我的问题所在;

如果我只是将“Electrode”对象传递给我的“ExpermentStep”,它们可能会通过 Value 传递...是否可以创建一个 IEnumerable 来保存对唯一电极的引用,以便每次我想运行“ExperimentStep”时该步骤中使用的“电极”列表是否包含“HasSample”的最新值?我应该为此使用指针吗?根据我对 C++ 的有限知识,我预计这在该语言中是微不足道的(因为您大部分时间都使用指针)。但在 C# 中我真的一无所知(而且我的经验也不够)。

I am working on some software that should be used for a special type of experiment.

The experiments are performed using:

1) A "Chip" (basically an XY grid of known dimensions).
2) Each Chip contains "Electrodes", identified by their X and Y coordinate on the chip and by a unique ID. Each electrode can also hold or not hold a sample, indicated by a simple bool (it's a biosensing chip).

I have objects that represent this hardware in C#.

I now need to use the hardware in an experiment;

1) I have an "Experiment" which exposes an IEnumerable holding "ExperimentStep" objects.
2) An "ExperimentStep" holds a name, and a limited list of "Electrodes" involved among other things.

Some experiment steps could run concurrently and could change the "HasSample" property of an electrode. Therefore, when I perform an "ExperimentStep" it might be good to know what the initial "HasSample" property is at any one time.

This is where my problem lies;

If I just pass "Electrode" objects to my "ExpermentStep" they will probably be passed by Value... Is it possible to create an IEnumerable that holds references to the unique electrodes so that each time I want to run an "ExperimentStep" the list of "Electrodes" used in that step holds the most recent value for "HasSample"? Should I use pointers for this?? From my limited knowledge of C++ I would expect that this would be trivial in that language (since you work with pointers most of the time). But in C# I really have no clue (and I am not experienced enough).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏天碎花小短裙 2024-08-10 23:15:03

c# 中,类是引用类型。这意味着,如果您创建一个类的实例列表,然后还将这些实例添加到另一个列表中,那么它的项目是相同的。每个列表都会包含一个参考。因此,您可以使用 IEnumberable 来增加项目。

In c# a class is reference type. This means that if you create list of instances of a class and then also add the instances to another list then its the same items. Each list will hold a reference. So to that effect yes you can up the items using an IEnumberable.

烟若柳尘 2024-08-10 23:15:03

我怀疑您不理解引用类型和值类型之间的区别,以及按值传递在 C# 中的真正含义。

假设 Electrode 是一个类,您可以修改它的实例的属性,并且这些更改将通过对同一对象的任何引用可见。

强烈建议您在尝试开发大量生产代码之前确保对 .NET 类型系统有深入的了解。不了解正在发生的事情的后果可能是灾难性的。

我关于这些主题的几篇文章:

...但我建议你读一本很好的 C# 入门书以及。

I suspect you don't understand the difference between reference types and value types, and what pass by value really means in C#.

Assuming Electrode is a class, you can modify the properties of an instance of it and those changes will be visible via any reference to the same object.

I strongly recommend you make sure you have a firm understanding of the .NET type system before trying to develop a lot of production code. The consequences of not understanding what's going on can be disastrous.

A couple of my articles on these topics:

... but I suggest you get a good introductory C# book as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文