Nullable primitives with Generics?

Public bool ValidateNotNull(T nullableField) { return Equals(nullableField, default(T)); } Updated: Either its a primitive and all is well, or its not null: public bool ValidateNotNull(T nullableField) { return typeof(T). IsPrimitive ||! Equals(nullableField, null); }.

Public bool ValidateNotNull(T nullableField) { return Equals(nullableField, default(T)); } Updated: Either its a primitive, and all is well, or its not null: public bool ValidateNotNull(T nullableField) { return typeof(T). IsPrimitive ||! Equals(nullableField, null); }.

No, because some of the fields I'm passing in might be 0 or false, which are valid. It's only null which is invalid. – Lunivore Aug 27 '10 at 12:31 Ahh ok.. updated my answer!

:) – Arcturus Aug 27 '10 at 12:45 Your update is incorrect. The first part is returning true if T is a value type. System.

Nullable is a value type, so bool? Bar = null would be evaluated by your method to be not null. – Anthony Pegram Aug 27 '10 at 12:55 Dunno whether this was your original answer but I thought I'd give it a go, and it works - unit testing rocks.

False and default(bool? ) are not the same. Thank you!

This solves my problem and I learnt a little more about C#! (And it does work - bool? Bar = null does evaluate to null.) – Lunivore Aug 27 '10 at 12:57 You are right.. so I changed the code to IsPrimitive.. That should do the trick!

– Arcturus Aug 27 '10 at 12:58.

The code below will validate nullable types and reference types: public bool ValidateNotNull(Nullable nullableField) where T:struct { return nullableField. HasValue; } public bool ValidateNotNull(T nullableField) where T:class { return nullableField! =null; }.

Though. – Lunivore Aug 27 '10 at 12:29 It does on my machine! – Sean Aug 27 '10 at 13:27 Ah, you're right - I didn't spot that they were actually different implementations as well as different signatures.

I prefer just having one method, but this is also beautiful so have an upvote. – Lunivore Aug 27 '10 at 18:19.

I also want to be able to do the same thing with some other fields, including strings, ints, etc OK, so you want a method that can be passed either a Nullable (which is a struct), or a reference type, and will return true iff the argument is null (note that your name appears to be the wrong way round, but there you go). The thing is, if that's all this method has to do, then you don't need generics because you don't need type-safety. This example works and does what you want, I think: class Program { static void Main(string args) { int?

X = null; int? Y = 5; string s = null; string r = "moo"; Console. WriteLine(ValidateNotNull(x)); Console.

WriteLine(ValidateNotNull(y)); Console. WriteLine(ValidateNotNull(s)); Console. WriteLine(ValidateNotNull(r)); Console.ReadLine(); } private static bool ValidateNotNull(object o) { return o == null; } } This outputs True False True False which is I believe your required output.

Simple and elegant. – jdv-Jan de Vaan Aug 27 '10 at 11:01 Hm, okay - I actually use the fact that it's a boolean elsewhere; this isn't the complete code, nor is it the only time I've run into this problem. I'm still looking for a way to maintain the type without having to cast.

Thanks for the note on the! = (copying and pasting from code which does some slightly different stuff! ) – Lunivore Aug 27 '10 at 12:29.

I don't know why you thing your method doesn't work (apart from using! = rather than ==), because as far as I can tell, it already works! You can compare any value of type T with null (when T is a generic type parameter).

If a non-nullable value type is used for T, the result will be treated as a constant (false for == null, true for! = null). So, this should be fine.

Public bool ValidateNotNull(T nullableField) { return nullableField! = null; } But this would also work without generics: public bool ValidateNotNull(object nullableField) { return nullableField! = null; } (Because boxing a Nullable null value returns a null reference.).

Like I said, it compiles and works fine - but Resharper complains. – Lunivore Aug 27 '10 at 12:30 In that case, use the second solution (with object, not T) – Ruben Aug 27 '10 at 17:46.

You need to use public bool ValidateNotNull(T nullableField) { return nullableField. HasValue; }.

This does not compile! – Arcturus Aug 27 '10 at 10:53 Yes, Yes it does. Stop marking down if you got no clue.

– Ashley Aug 27 '10 at 14:34.

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