The C# [1] variables are categorized into the following types:
Value-type variables can assign a value directly. The class System.ValueType
defines them.
The value types directly contain data. Value types may be: int
, char
and float
, storing numbers, strings or floating point values. When an int type is declared, the system allocates memory to store its value.
The available value types listed in C# are presented as follows (table 1):
Object Type
The Object Type is an alias for the System.Object
class. It is the ultimate base class for all data types in the C# Common Type System (CTS). The object types can be assigned with values of any other types, value types, reference types, predefined or user-defined types. Before assigning values, the type conversion is needed.
When a value type is converted to an object type, it is called boxing; when it is converted to a value type, it is called unboxing.
object obj; obj = 100; //This is boxing
Dynamic Type
The data type variable can store any value. But this type of checking takes place at run-time.
The syntax for declaring a dynamic type is:
dynamic <variable_name> = value;
For example,
dynamic d = 20;
Dynamic types are similar to object types. That type checking for object type variables takes place at compile time. For the dynamic type variables, checking takes place at run time.
String Type
The string type allows assigning any string values to a variable. The string type is an alias for the System.String
class derived from the object type. The string type value can be assigned using string literals in two forms: quoted and @quoted.
For example,
string str = "Tutorials Point";
A @quoted string literal looks as follows:
@"Tutorials Point";
The user-defined reference types are class
, interface
, or delegate
.
Reference Type
The reference types don't contain the actual data stored in a variable. They contain a reference to the variables.
Using multiple variables, the reference types can refer to a memory location. If the variable changes the data in the memory location, the other variable automatically reflects this change in value. Built-in reference example types are object
, dynamic
, and string
.
Pointer Type
Pointer-type variables store the memory address, which is another type. Pointers in C# are similar to pointers in C or C++.
The syntax for declaring a pointer type is:
type* identifier;
For example,
char* cptr; int* iptr;
Each variable in C# has a specific type, which determines the size and layout of the variable's memory.
The basic value types in C# can be categorised as follows:
Variable Definitions
Variable syntax definition in C# is:
<data_type> <variable_list>;
data_type
must be a valid C# data type like char
, int
, float
, double
, or any user-defined data type. variable_list
may consist of one or more identifier names separated by commas.
Examples of valid variable definitions are shown below:
int i, j, k; char c, ch; float f, salary; double d;
The variable can be initialized immediately during definition time:
int i = 100;
Variables Initialization
Variables are initialized with an equal sign followed by a constant expression. The general initialization form looks:
variable_name = value;
Variables can be initialized during their declaration. The initializer consists of an equal sign followed by a constant expression as:
<data_type> <variable_name> = value;
Some examples are:
int d = 3, f = 5; /* Initializing d and f */ byte z = 22; /* Initializes z */ double pi = 3.14159; /* Declares an approximation of PI */ char x = 'x'; /* The variable x has the value 'x' */
It is essential to initialize variables properly; otherwise, sometimes, it may produce unexpected results.
The following example uses various types of variables:
using System; namespace VariableDefinition { class Program { static void Main(string[] args) { short a; int b ; double c; /* Actual initialization */ a = 10; b = 20; c = a + b; Console.WriteLine("a = {0}, b = {1}, c = {2}", a, b, c); Console.ReadLine(); } } }
Output:
a = 10, b = 20, c = 30