English 中文(简体)
为什么对“法令”的“明确经营人”的称呼?
原标题:Why does an explicit cast to ‘decimal’ call an explicit operator to ‘long’?

考虑以下法典:

class Program
{
    public static explicit operator long(Program x) { return 47; }

    static int Main(string[] args)
    {
        var x = new Program();
        Console.WriteLine((decimal) x);
    }
}

令我惊讶的是,这一产出在<代码>47<>>/代码>上;换言之,<代码>可乘以长途<>代码/代码>,即使该编号为<编码>decimal。

C#pec中是否有人明确指出,这种情况应当发生(如果是,究竟是在哪里发生),还是是因为我的一些其他规则缺失?

最佳回答

我已找到答案。 首先,有一种概念是encompassed,由另一个类别界定,载于6.4.3 对用户界定的转换进行评价<>。

If a standard implicit conversion (§6.3.1) exists from a type A to a type B, and if neither A nor B are interface-types, then A is said to be encompassed by B, and B is said to encompass A.

6.3.1 Standard implicit conversions states that “Implicit numeric conversions (§6.1.2)” are a standard implicit conversion, and 6.1.2 Implicit numeric conversions in turn defines an implicit conversion from long to decimal. Therefore, long is encompassed by decimal.

其次,6.4.5 用户定义明确的转换表示,确定明确转换是否适用的一个阶段是:

Find the set of applicable user-defined and lifted conversion operators, U. This set consists of the user-defined and lifted implicit or explicit conversion operators declared by the classes or structs in D that convert from a type encompassing or encompassed by S to a type encompassing or encompassed by T. If U is empty, the conversion is undefined and a compile-time error occurs.

此处,<代码>D系指早先步骤的结果,在此情形下,该步骤仅包含decimal ,Programobject。 因此,既定的<代码>U将包含Program-to-long。 明确操作 我宣布,<代码>long包含在decimal(正如我们先前发现的那样)。

下一个步骤之一选定<代码>long为最具体的目标类型,。 www.un.org/Depts/DGACM/index_french.htm

最后,同一议会的最后一步是:

最后,适用转换:

  • If S is not SX, then a standard explicit conversion from S to SX is performed.
  • The most specific user-defined conversion operator is invoked to convert from SX to TX.
  • If TX is not T, then a standard explicit conversion from TX to T is performed.

此处的<代码>S和S/code>均为Program,因此第一部分没有。

问题回答

The only explanation I can think of is that the compiler is smart enough to realize there is an implicit operator that will convert long to decimal, that it can use to satisfy the explicit conversion between Program and decimal when Program can only convert to long.

http://www.un.org。 这里我们是;数字类型之间的换算成文:

6.1.2 默示数字换算

默示数字换算为:

· 从by到 short、 in、长、浮、两倍或小.。

· From byte to short, ushort, int, uint, long, ulong, float, double, or decimal.

· 从短期到内、长期、浮动、双重或 de。

· From ushort to int, uint, long, ulong, float, double, or decimal.

· 从中长期来看,浮、双倍或小数。

· 从int到长、ul、浮、两倍或小.。

From long torot, Double, or decimal.

· 从ul到浮、翻一番或 de。

· From char to ushort, int, uint, long, ulong, float, double, or decimal.

· 从浮到两倍。

Conversions from int, uint, long, or ulong to float and from long or ulong to double may cause a loss of precision, but will never cause a loss of magnitude. The other implicit numeric conversions never lose any information.

There are no implicit conversions to the char type, so values of the other integral types do not automatically convert to the char type.

因此,C#在转换方案与数字之间时知道,它可以暗中从任何数字类型转换成正数,因此,在进行这种明确转换时,它将寻找任何能够将方案变成数字型的经营者。

什么是令人感兴趣的,如果你也明确转而回48岁,情况如何? 汇编者会选取什么?





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...