可以像内置类型一样直接分配常量值的 .NET 结构

标签 .net struct

您能否制作一个行为类似于内置类之一的结构,您可以在其中直接分配值而无需调用属性?

前任:

RoundedDouble count;
count = 5;

而不是使用
RoundedDouble count;
count.Value = 5;

最佳答案

您可以通过 implicit 执行此操作关键词。

例如,在您的情况下,您需要以下内容:

public static implicit operator RoundedDouble(double value)
{
     return new RoundedDouble(value);
}

关于可以像内置类型一样直接分配常量值的 .NET 结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3818156/

相关文章:

swift - Swift 中的哪些值类型支持写时复制?

c - typedef 指针的大小

c# - IIS 7 配置数据库 : Setting the framework version and the managed pipeline mode programmatically

c# - 获取实现接口(interface)的 "default"具体类

c# - 使用哪个隔离级别来防止读取数据?

c - fscanf、structs 和使用struct c 编程

.net - 如何在 .NET/C#/VS2008 中的项目之间共享命名空间。接收错误 : "are you missing an assembly reference?"

.net - Winform 富文本框 : How can I scroll the caret to the middle of the RichTextBox?

c - 为什么我的循环没有到达文件末尾?

c++ - 如何在定义后立即创建结构的指针和实例