为了在通用类型或方法"Nullable< T>"中将其用作参数"T",类型"MyObject"必须是不可为空的值

编程入门 行业动态 更新时间:2024-10-17 17:27:09
本文介绍了为了在通用类型或方法"Nullable< T>"中将其用作参数"T",类型"MyObject"必须是不可为空的值类型.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用 framework 4.5

我收到以下错误

错误CS0453类型'MyObject'必须是不可为空的值类型,以便将其用作通用类型或方法'Nullable'中的参数'T'

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

public async Task<Nullable<MyObject>> MyMethod(string myParamter) {}

我也尝试过

public async Task<MyObject?> MyMethod(string myParamter) {}

如果我将其设置为可为空,那么为什么出现此错误消息的情况下方法名称下方的红线

If I set the thing as nullable then why do I get the red line below the method name with this error message

stackoverflow answer 很简单,将返回类型设置为可为空,但对我来说Visual Studio不允许.

The stackoverflow answer is easy, make the return type nullable but for me Visual studio doesn't allow it.

推荐答案

由于MyObject是一个对象,并且对象根据定义可以为空,因此按顺序使用Nullable<T> struct或?运算符是没有用的支持空值.

Since MyObject is an object, and objects are nullable by definition, there is no use to use the Nullable<T> struct or ? operator in order to support null values.

Nullable<T>用于结构(例如DateTime)和值类型(例如int,float等).

Nullable<T> is for structs (like DateTime) and value types like int, float, etc.

更多推荐

为了在通用类型或方法"Nullable&lt; T&gt;"中将其用作参数"T",类型"MyO

本文发布于:2023-11-02 14:14:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1552625.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类型   将其   为空   参数   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!