문제

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).

도움이 되었습니까?

해결책

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.

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top