没有可空类型 [英] Not nullable types

查看:110
本文介绍了没有可空类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法建立在C#中的非可空类型(如日期时间或时间跨度)。



另外有没有办法(也许一个属性)执行不是空参数将不会被传递给方法和属性,而无需添加

 如果(ARG1 == NULL)
{
抛出新的ArgumentNullException(这个属性为空)
}


解决方案

的DateTime 时间跨度不是空的,因为它们是结构真是让人不是上课。



至于你的第二个问题,有可以在C#中执行此操作的标准方式。为此,您可以使用PostSharp,这是一个AOP框架,或者规格#,这是一种全新的语言(的扩展C#),它允许一些期望的行为。


Is there a way to create a non nullable type in C# (like DateTime or TimeSpan).?

Also is there a way (an attribute maybe) to enforce that not null arguments wouldn't be passed to methods and properties without adding

if(arg1 == null)
{
   throw new ArgumentNullException("this attribute is null")
}

解决方案

DateTime and TimeSpan are not-nullable since they are structs rather than classes.

As for your second question, there is no standard way you can do this in C#. You can do this using PostSharp, which is an AOP framework, or with Spec#, which is a whole new language (an extension of C#) which allows for some of desired behavior.

这篇关于没有可空类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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