Should I use the structure instead of the class to put the elements into the int dictionary

advertisements

I've a custom Entity class (see code below). The objects of this class get populated into a Dictionary collection Dictionary<string, Dictionary <uint,Entity>> dt . Someone ask me to use Struct instead of Class, since value types won't get copied into heap. Well, I think in this case Class seems better choice since Dictionary will then only contains reference to the objects of type Entity. Entity type in this case represents a row in a csv file. So for every row there will be a Entity type object. The encapsulated Dictionary within Entity type contains key,value pair representing column,value for a row in a csv file.

But I want to make sure I didn't miss anything obvious so thought better to ask.

 public class Entity
    {
        Dictionary<string, string> dtValues = new Dictionary<string,string>(); //contains values    from CSV file.
        public Dictionary<string, string> Values
        {
          get { return dtValues; }
          set { dtValues = value; }
        }

    }


Someone ask me to use Struct instead of Class, since value types won't get copied into heap.

That person is dead wrong. Simply put: the dictionary itself is on the heap, so its contents will also be on the heap.

If you're not sure whether your should use a reference type or a value type, then you should research the pros and cons of both (I listed a few here). But you should probably use a reference type.

Remember: premature optimization is the root of all evil.