A data type is a data representation which determines the following:
- How the values of a given type are stored in memory.
- The set of values that are stored.
- The set of operations that can be applied to all the values in that set.
A data type can also simply be referred to as a type.
Every variable normally has a data type that determines the kind of data that it can hold.
Categories for Data Types
Data types can categorized into two general categories:
- Built-in data types – These are data types predefined by the programming language.
- User-Defined data types – Custom data types created by the programmer.
Basic Measurement Units of data
Computers can handle as the smallest piece of data a bit.
A bit is a single binary digit of memory that can have either
A byte is a chunk of memory holding
8 bits. A signed byte can hold from 0 (00000000 in binary) to 255 (11111111 in binary).
Some of the measurement units you may have heard of in computers with regards to space include:
|1 byte||8 bits|
|1 Kilobyte||1024 bytes|
|1 Megabyte||1024 kilobytes|
|1 Gigabyte||1024 megabytes|
|1 Terabyte||1024 gigabytes|
Multiple bytes get grouped into words that contain 2, 4 or more bytes depending on the computer hardware.
Majority of computers these days use 4-byte(32-bit) words or 8-byte(64-bit) words.
C# also groups bytes in different ways to form data types with a greater logical meaning. E.g it uses 4 bytes to make an integer, a numeric data type that holdvalues between:
−2,147,483,648 to 2,147,483,647.
C# defines some keywords for fundamental data types. These can be used to represent:
- Local variables
- Class member variables.
- Return values for methods.
Why Data Types?
C# is a strongly typed programming language.
This implies that in C# all operations are type checked at compile time by the compiler for type compatibility.
So if an illegal operation or assignment is detected, then an error is flaged immediately. The error has to be fixed before successful compilation.
Thus this compile-time type checking helps write safe code that is reliable.
However, obviously for us to talk about type checking then it means we need the types, or the data types.
Therefore all variables, expressions and values in C# have types. Having types also helps in determining the operations that can be done on given types.
In C# there are two general categories of data types:
- Value Types – Holds an actual value say for example an int value of
45. Int in this case is a value type.
- Reference Types – Holds a reference to the value, not the actual value. e.g is the class.
Value types hold actual values, single values.
are also called simple types. Or primitive types.
Ther are 13 built in value types in C#. They provide the basis of C#’s type system.
The low level data elements onto which all C# programs rely on.
Using the new Operator with Intrinsic Data Types
All the intrinsic data types support a default constructor.
This allows the creation of variables using the new keyword. This will automatically set the variable to its default value as follows:
char: single empty character
1/1/0001 12:00:00 AM
int age= new int();
float salary=new float();
double d=new double();
bool isMarried=new bool();
DateTime date=new DateTime();