ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

Other Software > Developer's Corner

Pointers to nullable types (C#)

<< < (2/2)

wraith808:
I've done no research on the subject, though I've done some stuff, including nullable types, in C#. But not in this direction. Nevertheless...

I kind of assume you read this msdn subject explaining nullable types?
It states:
T can be any value type including struct; it cannot be a reference type
--- End quote ---

This makes me suspect the actual implementation is something handled at compile-time, so pointers for getting at it won't exist during runtime?
-Ath (May 07, 2011, 08:59 AM)
--- End quote ---

Nothing so esoteric.  A nullable type is a struct, and using reference types as fields in structs is A Bad Idea.

When you have a struct that contains a reference type and you copy an instance of the struct, the reference type inside isn't "deep" copied, because it is simply a reference (pointer) to a memory location containing the actual class. So if you copy a struct that contains class instances, the copy will be referencing the same instances as the original.

So I think that's why nullable types can't be reference types- and it would be pointless in any case, since any reference type is already nullable.

Thanks for the links also, but neither one of those explains why you can't get a pointer to System.Nullable.  It's implemented as a struct (as they point out in the links) so why is it special?  As I said, if I create a struct that has the exact same format as System.Nullable, I can get a pointer to it... so again, why is it special?

Ath:
@wraith808: That all makes perfectly good sense.

so again, why is it special?
-wraith808 (May 07, 2011, 08:47 PM)
--- End quote ---
A few more thoughts:

* They (MS) explicitly don't want you to do this
* You could exchange the int? by Int32 and get a pointer to that

wraith808:
@wraith808: That all makes perfectly good sense.

so again, why is it special?
-wraith808 (May 07, 2011, 08:47 PM)
--- End quote ---
A few more thoughts:

* They (MS) explicitly don't want you to do this
* You could exchange the int? by Int32 and get a pointer to that-Ath (May 08, 2011, 05:30 AM)
--- End quote ---

That wouldn't work in this case.  It needs to be nullable to be able to store the database value, and the idea is to do a bulk memory copy, mapping the memory locations of the values over the memory locations of the structure.  And yeah, I think at this point that MS just explicitly made this impossible.

Renegade:
IntPtr doesn't help, eh?

wraith808:
IntPtr doesn't help, eh?
-Renegade (May 08, 2011, 07:32 PM)
--- End quote ---

Unfortunately not. :(

Navigation

[0] Message Index

[*] Previous page

Go to full version