Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this implemented as a struct?

Tags:

c#

linq

struct

In System.Data.Linq, EntitySet<T> uses a couple of ItemList<T> structs which look like this:

 internal struct ItemList<T> where T : class   {     private T[] items;     private int count;     ...(methods)...   } 

(Took me longer than it should to discover this - couldn't understand why the entities field in EntitySet<T> was not throwing null reference exceptions!)

My question is what are the benefits of implementing this as a struct over a class?

like image 280
Simon Hewitt Avatar asked Jun 01 '11 06:06

Simon Hewitt


People also ask

How is struct implemented?

An array is contiguous, but structs may contain padding. They aren't implemented "in the language itself". A C implementation uses a structure definition as a guide to access a piece of memory (in an implementation defined manner). After translation, any notion of human readable "structures" is gone.

Why would you use a struct?

Structs are best suited for small data structures that contain primarily data that is not intended to be modified after the struct is created. 5) A struct is a value type. If you assign a struct to a new variable, the new variable will contain a copy of the original.

What is meant by struct?

Struct definition (computing, programming) A data structure, especially one that serves to group a number of fields (in contrast to an object-oriented class with methods) or one that is passed by value rather than by reference. noun.

Why struct is used instead of class?

Structs copy the entire value on the assignment, whereas reference types copy the reference on assignment. So, large reference type assignments are cheaper than the value types. Instance field declarations in Struct cannot include variable initializers. But, static fields in Struct can include variable initializers.


1 Answers

Lets assume that you want to store ItemList<T> in an array.

Allocating an array of value types (struct) will store the data inside the array. If on the other hand ItemList<T> was a reference type (class) only references to ItemList<T> objects would be stored inside the array. The actualy ItemList<T> objects would be allocated on the heap. An extra level of indirection is required to reach an ItemList<T> instance and as it simply is a an array combined with a length it is more efficient to use a value type.

Struct vs class

After the inspecting the code for EntitySet<T> I can see that no array is involved. However, an EntitySet<T> still contains two ItemList<T> instances. As ItemList<T> is a struct the storage for these instances are allocated inside the EntitySet<T> object. If a class was used instead the EntitySet<T> would have contained references pointing to EntitySet<T> objects allocated separately.

The performance difference between using one or the other may not be noticable in most cases but perhaps the developer decided that he wanted to treat the array and the tightly coupled count as a single value simply because it seemed like the best thing to do.

like image 105
Martin Liversage Avatar answered Sep 29 '22 11:09

Martin Liversage