简体   繁体   English

在没有枚举的情况下在C#中实现自己的数据类型?

[英]Implementing own data types in C# without enum?

The main difference between built-in data types and user defined data types is that: built-in data types can accept literal values(values inserted into code directly,this process is also know as hard-codding). 内置数据类型与用户定义的数据类型之间的主要区别在于:内置数据类型可以接受文字值(直接插入代码中的值,此过程也称为硬编码)。

So is it possible create custom data type same as boolean which accepts three values: yes/no/maybe WITHOUT USING enums. 因此,可以创建与布尔值相同的自定义数据类型,该类型接受三个值:是/否/也许不使用枚举。

such as following code: 如以下代码:

MyCustomBoolean a = maybe;

I asked above question because i want to understand that built-in data types in C# are instructed in Core Language(such as C++ int,char...) or no? 我问了以上问题,是因为我想了解C#中的内置数据类型是用Core Language(例如C ++ int,char ...)指示的还是否?

---Update--- -更新-

for the second question,let me ask a question to make the 2nd question more clear: 对于第二个问题,让我问一个问题,以使第二个问题更明确:

I know that for example string is alias of System.String but does pure string in C# works without System.String? 我知道例如string是System.String的别名,但是C#中的纯字符串在没有System.String的情况下可以工作吗?

There are no ways to do this exactly as you've requested. 无法完全按照您的要求执行此操作。 You can however create constant fields in C# which can accomplish this result (named values) but only with the integral types or strings - in other words, the things you can already use for compile-time constants. 但是,您可以在C#中创建常量字段 ,该字段可以完成此结果(命名值),但只能使用整数类型或字符串-换句话说,您可以在编译时常量中使用的东西。 This can be particularly useful for otherwise Magic Values . 这对于其他魔术值特别有用。

public const string Maybe = "Maybe";
public const int Maybe = 0;

One way around this, though it will not be usable as a true constant, is to initialize static readonly fields or properties. 解决此问题的一种方法(尽管它不能用作真正的常量)是初始化静态只读字段或属性。 For example 例如

public static readonly MyCustomBoolean Maybe { get { return new MycustomBoolean(); } }
public static MyCustomBoolean Maybe = new MyCustomBoolean();

You can have a nullable bool instead for this specific case. 在这种情况下,可以使用可为空的布尔值。 With typical operations like: 具有以下典型操作:

bool? b = null
if (b.HasValue)
if (b.Value)
b = true

Where the maybe would be null, or having HasValue == false . 可能为null或HasValue == false

Literals are a feature of the C# language. 文字是C#语言的功能。 You cannot create new literals. 您不能创建新的文字。

You might be interested in a different programming language such as Nemerle that allows you to create user defined literals such as this XML literal: https://github.com/rsdn/nemerle/wiki/XML-literals 您可能对其他编程语言(例如Nemerle)感兴趣,该语言可让您创建用户定义的文字(例如XML文字): https : //github.com/rsdn/nemerle/wiki/XML-literals

Enums and constants usually fill the void for C#. 枚举和常量通常会填补C#的空白。 For more complex structures you often see some flavor of fluent API being used. 对于更复杂的结构,您经常会看到使用了一些流利的API。

Of course we can see the lack of this feature in C# meant that to provide a natural way to define queries meant the C# language spec needed to change to support LINQ. 当然,我们可以看到C#中缺少此功能意味着要提供一种自然的方式来定义查询,这意味着需要更改以支持LINQ的C#语言规范。 Had C# had similar meta programming features as Nemerle, then they could have accomplished the same goals as LINQ without changing the language syntax. 如果C#具有与Nemerle类似的元编程功能,那么它们可以实现与LINQ相同的目标,而无需更改语言语法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM