C # Primitive types

Source: Internet
Author: User

In C # programming, there are two ways of initializing an integer:

(1), the more cumbersome method, the code is as follows:

New Int32 ();

(2), the minimalist method, the code is as follows:

int a=0;

Comparing two methods, the analysis is as follows:

The first type: too cumbersome, but is the standard way to initialize an integer

The second is to enhance the readability of the code and generate the same IL code as in the first way. The data types that are declared in this way are collectively referred to as primitive types.

Primitive types map directly to types that exist in the framework (FCL). For example, when an integer is initialized with a primitive type int, the int is mapped directly to the System.Int32 type in the FCL, and the compiler completes the process automatically.

Then there are the following 4 lines of code:

int 0;  // The simplest way 0 // the simple way int New int // Inconvenient Way System.Int32 d=New Int32 ();  // The most inconvenient way

The exact same IL code is generated in the 4 different ways of initializing an integer:

. methodPrivateHidebysigStatic voidMain (string[] args) CIL managed{. entrypoint. Maxstack1. Locals init ([0] Int32 num, [1] Int32 num2, [2] Int32 num3, [3] int32 num4) L_0000:nop L_0001:ldc.i4.0L_0002:stloc.0L_0003:ldc.i4.0L_0004:stloc.1L_0005:ldc.i4.0L_0006:stloc.2L_0007:ldc.i4.0L_0008:stloc.3L_0009:ret}

The following are all primitive types in C #, as well as the FCL type in the framework (FCL) of the primitive type, and the CLS (Common Language Specification) compatibility of the current primitive type, and other languages provide similar primitive types for types that are compatible with the common language specification.

It is a matter of benevolent see to use primitive or FCL types when developing. The C # Language specification suggests that we use primitive types, but there are still a lot of pits using primitive types, and here are a few examples:

(1), many people will be puzzled to use a string or string, is actually the same, if you use String to initialize a string, the compiler will map a string to a string.

(2), the int primitive type always maps to Int32, long maps to Int64, does not exist on 32-bit systems, int represents Int32, and changes Int64 on 64-bit systems.

(3), note that the float primitive type represents a single class, which is a pit. Such as: There is new BinaryReader (). Readsingle () This method, there is no new BinaryReader (). Readfloat () This method

C # Primitive types

Related Article

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.