📜  c# enum 变量设置为 nonthing - C# 代码示例

📅  最后修改于: 2022-03-11 14:48:54.841000             🧑  作者: Mango

代码示例1
An enum is a "value" type in C# (means the the enum is stored as whatever value it is, not as a reference to a place in memory where the value itself is stored). You can't set value types to null (since null is used for reference types only).

That being said you can use the built in Nullable class which wraps value types such that you can set them to null, check if it HasValue and get its actual Value. (Those are both methods on the Nullable objects.

name = "";
Nullable color = null; //This will work.
There is also a shortcut you can use:

Color? color = null;
That is the same as Nullable;