带有问号好奇的C#语法 [英] A curious C# syntax with a question mark

查看:242
本文介绍了带有问号好奇的C#语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 私人枚举E_Week
{
   周一= 0,
   星期二,
   。 。 。
}

什么是以下code是什么意思?

  E_Week?周= NULL;

是不是等于下列code?是什么的函数'?'在这里签字?

  E_Week周= NULL;


解决方案

您code是使用什么叫做的可空类型。枚举,很像一个int或一个DateTime,就是被称为价值型,这是需要始终有一定的价值。可空类型允许你对待值类型,如果他们这样做允许空值。

例如,这code是无效的,不能编译,因为枚举不能为空:

  E_Week周= NULL;

但是,这code是有效的:

  E_Week?周= NULL;

和它是完全一样的:

 可空< E_Week>周= NULL;

private enum E_Week
{
   Mon = 0,
   Tue,
   . . .
}

What does the following code mean?

E_Week? week= null;

Is it equal to the following code? What is the function of the '?' sign here?

E_Week week= null;

解决方案

Your code is using what's called a nullable type. An enum, much like an int or a DateTime, is what is known as a "value type", which is required to always have some value. Nullable types allow you to treat value types as if they do allow null values.

For example, this code is invalid and won't compile because enums cannot be null:

E_Week week = null;

But this code is valid:

E_Week? week = null;

And it's exactly the same as this:

Nullable<E_Week> week = null;

这篇关于带有问号好奇的C#语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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