简体   繁体   English

初始化System.Decimal v初始化十进制数据类型

[英]Initialising a System.Decimal v Initialisting a decimal data type

I am use to using the decimal data type like this: 我习惯于使用像这样的十进制数据类型:

decimal Cost = 30M;

This initialises the decimal with £30 (£ is in the context of my application). 这用30英镑初始化小数(在我的应用程序上下文中为英镑)。 I am planning to use a type alias for Decimal in my application like this: 我打算在我的应用程序中为Decimal使用类型别名:

using Cost = System.Decimal

I believe I have to use the object rather than the primitive type when using a type alias. 我相信使用类型别名时必须使用对象而不是原始类型。 Please let me know if that is not correct? 请让我知道这是否不正确?

Also I have noticed that I cannot do this: 我也注意到我不能这样做:

Decimal cost = new Decimal(30M);

I have to do this instead: 我必须这样做:

Decimal cost = new Decimal(30);

Is the initialisation code above suitable for a currency? 上面的初始化代码是否适用于货币?

This initialises the decimal with £30 (£ is in the context of my application). 这用30英镑初始化小数(在我的应用程序上下文中为英镑)。 I am planning to use a type alias for Decimal in my application like this: using Cost = System.Decimal 我打算在我的应用程序中像这样对Decimal使用类型别名: using Cost = System.Decimal

Ok, thats a valid option. 好的,那是一个有效的选择。

I believe I have to use the object rather than the primitive type when using a type alias. 我相信使用类型别名时必须使用对象而不是原始类型。 Please let me know if that is not correct? 请让我知道这是否不正确?

I'm not sure what you are asking, but if you are using a type alias, the whole idea is to use it: 我不确定您要问什么,但是如果您使用类型别名,则整个想法就是使用它:

var cost = new Cost(10);

Rememnber, a type alias is a type alias; 记住,类型别名是类型别名。 wherever you can System.Decimal , Cost is valid too. 只要可以在哪里System.DecimalCost也是有效的。

Also I have noticed that I cannot do this: Decimal cost = new Decimal(30M); 我也注意到我不能这样做: Decimal cost = new Decimal(30M);

Yup, and you can't do Cost cost = new Cost(30M); 是的,您不能做Cost cost = new Cost(30M); either, because decimal doesn't define such constructor overload. 也可以,因为decimal没有定义此类构造函数重载。 The reason being, if you already have a Decimal / Cost in hand why in the world would you want to new it up again? 原因是,如果您已经掌握了Decimal / Cost ,那么为什么要在世界范围内再次进行更新? Cost cost = 30M; would do just fine. 会很好的。

Also notice that Cost cost = 30; 还要注意, Cost cost = 30; is just fine too, the compiler will perform the implicit conversion for you. 也很好,编译器将为您执行隐式转换。

  1. Yes, you need to use the fully qualified type name to use an alias. 是的,您需要使用完全限定的类型名称来使用别名。 From the docs : 文档

    Create a using alias to make it easier to qualify an identifier to a namespace or type. 创建using别名,以使将标识符限定为名称空间或类型变得更加容易。 The right side of a using alias directive must always be a fully-qualified type regardless of the using directives that come before it. using别名指令的右侧必须始终是标准类型,而不管其前面的using指令如何。

    It's worth noting that using a type alias is very uncommon, particularly when using a built in value type like decimal . 值得注意的是,使用类型别名非常少见,尤其是当使用内置值类型(如decimal I would not recommend that you do that. 我不建议您这样做。

  2. You can't do new Decimal(30M) because the type doesn't have a constructor that takes a decimal, after all it would be pointless because you can simply do this (where the M qualifier is optional): 您不能执行new Decimal(30M)因为该类型没有采用小数的构造函数,毕竟这将毫无意义,因为您可以简单地执行此操作(其中M限定符是可选的):

     Decimal cost = 30M; 

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

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