是创建接受(可为空)值类型和引用类型可能是C#泛型方法? [英] Is creating a C# generic method that accepts (nullable) value type and reference type possible?

查看:347
本文介绍了是创建接受(可为空)值类型和引用类型可能是C#泛型方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个可同时接收的值类型引用类型的参数,即INT一个简单的方法是价值,而string是参考。

所以这就是我开始:

 公共BOOL areBothNotNull< T>(γ T P1,T P2)
{
    返程(p1.HasValue&安培;&安培; p2.HasValue);
}
 

所以,我希望能够用这样的:

  VAR R1 = areBothNotNull< INT>(3,4); //将是真实的
VAR R2 = areBothNotNull< INT>(3,NULL); //会是假的
VAR R3 = areBothNotNull<字符串>(三,四); //将是真实的
VAR R4 = areBothNotNull<字符串>(NULL,四大); //会是假的
 

但我遇到的第一个问题就是

类型'T'必须是一个非空的值类型,以将其用作参数'T'中的泛型类型或方法System.Nullable

当我添加结构约束,以我的方法

 公共BOOL areBothNotNull< T>(T P1,T P2?)其中T:结构
 

但是,现在的方法将不接受基于字符串的呼叫,并给了我这个错误:

类型字符串必须是为了用它作为参数T中的泛型类型或方法非空值类型。

这可能吗?或者为什么我们起价这样pvented p $?

解决方案

您的问题是,你想要的是相互冲突泛型类型的限制:

  • 可空< T> 适用于价值类型仅
  • 引用类型不是值类型

所以,你需要有两个重载你的code工作:

 公共静态布尔areBothNotNull< T>(T P1,T P2?)其中T:结构
{
    返程(p1.HasValue&安培;&安培; p2.HasValue);
}

公共静态布尔areBothNotNull< T>(T P1,T P2)
{
    返程(P1 = NULL和放大器;!&安培; P2!= NULL);
}
 

不过,下面这行永远不会编译:

  VAR R3 = areBothNotNull<字符串>(3,4);
 

有冲突这里,这里的泛型类型参数指出参数的数据类型为字符串,而code试图通过 INT !而非

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?

解决方案

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.

这篇关于是创建接受(可为空)值类型和引用类型可能是C#泛型方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆