What are the differences between C ++ and C # primitive data types?

I am trying to figure out the differences between C ++ and C # data types. I know that C # and java differ in that data types are stored as objects in C #, instead of having a base class library providing a wrapper class for representing data types as a Java object. However, I can't learn much about the differences between C # and C ++ data types ...

+3


source to share


2 answers


The difference you are describing is wrong. Java, C # and C ++ treat primitives as core objects. C and C ++, being low-level languages, save them this way - they are unique to the compiler as primitives.

There are thin wrappers in Java such as java.lang.Integer

which is a class that contains a single member variable int

.



C # can implicitly treat a primitive as an object and will convert on the fly, for example, int

to System.Int32

, as required by various situations. This process is called Boxing and Unboxing , of which the first is implicit and the second is explicit. See the linked article for more information.

+4


source


In simpler terms, C # primitive types such as int

bool

, short

etc. are organized as structures, as opposed to C ++ primitive types, which are not structures.

For example, in C #, in the most primitive style, int

you can call some methods (for example, you can call methods Parse

or Equals

). The same is true for primitive types bool

.

To go even further, Int32

and int

are completely identical types in C #, as well as bool

and Boolean

. Thus int

, bool

, short

etc. - these are the key words in C #, which actually masks the following structure Int32

, Boolean

, Int16

. You can try it by calling:

int a=int.MaxValue;
Int32 b = a;

      



In the first line, we create a variable a

whose type is int

. The value of the variable is a

set to int.MaxValue

, which is actually a constant, defined in type int

or more precisely Int32

.

On the second line, the value of b becomes the value of a. This confirms that a

they b

are variables of the same type, otherwise an error occurs.

On the other hand, in C ++, primitive types are not organized as structures, so you cannot call any method on a primitive type or an instance of a primitive type. They are also called compiler primitives.

+1


source







All Articles