
Introduction to Data types in C#


Introduction to Data types in C#
- Data Types in C# are used to inform the compiler about the type, size & nature of data that can be stored in a variable.
- Whenever we need to declare a variable, we need to inform the compiler about the data size & type of the variable.
- There are 3 types of data types available in C# programming, which are:
-
- Value Data Type
- Pointer Data Type
- Reference Data Type
-
- Here's a Flow Diagram of C# Data types for better understanding.

- Let's discuss these C# data types one by one in detail:
1. Value Data Types
- Value Data Type Variable is the simplest variable in C#, to which we can assign a value directly and they save this value on the stack, so it's not cleared by Garbage Collector.
- Each variable stores its data in a separate memory so we can't assign a single memory to multiple variables, although we can update their data quite easily.
- Value data types are further divided into two types, named:
- Predefined data types.
- User defined data types.
- Bool: a short form of Boolean, contains two values i.e. true or false.
- Int: a short form of integer.
- Char: a short form of character, which is used to store alphabets.
- Float: is used to store a floating point number i.e. number with decimal values.
- Here's a table showing Memory Size & Range of these Data Types in C#:

- Enumerations This value data type contains set of related named constants which are called as enumerator list. The "enum" is used to declare the word enumerations.
- Structure or struct is referred as a grouped list of variables that can be placed under one name in memory. It is same like a "Class" in the reference type.
2. Pointer Data Type
- The pointer, also called as indicator or locator, is a data type in C# that points towards the memory address of a data, we need to use ( * ) in its declaration.
- The pointer in C# can only hold the memory address of arrays and value types i.e. int, char, float etc.
- No conversion is allowed between pointer types and objects.
- However, conversion between two different pointer types is allowed, you can also convert between pointers and integral types.
- You can also point multiple pointers in same data type.

3. Reference Data Types
- Reference data types in C# don't consist of actual data, instead they point towards the reference to the data stored in a variable, it saves data in the heap.
- In reference data types, it's possible for two variable to reference to the same object.
- Reference Data Types are further divided into two types:
- String is a sequence of finite characters which can contain spaces and number. Total number of characters in the string refers to the string length. i.e. "I need 20 dollars" is a string.
- Object is referred as a block of memory that executes according to the blueprint.
Nullable Data Types
- In C# data types, the most commonly used data types are Value Types & Reference Types. Pointer types are not used that much.
- In above discussion, we have seen that:
- Value Data Types: int, float, double, structs, enums etc.
- Reference Data Types: string, interface, class, delegates etc.
- By default, Reference data types are nullable datatypes i.e. we can assign null value to these datatypes.
- Let's say, if I want to initialize a string with null value, I need to use this code: string[ ] data = null;
- On the contrary, Value Data Types are non nullable by default in C# i.e. we can't initialize an integer with null value.
- But we can make a value data type nullable by using a ( ? ) sign in front of the datatype.
- int? i = null; It will work fine and won't create any error because now i has created a nullable integer.
- Let's say you are designing a sign up form and you want user to select the gender i.e. male or female.
- We can store this value in Boolean variable but what happens if the user doesn't select any value.
- That's where nullable comes in handy and in this third case where user hasn't selected any value, we can assign a null value to our Boolean variable.
×
![]()