Data types are everywhere in programming. They are used to define the type of variable declared in a program. In C#, there are three types of data types: 

  • The value types. 
  • The reference type (Object, dynamic, and string type)
  • Pointer data type.  We will discuss how to define, assign, and manipulate variables in the next chapter.

Value Type

The value type is the variable that directly contains the data. Whenever you declare a variable of type int, float, char, you are creating a value data type variable. In .NET Framework, value data types are derived from the class System.ValueType. 

The table below represents the built-in value type in C# in alphabetical order:

NameDescriptionType SizeRange
boolbool or Boolean hold of two values, true or false.1 byte
byteHolds 8-bit unsigned integers. Can only hold positive numbers. 8 bit0 to 255
charHolds 16-bit Unicode characters.16 bit0 to 65,535
decimalHolds a 128-bit signed floating-point value with 28 digit precision.128 bit –7.9 × 10^−28 to 7.9 × 10^28
doubleHolds a 64-bit signed floating-point value with 15 digit precision,64 bit5.0 × 10^−324 to 1.7 × 10^308
floatHolds a 32-bit signed floating-point value with 7 digit precision.32 bit1.5 × 10^−45 to 3.4 × 10^38
intHolds 32-bit signed integers.32 bit-2,147,483,648 to 2,147,483,647
longHolds 64-bit signed integers.64 bit–9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
sbyteHolds 8-bit signed integers.8 bit-128 to 127
shortHolds 16-bit signed integers.16 bit-32,768 to 32,767
uintHolds 32-bit unsigned integers. 32 bit0 to 4,294,967,295
ulongHolds 64-bit unsigned integers.64 bit0 to 18,446,744,073,709,551,615
ushortHolds 16-bit unsigned integers.16 bit0 to 65,535

Reference Type

Unlike value type, reference types do not contain the actual data assigned to a variable, but they contain a reference to the variable. The reference type points to a memory location where the value of a variable is stored.  

The tree reference types in C# are: object, dynamic, and string. 

Object:

The object data type is the base class of all types in C#. That means object data types can hold all sort of values. An object data type can be converted to value type, and vice versa. These operations are called boxing and unboxing. We will explain in other chapter in detail about the boxing and unboxing operations. 

Dynamic:

A dynamic variable can have any type, which changes during the runtime. the dynamic type is similar for the object data type, e except of that the type checking for object take place in the compile time, while the type checking of dynamic take place in the runtime. The dynamic data type usage comes with a complication and it’s advised to avoid using it.    

String:

The string type allows you to assign encoding characters values to a variable. It’s very important to know how to deal with string data type, since string is immutable, or ready only. Once a string is assigned a value, the content can’t be changed. So, when you try to manipulate a string, the value doesn’t change, but instead a new reference of that variable will get created in the memory containing the new value. This is why string is not a good for large data manipulation, such as creating a dynamic SQL Query or an error log message. That is why we use the string builder instead, which is derived from the System.Text Class. We will discuss in details about string builder in later chapter. 

Pointer Type 

The pointer types in C# are similar to the ones in C and C++. However, it’s rarely used in C#. More details will be discussed on pointer type later in this tutorial. 

Last modified: July 28, 2018

Comments

Write a Reply or Comment

Your email address will not be published.