Hey Guys! I hope you all are doing great. I am back to give you daily dose of information so you can excel in your relevant field. 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. I’ll try to cover every aspect related to Datatypes in C# so you get a clear idea what are datatypes and what they are used for? Let’s get started.
Introduction to Data types in C#
- Data Types are widely used in C# programming language.
- Data Type mainly informs the compiler about the type of data that can be stored in a variable.
- Or in order words, every time you declare a variable, you need to assign a specific data type to that variable.
There are 3 types of data types in C# programming.
- Value Data Type
- Pointer Data Type
- Reference Data Type
Let’s discuss one by one in details.
1. Value Data Types
- As stated earlier we need to define the type of data type before we declare the variable.
- Variable in value data type contains the value that is directly stored in the memory and each variable contains its own copy of data.
- Value type variable are stored in a stack which means they can not be marked for garbage collection.
Value data types are further divided into two types called Predefined data types and User defined data types.
A. Predefined Value Data Types contain char, bool, float, int etc.
Int, a short form of integer, mainly contains a number. Decimal value can not be stored in int.
Char, is a short form of character which is used to store alphabets.
Bool, a short form of Boolean, contains two values i.e. true or false. It is widely used in C# programming.
Float is used to contain a floating point numbers which can also contain decimal values.
B. User Defined Value Data Types contain structure or enumerations etc.
- User defined data type is a data type that can be generated from existing data type.
- In other words, you can create your own customized data type from existing data type.
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 variable in C# language that points towards the memory address of a data.
- The pointer in C# can only hold the memory address of arrays and value types.
- No conversion is allowed between pointer types and object.
- However, conversion between two different pointer types is allowed also you can convert between pointers and integral types.
- You can also point multiple pointers is same data type.
3. Reference Data Types
- Reference data types in C# language don’t consist of actual data instead they point towards the reference to the data stored in a variable.
- In reference data types, it is possible for two variable to reference the same object.
- Reference type variables are stored in a heap which means when reference variables are of no use, they can be referred for a garbage collection.
Reference are further divided into two types.
Predefined Reference Data Types contain objects, strings etc.
A. 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.
B. Object is referred as a block of memory that executes according to the blueprint.
User Defined Reference Data Types contain classes, interface, etc.
A. Class is like a blue that defines the contents of the object. It is used to create object at any time. If you define a class named as “POOL” and then we initialize variable P of class type “POOL” then P is said to have the object of the POOL.
B. Interface is referred as a shared boundary in which two different components of the computer can exchange information.
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!