Today, I am going to unlock the details on the Introduction to Data types in C#. Data Type mainly informs the compiler about the type of data that can be stored in a variable. Data types are divide into three types i.e. value data type, pointer data type, and reference data type.
Hey Guys! I hope you all are doing great. Today, I am going to give a detailed Introduction to Data types in C#. It's our 3rd tutorial in C# series & it's a theoretical tutorial so you just need to read it once.
I'll try to cover every aspect related to Data types in C# so you get a clear picture of what are data types and why we need them? So, let's get started with 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.
A. Predefined Value Data Types: These Value Data Types are predefined in C# i.e. bool, int, float, decimal, char etc.
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#:
B. User Defined Value Data Types: User can also create customized data types in C# using existing data types. i.e. structure or enumerations etc.
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.
Don't worry about these concepts, we will cover them on by one in our coming lectures.
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:
A. Predefined Reference Data Types: contain objects, strings etc.
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.
B. User Defined Reference Data Types: contain classes, interface, etc.
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.
Why we need nullable ?
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.
That's all for today. I hope you have got a clear idea about data types in C# language. However, if still you feel skeptical or have any question, you can ask me in the comment section below. I'll try to resolve your query according to best of my expertise. Keep your feedback and suggestions coming, it will help us to augment the quality of our article and give you flawless work that resonates with your needs and expectations. Stay tuned!
I am Syed Zain Nasir, the founder of The Engineering Projects (TEP). I am a
programmer since 2009 before that I just search things, make small projects and now I am sharing my
knowledge through this platform. I also work as a freelancer and did many projects related to
programming and electrical circuitry. My Google Profile+Follow