C# Data Type – Introduction

A data type is a data representation which determines the following:

  1. How the values of a given type are stored in memory.
  2. The set of values that are stored.
  3. 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:

  1. Built-in data types – These are data types predefined by the programming language.
  2. 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 0 or 1.

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:

Size Equivalence
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.
  • Parameters.

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:

  1. Value Types – Holds an actual value say for example an int value of 45. Int in this case is a value type.
  2. Reference Types – Holds a reference to the value, not the actual value. e.g is the class.

Value Types

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:

  • bool : false
  • Integer : 0
  • Float : 0.0
  • char : single empty character
  • BigInteger : 0
  • DateTime : 1/1/0001 12:00:00 AM
  • Object : null

Example
“`c#
int age= new int();
float salary=new float();
double d=new double();
bool isMarried=new bool();
DateTime date=new DateTime();

Share



Share an Example

Share an Example

Browse
What is the capital of Egypt? ( Cairo )