Data Types in a programming language describes what type of data a variable can hold. CSharp is a strongly typed language. Therefore every variable and object must have a declared type. The CSharp type system contains three Type categories. They are Value Types, Reference Types and Pointer Types. In CSharp, it is possible to convert a value of one type into a value of another type. The operation of Converting a Value Type to a Reference Type is called Boxing, and the reverse operation is called Unboxing.
When we declare a variable, we have to tell the compiler about what type of data the variable can hold or which data type the variable belongs to.
- DataType: The type of data that the variable can hold
- VariableName: the variable we declare to hold the values.
- int: is the data type
- count: is the variable name
The above example shows declare a variable ‘count’ for holding an integer value.
The following are the commonly using datatypes in C#.
The bool keyword is an alias of System. Boolean. It is used to declare variables to store the Boolean values, true and false. In C#, there is no conversion between the bool type and other types.
- C# Runtime type : System.Boolean
- CSharp declaration : bool flag;
- CSharp Initialization : flag = true;
- CSharp default initialization value : false
int variables are stored signed 32 bit integer values in the range of -2,147,483,648 to +2,147,483,647
- C# Runtime type : System.Int32
- CSharp declaration : int count;
- CSharp Initialization : count = 100;
- CSharp default initialization value : 0
The decimal keyword denotes a 128-bit data type.The approximate range and precision for the decimal type are -1.0 X 10-28 to 7.9 X 1028
- C# Runtime type : System.Decimal
- CSharp declaration : decimal val;
- CSharp Initialization : val = 0.12;
- CSharp default initialization value : 0.0M
The string type represents a string of Unicode characters. string variables are stored any number of alphabetic,
numerical, and special characters.
- C# Runtime type : System.String
- CSharp declaration : string str;
- CSharp Initialization : str = “csharp string”;