Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are primitive data types made in C#?

How do System.float, System.int and other primitives types work? I never understood how it was possible to make primitives structs and I wonder if I could make my own numeric type.  

like image 925
Giovanni Del Gallo Avatar asked Nov 20 '17 08:11

Giovanni Del Gallo


People also ask

What makes a data type primitive?

A primitive type is predefined by the language and is named by a reserved keyword. Primitive values do not share state with other primitive values. The eight primitive data types supported by the Java programming language are: byte: The byte data type is an 8-bit signed two's complement integer.

What is primitive data type in C?

Primitive is the most fundamental data type usable in the Programming language. There are eight primitive data types: Boolean, byte, character, short, int, long, float, and double. In a Programming language, these data types serve as the foundation for data manipulation.

Is primitive data type built-in data type?

Primitive Data Types: These data types are built-in or predefined data types and can be used directly by the user to declare variables. example: int, char, float, bool, etc. Primitive data types available in C++ are: Integer.

How are primitive data types stored?

Stack memory stores primitive types and the addresses of objects. The object values are stored in heap memory.


2 Answers

Assuming we're talking about a C# compiler which targets the Common Language Infrastructure (CLI), as almost everything does, it basically uses the primitive types exposed by the CLI.

There are effectively three levels of support to consider:

  • Genuine primitives, with their own representation and instructions in IL
  • Numeric types that the C# compiler has special knowledge of, but which aren't part of the the CLI - basically, System.Decimal. This is also part of the Common Type System (CTS) which means that if you create a const decimal in C#, you can still consume it in VB, for example. But there's still no direct IL support.
  • Other numeric types, such as BigInteger - you can write your own ones of these.

The middle ground of the second bullet allows C# to have decimal literals and decimal constants, neither of which are possible for the third bullet. For example, BigInteger doesn't have language support, so you can't write:

// This isn't valid
BigInteger bigInteger = 123456789012345678901234567890;

You'd have to parse a string representation instead. Likewise you can't have a const BigInteger.

(In theory it would be possible to have a type with support in C# but not in the CTS. I don't know of any such types.)

like image 61
Jon Skeet Avatar answered Oct 02 '22 16:10

Jon Skeet


Primitive types like int and float are supported directly by the processor, and the .net platform and languages have similar built-in support for them. So there are no way you could create your own primitives at the language level.

like image 20
JacquesB Avatar answered Oct 02 '22 16:10

JacquesB