   Putting .net structures in containers
  Hi there, If I add an instance of a structure to a container, for example, a dictionary object, is the structure boxed and put onto the heap?BD  
  If it''s a struct, yes. It''s something you should currently avoid.Generics in C# 2.0 should fix this(unlike Java 1.5 generics).   --AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [Blog] [RSS] [Browse the source] [IRC channel] ;  Would it be inappropriate for me to ask why one shouldn''t do it?Basically, I had a struct and I wanted to put it in asp.net session state and it occurred to me that session state was hardly going to accommodate the data internally.BD. ;  If speed isn't an issue, don't mind. The boxing is neccessary because unlike normal classes value types lose some of the properties of an Object (capital letter!) e.g. the vptr and the sync-handle. The .NET containers however only handle normal Object-instances. To satisfy them you have to put them in an Object-compatible wrapper. This proces is called boxing. During boxing. Most issues of the boxing is handled by the compiler/the runtime. A new instance of the "box" is created and all values from the original value type are copied. And there lies the problem. This allocation and copying can be bad for the performance if you do it over and over again. And it gets worse. If you want the value back from the collection you need to unbox it - that's another copy  That's why the billboard example of managed directx is so slow. For a ASP.NET website you maybe won't notice a difference, however in a game where speed counts this can be crucial.Sidenote: Managed C++ can access boxed structs without unboxing Regards,Andre (VizOne) Loker   [edited by - VizOne on February 16, 2004 3:59:55 PM] [edited by - VizOne on February 16, 2004 4:00:22 PM];quote:Original post by Barn Door  Would it be inappropriate for me to ask why one shouldn''t do it?Performance reasons. However, if you do it infrequently it doesn''t really matter. But you don''t go boxing structs in the inner loop of your renderer.quote:Basically, I had a struct and I wanted to put it in asp.net session state and it occurred to me that session state was hardly going to accommodate the data internally.That should work just fine. 