Primitive types in C #

Source: Internet
Author: User

I'd like to review the primitive types in C # in this article. While figuring out the basics of a primitive type is not necessary for your work, it is necessary to be a skilled person. At least can deal with some appear to compare BT face questions, haha!

As for what a primitive type is, I don't think it's clear to every developer that some of the friends only know how to apply it in their work (such as int,string). If a program based on a more solid friend of course, not to mention others, take my own, programming for more than three years, I do not care what is a primitive type, what it uses, in fact, do not know that these work will not have a decisive impact. If you do not know the concept of the base type, but will use the int,float and so on, the normal work is not much influence. But if someone talks to you about these things, it is very necessary to understand and learn about them.

A more common problem, one of which was recently said by the Garden friends:

First: What is the difference between int and int32?

Second: What's the difference between string and string?

Like many friends back to the same, these things are sometimes unnecessary too serious, but since there is such a problem, the general rules to have the answer. Here I list some of the following code that declares an integer variable:

The simplest

int a = 0;

A more concise

Int32 b=0;

Not simple.

int c = new int ();

Most not concise

System.Int32 d = new System.Int32 ();

Let's take a look at the IL code generated by these codes:

. method private Hidebysig static void Main (string[] args) cil managed

{

. entrypoint

Code size (0xa)

. maxstack 1

. Locals init ([0] int32 a,

[1] int32 B,

[2] int32 C,

[3] int32 D)

Il_0000:nop

il_0001:ldc.i4.0

il_0002:stloc.0

il_0003:ldc.i4.0

Il_0004:stloc.1

il_0005:ldc.i4.0

Il_0006:stloc.2

il_0007:ldc.i4.0

Il_0008:stloc.3

Il_0009:ret

}//End of method Program::main

Conclusion: They all declare a variable of a int32 type and initialize it. What is the reason for this? Here we can only use primitive types to explain. Let's take a look at C # 's primitive types and FCL, as well as some of the CLS's relationships. As you can see from the table below:

C # Primitive Typ FCL Type Cls-compliant
SByte System.sbte NO
Byte System.Byte YES
Short System.Int16 YES
UShort System.UInt16 NO
Int System.Int32 YES
UInt System.UInt32 NO
Long System.Int64 YES
ULong System.UInt64 NO
Char System.Char YES
Float System.Single YES
Double System.Double YES
Decimal System.Decimal YES
Object System.Object YES
String System.strign YES

The 1:int is mapped to the System.Int32 in the FCL. Here is enough to explain why the results of the above four creation variables are the same reason.

There is no real difference between 2:string and system.sting, and the simple point is that string is an alias for string.

The using method understands the relationship between primitive types and FCL: You can use a using statement to implement:

Using Sbyte=system.sbyte;

Using Int=system.int32;

Using String=system.string;

Conversions between FCL types: We know that there can be a related conversion between the FCL type variables, for example:

Int32 i=0;

Int64 j=i;//implicitly converted into Int64

Here is the corresponding IL code: we can see that one person conv.i8 the operation, and here is the conversion of the data type.

. method private Hidebysig static void Main (string[] args) cil managed

{

. entrypoint

Code size 7 (0x7)

. maxstack 1

. Locals init ([0] int32 I,

[1] Int64 J)

Il_0000:nop

il_0001:ldc.i4.0

il_0002:stloc.0

il_0003:ldloc.0

IL_0004:conv.i8

Il_0005:stloc.1

Il_0006:ret

}//End of method Program::main

Analysis: From an OO perspective, this conversion is not "too normal":

1:int32 and Int64 are two different types of data;

2: There is no inheritance relationship between the two.

Question: Why is it normal to switch between the two? Also because of the primitive type and the relationship.

There are two ways in which conversions between types are available:

First: implicit conversion, if the two types between the type is safe, you can directly convert;

Second: Display conversions, which are unsafe between types and require casting.

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.