Primitive type in C #

Source: Internet
Author: User

This articleArticleI want to review the primitive types in C. Although understanding the knowledge of the Chu-yuan type is not a necessary condition for your work, it is very necessary as a technical person. At least we can deal with some questions that seem Bt-like. Haha!

I don't think every developer knows what the primitive type is. Some friends only know how to apply it (such as int and string) at work ). If a friend with a solid programming foundation is not talking about others, for example, for programming for more than three years, I don't care much about what the primitive type is and what its usage is, in fact, I do not know that these will not have a decisive impact on my work. If you do not know the concept of the base type, but use int, float, and so on, it does not have a great impact on common work. However, if someone talks to you about these things, it is very necessary to understand and learn them.

FAQs, One of which is also said recently by yuanyou:

First: What is the difference between int and int32?
Second, what is the difference between string and string?

As many garden friends reply, these things sometimes do not need to be too truthful. However, since there is such a question, the general rule should have an answer. Here I list the following typesCode:

// Simplest
Int A =   0 ;
// Relatively concise
Int32 B = 0 ;
// Not concise
Int C =   New   Int ();
// Least 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) Pencil managed
{
. Entrypoint
// Code size 10 (0xa)
. Maxstack 1
. Locals Init ([ 0 ] Int32,
[ 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 an int32 type variable and initialize it. What is the reason? Here we can only use the primitive type for explanation. Let's take a look at the relationship between the primitive types of C # And FCL and Cls. We can see from the following table:

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


1: int is mapped to system. int32 in FCL. Here is enough to explain why the results of the above four variables are the same.
2: there is no real difference between string and system. Sting. To put it simply, string is an alias of string.

Understanding the relationship between primitive types and FCL in using mode: You can use the using statement to implement:

Using   Sbyte = System. sbyte;
Using   Int = System. int32;
Using   String = System. String;


Conversion between FCL types:We know that the FCL type variables can be converted accordingly, for example:

Int32 I = 0 ;
Int64 J = I; // Implicit conversion to int64

The following is the corresponding il code: we can see that there is a personConv. i8Here is the conversion of data types.

. Method Private Hidebysig Static   Void Main ( String [] ARGs) Pencil 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 the OO perspective, this conversion is not "too normal ":
1: int32 and int64 are two different data types;
2: There is no inheritance relationship between the two.
Q: Why can the two be converted normally? It is also because of the relationship between primitive types.

The conversion between types provides two methods:
1. implicit conversion. If the two types are type-safe, they can be directly converted;
Second: Display conversion. The data types are insecure and need to be converted forcibly.

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.