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#)

(1/2) > >>

wraith808:
I found something out that was pretty strange. 

I have an object that's based off the IEditableObject interface.  It maps to certain db fields- I'm trying to abstract the data layer from the application so that it can be replaced; we have a legacy system that we're going to try to get rid of down the line, so this is a first step in that direction.  I was going to do a bulk memory copy based on the memory location of each field in the object, but by necessity, since some of the fields are nullable, we use nullable types to represent those fields, i.e. int? instead of int. 

Going further, i realized that a nullable type is just a struct with a value and a bool for isnull, which makes sense, but I never thought of it that way.  So I was going to have to get pointers to the fields to get the memory locations- but you can't get a pointer to a nullable type.  I created a struct that was the exact same format as the nullable type, and was able to get a pointer to that...so I don't understand the logic behind this...

Thoughts?

Renegade:
That's a very interesting problem. I've never used pointers for nullable types, so I can't say anything intelligent. But this thread will be interesting to watch...

Ath:
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:
There's this blog article from around when nullable types where introduced (.NET 2.0), that has some more interesting info

And also this blog post that puts a claim around my original assumption... (I did not check these claims/examples though)

Ath:
And finally, a Stack Overflow article on the subject, showing a nice extension method that might come in useful

Navigation

[0] Message Index

[#] Next page

Go to full version