什么课? (带问号的类)是指C#泛型类型约束? [英] What does class? (class with question mark) mean in a C# generic type constraint?

查看:211
本文介绍了什么课? (带问号的类)是指C#泛型类型约束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我试图找到另一个问题的答案时,我注意到该代码是在C#中编译的:

While I tried to find an answer to another question I noticed that this code compiles in C#:

public void Foo<T>(T obj)
    where T : class?
{
}

我没有设法在文档中找到它的含义.

I did not manage to find in the documentation what it even means.

推荐答案

它强制T必须是可为空的引用类型.

It enforces that T has to be a nullable reference type.

您为T设置的类型必须源自object?.

The type you set in for T, must derive from object?.

这是C#8中的一项新功能,可以将类型明确声明为可为空. 如果有

It's a new feature in C#8, to explictly declare a type as nullable. if you have

 Add<T>(T tmp);

您记录了文档,可以添加null;

You document, it's OK to Add null;

这篇关于什么课? (带问号的类)是指C#泛型类型约束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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