Since connections are inherently not type safe and do not contain pointers, the CLR puts a lot of emphasis on both.
Consider the following:
union MyUnion { private List<int> myList; private int integer; public MyUnion(int foo) { integer = foo; } public List<int> List { get { return myList; } } }
Not only can this not work very much (especially on a 64-bit platform), but it also violates so many CLR guarantees that it hurts him to read it.
First, it allows you to get a pointer to an object without actually using pointers, so all unions must be classified by unsafe . Then it also allows you to change this pointer, which can have very funny consequences: for example, if you accidentally change it to the address of another valid list, the garbage collector may decide to move this list around and change the value of your integer without indicating.
It is also completely unsafe. Lists have nothing to do with integers; but since integers look like pointers, this is "not so bad." Now consider the following example:
union MyUnion { private List<int> myList; private Form myForm; public MyUnion(Form theForm) { myForm = theForm; } public List<int> List { get { return myList; } } }
Now, what will it be with your form if you call MyUnion.List.Add (4)? A lot of obvious wreck. Because it is completely unsafe, but unions allow you to believe in it.
source share