0

I've inherited an old program with a data structure that is basically image reading (x,y, and a few properties). it works ok but i have to do a major expansion/refactor now, so i want to address some issues with my knowledge now

        public struct DataSet          
        {
            public double X;
            public double Y;
            public double A;
            public double B;
            public double C;
        }
        List<List<DataSet>> RasterSet = List<List<DataSet>>();

        //create the structure from the image file using X,Y,A,B,C

        sublist = new List<DataSet>();
        RasterSet.Add(sublist);

the question is, i've always assumed through my reading that passing a big struct around is expensive, so i've been making the struct and RasterSet list as public static. I'd really rather fix this, but i don't know how.

I'm ONLY using the RasterSet List<List<Struct>>; the whole reason for the struct is that is how the image software i'm talking to gives me it's data.

Even better would be to understand if there is a way to keep the "structure" (X,Y,A,B,C) without the struct (reference rather than value) but i have no idea how or if.

i'm lacking in experience here, but the reading on struct is very focused on the old chestnut "when to use a struct" and not a lot of practical examples (i don't need what is a struct and when to use it advice please).

ferday
  • 197
  • 4
  • 13
  • First answer to first marked duplicate is best: use `struct` if you need value semantics, `class` if you need reference semantics. Only worry about performance considerations if you have problems you can measure. As for your particular scenario, it's not clear how you're using the data structure, so we can't say what would be best. You have, effectively, an array of these things, so at least when you are passing the lists, you are still passing reference types anyway. – Peter Duniho Mar 31 '17 at 04:47
  • Peter, i've made an edit, but i'm very interested in your last statement - is the List> being passed as a reference (like a normal List, and therefore the same cost?). – ferday Mar 31 '17 at 04:51
  • Yes. `List` is a reference type, and so the value passed is always a reference, even if the `T` type is a value type. – Peter Duniho Mar 31 '17 at 05:03
  • @ferday However, keep in mind that some operations with these lists may copy values. For example, a Where(x => ) probably pass those items as value inside the anonymous function (lambda). – Guilherme Mar 31 '17 at 05:06
  • thanks both, food for thought. – ferday Mar 31 '17 at 05:13

1 Answers1

0

You can try using ref. More info on this MSDN page.

Guilherme
  • 5,143
  • 5
  • 39
  • 60