C# - Data Types
Data types specify the type of data that a valid C# variable can hold. C# is a
strongly-typed language. It means we must declare the type of a variable that
indicates the kind of values it is going to store, such as integer, float, decimal,
text, etc.
There are 3 types of data types in C# language.
Types Data Types
Value Data Type short, int, char, float, double, struct,
enum, null
Reference Data Type String, Class, Object, Interface and array
Pointer Data Type Pointers
Value Data Type
The value data types are integer-based and floating-point based. C# language supports
both signed and unsigned literals.
There are 2 types of value data type in C# language.
1) Predefined Data Types - such as Integer, Boolean, Float, double etc.
2) User defined Data Types - such as Structure, Enumerations, etc.
1) Predefined Data Types: The memory size of data types according to 32 bit operating
system.
Data Memory Range
Types Size
Char 1 byte -128 to 127
signed char 1 byte -128 to 127
unsigned 1 byte 0 to 127
char
Short 2 byte -32,768 to 32,767
signed short 2 byte -32,768 to 32,767
unsigned 2 byte 0 to 65,535
short
Int 4 byte -2,147,483,648 to -2,147,483,647
signed int 4 byte -2,147,483,648 to -2,147,483,647
unsigned int 4 byte 0 to 4,294,967,295
Long 8 byte ?9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
signed long 8 byte ?9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
unsigned 8 byte 0 - 18,446,744,073,709,551,615
long
Float 4 byte 1.5 * 10-45 - 3.4 * 1038, 7-digit precision
Double 8 byte 5.0 * 10-324 - 1.7 * 10308, 15-digit precision
Decimal 16 byte at least -7.9 * 10?28 - 7.9 * 1028, with at least
28-digit precision
int myNum = 5; // Integer (whole number)
double myDoubleNum = 5.99D; // Floating point number
char myLetter = 'D'; // Character
bool myBool = true; // Boolean
string myText = "Hello"; // String
2) User defined Data Types
Structure: In C#, struct is the value type data type that represents data structures.
It can contain a parameterized constructor, static constructor, constants, fields,
methods, properties, indexers, operators, events, and nested types.
struct Coordinate
{
public int x;
public int y;
}
enum: enum (or enumeration type) is used to assign constant names to a
group of numeric integer values. It makes constant values more
readable, for example, WeekDays.Monday is more readable then number 0
when referring to the day in a week.
An enum is defined using the enum keyword, directly inside a namespace,
class, or structure. All the constant names can be declared inside the
curly brackets and separated by a comma.
Example:
enum WeekDays
{
Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
}
Reference Data Type
The reference data types do not contain the actual data stored in a variable, but they
contain a reference to the variables.
If the data is changed by one of the variables, the other variable automatically reflects
this change in value.
There are 2 types of reference data type in C# language.
1) Predefined Types - such as Objects, String.
2) User defined Types - such as Classes, Interface.
Pointer Data Type
The pointer in C# language is a variable, it is also known as locator or indicator that
points to an address of a value.
Symbols used in pointer
Symbol Name Description
& (ampersand sign) Address operator Determine the address of a variable.
* (asterisk sign) Indirection operator Access the value of an address.
Declaring a pointer
The pointer in C# language can be declared using * (asterisk symbol).
1. int * a; //pointer to int
2. char * c; //pointer to char