Is creating a C# generic method that accepts (nullable) value type and reference type possible?

P2) where T : struct { return (p1. HasValue && p2. HasValue); } public static bool areBothNotNull(T p1, T p2) { return (p1!

= null && p2! = null); } Still, the following line will never compile: var r3 = areBothNotNull(3, 4) There is a conflict here, where the generic type argument states that the parameters are of type string but the code tries to pass int s instead.

P2) where T : struct { return (p1. HasValue && p2. HasValue); } public static bool areBothNotNull(T p1, T p2) { return (p1!

= null && p2! = null); } Still, the following line will never compile: var r3 = areBothNotNull(3, 4); There is a conflict here, where the generic type argument states that the parameters are of type string, but the code tries to pass ints instead.

Reference types are not value types" LOL – Danny Chen Oct 23 '10 at 8:27 I would also add where T : struct and where T : class constraints to methods. – Andrew Bezzub Oct 23 '10 at 8:44 Sorry it was a copy/paste mistake for line r3, I'll update the question, "three", "four" should be the parameters. – Sara Gamage Oct 23 '10 at 10:44 Also thanks, I see it's about understanding "Nullable" now :) – Sara Gamage Oct 23 '10 at 10:49 @Sara: regarding understanding Nullable, I wrote a blog post about that a while ago: softwareblog.alcedo.Com/post/2010/02/16/… – Fredrik Mörk Oct 23 '10 at 13:45.

If you want to achieve this, you need to use non nullable types in your parameters areBothNotNull(T p1, T p2) and check whether T is a Nullable in your implementation If so, use IsNull otherwise, use object. ReferenceEquals (I presume you want to ignore value types like int...).

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