What are “native” and “literal” keywords

后端 未结 2 1634
旧巷少年郎
旧巷少年郎 2020-12-30 11:03

I see many times using native and literals \"keywords\" in C# articles. What do they mean?

examples:

string.Empty article:

2条回答
  •  囚心锁ツ
    2020-12-30 11:35

    From the C# spec section 2.4.4:

    A literal is a source code representation of a value.

    So for example there are literals for strings and numbers:

    string x = "hello";
    int y = 10;
    

    ... but C# has no literal syntax for dates and times; you'd have to use:

    DateTime dt = new DateTime(2011, 12, 11);
    

    As for native support - there are different levels of "native" here, but as far as C# is concerned, I'd usually consider it type-specific support in whatever output format is used. So for example, there are IL instructions to deal with the binary floating point types (float, double) but when the C# compiler emits code dealing with decimal values, it has to call the operators declared in System.Decimal. I'd therefore consider that float and double have native support in IL, but decimal doesn't.

    (It would be possible to write a C# compiler targeting a different platform which did have native support for decimal - or which didn't have native support for float and double, for example. Unlikely, but possible.)

    Then when the IL is being run in an execution engine, that will be running on top of "real" native code - x86 for example - which can have specific support for certain types. That's another level of "native". For example, if someone came up with a new version of IL which included native support for decimal, that wouldn't mean that the CPUs themselves suddenly gained native support.

提交回复
热议问题