Are C # primitive data types part of the language?

In C ++, the compiler knows about primitive data types such as int , but in C # these are mostly structures (e.g. System.Int32 ). But can I assume that C # knows about these types. I think this is because the int literal in C # is basically an instance of System.Int32 . For example, this will work:

 Console.WriteLine((12345).GetType()); 

Output:

 System.Int32 
+7
c #
source share
4 answers

The C # compiler knows about primitive data types. Whenever the compiler encounters the addition operator a + b , it calls the corresponding static method operator + .

However, if the operands are of primitive types, it generates IL code for direct calculation.

But usually the compiler and the CLR try to pretend that primitive types are just like any other.

In addition, as has been written, the compiler uses aliases for some basic types, such as int , char , bool , etc.

+1
source share

Of course, type names such as int and bool are C # keywords . A property that it shares with many languages, including C ++. They are currently mapped to types in the .NET Framework that you already know. Make no mistake assuming that System.Int32 is a structure, but of course, this is not how it is used on the processor. This is a type that easily fits into the processor register, which makes super-duper important for programs to work quickly. The structure declaration you can see is only representative for the boxed insert. Used to create the illusion that all types, even value types, come from System.Object.

The language rigidly bakes comparison with the physical type of the processor only because of its danger. Processors are changing rapidly. The canonical example is C, the grandson of C #, for 43 years and still very relevant in software development. It runs on a 16-bit computer with 64 KB of memory. Following the island of stability, 32-bit designs have been around for a long time, capable of addressing 4 GB of memory. Still relevant on 64-bit machines, today the norm is capable of addressing 256 terabytes. Four billion times more than 43 years, this is a terrific improvement. C-language survived this revolution by making keywords of type names, like C #, and not assuming size sizes.

Yes, the current C # compiler has strong baked knowledge of the types used when running the program on the machine. When you say int in your program, then it emits System.Int32 in the assembly. So do jitter and CLRs, they do most of the heavy lifting of value type mapping into processor registers and memory. Will it forever emit System.Int32? Not if he wants to still be in the know for 43 years. Or rather, the strictest architectural assumption baked in C # is the maximum size of the array. It cannot have more than 2 billion elements, indexing it with an int that goes beyond this gas. When you already have 256 terabytes of memory, this is, but a little speck of course.

For comparison, 128-bit processors are the future that is here today. IBM sells them on a truck. Some pretty interesting architectural things that you can do with such a large address space, the concept of a file becomes inappropriate. The whole file system is mapped to memory, if you want to write to a file, then you just write to memory. Files with more than 2 billion bytes, of course, are not unusual today.

There are several technical road blocks for cleaning before this is really necessary, hard drives are too slow and memory bus bandwidth is too low, garbage collecting a terabyte heap will temporarily freeze your program :) We will have to wait and see.

+3
source share

Any data types that directly support the compiler are called "primitive data types."

In C #, you can highlight an integer using the following syntax:

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

But many compilers (including C #) allow you to use the following syntax:

 int a = 0; 

We could also use the following:

 System.Int32 a = 0; int a = new int(); 

All these cards are directly of type System.Int32.

i.e. All primitive data types in C # are directly mapped to types that exist in the frame class library (FCL) library.

Also, as mentioned in the book "CLR through C #" when you use,

 Int a = 0; 

you may think that the C # compiler automatically assumes that there is a Using directive in the source file, for example:

 Using int = System.Int32; 
+1
source share

Of course they are.

See C # 77-85 specification pages:

http://www.microsoft.com/en-us/download/details.aspx?id=7029

Also C # is the CLS (Common Language Specification) complaint language, so it supports everything in CTS (Common Type System) here: https://msdn.microsoft.com/en-us/library/vstudio/zcx1eb1e(v=vs. 110) .aspx

0
source share

All Articles