Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 525
Sara Gamage Avatar asked Oct 23 '10 07:10

Sara Gamage


People also ask

What are examples of C corporations?

Big American companies like Microsoft and Walmart are C corporations—that is, their income is taxed under Subchapter C of the US Internal Revenue Code.

What is an example of as corporation?

Example of S Corporation Taxation Let's say, XYZ Inc. is an S corporation, which is owned 60 percent by Tom and 40 percent by Harry. If it makes a net income of $10 million in a financial year, Tom will report $6 million and Harry will report $4 million as income in their personal tax returns.

Is Apple an S Corp?

C corporations are the publicly traded companies you see everyday on Wall Street such as Microsoft, Intel, or Apple.

What are the advantages of C corporation?

One advantage of a C corp over an S corp or LLC is that it has an easier time attracting investors, including obtaining capital through equity financing. Owning shares is generally considered preferable to owning LLC membership interests. Also, venture capitalists prefer to invest in C corporations.


1 Answers

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.

like image 159
Fredrik Mörk Avatar answered Sep 20 '22 15:09

Fredrik Mörk