By Sara Gamage


2010-10-23 07:56:25 8 Comments

I want to create a simple method that accepts both value type and reference type parameters, i.e. int is value, and string is reference.

So this is what I start with:

public bool areBothNotNull<T>(T? p1, T? p2)
{
    return (p1.HasValue && p2.HasValue);
}

So I want to be able to use it like this:

var r1 = areBothNotNull<int>(3, 4); // will be true
var r2 = areBothNotNull<int>(3, null); // will be false
var r3 = areBothNotNull<string>("three", "four"); // will be true
var r4 = areBothNotNull<string>(null, "four"); // will be false

But the first issue I encounter is

The type 'T' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable'

To proceed I add a struct constraint to my method

public bool areBothNotNull<T>(T? p1, T? p2) where T : struct

But now the method won't accept the string based calls, and gives me this error:

The type 'string' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method.

Is this possible? Or why are we prevented from doing this?

1 comments

@Fredrik Mörk 2010-10-23 08:09:37

Your problem is that you want generic type constraints that are conflicting with each other:

  • Nullable<T> works with value types only
  • Reference types are not value types

So you will need to have two overloads for your code to work:

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

public static bool areBothNotNull<T>(T p1, T p2)
{
    return (p1 != null && p2 != null);
}

Still, the following line will never compile:

var r3 = areBothNotNull<string>(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.

@Andrew Bezzub 2010-10-23 08:44:20

I would also add where T : struct and where T : class constraints to methods.

@Sara Gamage 2010-10-23 10:44:00

Sorry it was a copy/paste mistake for line r3, I'll update the question, "three", "four" should be the parameters.

@Fredrik Mörk 2010-10-23 13:45:08

@Sara: regarding understanding Nullable<T>, I wrote a blog post about that a while ago: softwareblog.alcedo.com/post/2010/02/16/…

Related Questions

Sponsored Content

11 Answered Questions

[SOLVED] Can't operator == be applied to generic types in C#?

7 Answered Questions

[SOLVED] How do I make the return type of a method generic?

12 Answered Questions

20 Answered Questions

[SOLVED] Create Generic method constraining T to an Enum

4 Answered Questions

[SOLVED] Help with C# generics error - "The type 'T' must be a non-nullable value type"

  • 2010-02-09 16:27:35
  • Josh Kelley
  • 56516 View
  • 87 Score
  • 4 Answer
  • Tags:   c# generics

10 Answered Questions

[SOLVED] Nullable type as a generic parameter possible?

  • 2008-10-16 15:53:42
  • Tom Pester
  • 167922 View
  • 238 Score
  • 10 Answer
  • Tags:   c# generics

296 Answered Questions

[SOLVED] Hidden Features of C#?

  • 2008-08-12 16:32:24
  • Serhat Ozgel
  • 631068 View
  • 1476 Score
  • 296 Answer
  • Tags:   c# hidden-features

5 Answered Questions

[SOLVED] C# nullable string error

  • 2008-10-09 14:03:57
  • Mike Fielden
  • 128780 View
  • 135 Score
  • 5 Answer
  • Tags:   c# nullable

4 Answered Questions

Sponsored Content