Wrapper type in .NET: struct or class?

Often your class/struct will be stored or passed on internally by methods you call (you didn't show what Caller1 and Caller2 do), and in that case it will probably get boxed. Possibly more often than you expected. So unless you can prove that a struct is going to be way more efficient: don't bother and just stick with a class.

Often your class/struct will be stored or passed on internally by methods you call (you didn't show what Caller1 and Caller2 do), and in that case it will probably get boxed. Possibly more often than you expected. So unless you can prove that a struct is going to be way more efficient: don't bother and just stick with a class.

Besides, structs are often frowned upon unless they represent something like a data type, so chosing a class might prevent future discussions of a different nature. However, if performance is really a concern, and if you can guarantee that instances are only passed as a parameter with a generic type constraint, and never stored in interface-typed fields or collections, the current inlining of structs by the . NET runtime will be more efficient, as your wrapper-struct will mostly be optimized away.

But that's a lot of ifs.

Yes, since you will access wrapper via an IInterface variable, to avoid boxing, it should be a class. EDIT: If you access wrapper via a variable typed as Wrapper, and access Wrapper methods, not IInterface methods, then a struct is fine.

I don't access it via an IInterface variable, but a generic parameter constrained to derive from IInterface. See the update. – Alexey Romanov Jul 31 '09 at 18:49 That is, calling code looks like: Wrapper y = Caller2(); return Caller1(y); – Alexey Romanov Jul 31 '09 at 18:51.

I'd say class, because you're encapsulating behavior, not just data.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions