CLR notes: 5. Primitives, references, and value types

Source: Internet
Author: User

5.1 Primitive types

Any data type that is directly supported by the compiler (C #) is called a primitive type (primitive type), and the primitive type maps directly to the type that exists in the FCL. The Using string = System.String can be considered automatically generated.

Types in FCL have corresponding primitive types in C #, but not necessarily in the CLS, such as Sbyte,uint16, and so on.

C # allows implicit transformations when "secure"--no data loss, Int32 can be converted to Int64, but in turn shows transformations, and C # truncates the results when the conversion is displayed.

Unchecked and check control primitive type operations

C # each operator has 2 sets of IL directives, such as + corresponding add and add.ovf, which do not perform overflow checking, and the latter to check and throw System.OverflowException exceptions.

Overflow checking is turned off by default, which automatically corresponds to instructions such as add instead of add.ovf.

Methods to control C # overflow:

1. Use the/check+ compiler switch

2. Use operator checked and unchecked:

int b = 32767;      Max Short value

//b = Checked ((short) (b + 32767));          Throw 

system.overflowexception

b = (short) checked (b + 32767); Return-2

Here, the annotated statement will definitely check for overflow, run-time error, and the second sentence is checked in the Int32, so it will not overflow. Note that these two statements are intended only to explain when a check is functioning, two different semantics, rather than a single statement.

3. Use the checked and unchecked statements to achieve the same effect as the check operator:

int b = 32767;            Max Short value

checked
{
b = b + 32767;

Return
(short) b;

The System.Decimal type is primitive in C #, but not in the CLR, so check is not valid for it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.