C# Variables and Data Types

 General audience classification icon  General audience classification icon
The C# [1] variables are categorized into the following types:

  • value types,
  • reference types,
  • pointer types.

Value Type Variables

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):

Table 1: C# 2010 Value Definitions
Type Represents Range Default Value
bool Boolean value True or False False
byte 8-bit unsigned integer 0 to 255 0
char 16-bit Unicode character U +0000 to U +ffff '\0'
decimal 128-bit precise decimal values with 28-29 significant digits (–7.9 × 10E28 to 7.9 × 10E28) / 10E0 to 28 0.0M
double 64-bit double-precision floating-point type (+/–)5.0 × 10E–324 to (+/–)1.7 × 10E308 0.0D
float 32-bit single-precision floating-point type –3.4 × 10E38 to + 3.4 × 10E38 0.0F
int 32-bit signed integer type –2 147 483 648 to 2 147 483 647 0
long 64-bit signed integer type –9 223 372 036 854 775 808 to 9 223 372 036 854 775 807 0L
sbyte 8-bit signed integer type –128 to 127 0
short 16-bit signed integer type –32 768 to 32 767 0
uint 32-bit unsigned integer type 0 to 4 294 967 295 0
ulong 64-bit unsigned integer type 0 to 18 446 744 073 709 551 615 0
ushort 16-bit unsigned integer type 0 to 65 535 0

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;

C# Variables

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:

Table 2: C# 2010 Variables
Type Example
Integral types sbyte, byte, short, ushort, int, uint, long, ulong, and char
Floating point types float and double
Decimal types decimal
Boolean types true or false values, as assigned
Nullable types Nullable data types

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
en/iot-open/getting_familiar_with_your_hardware_rtu_itmo_sut/raspberrypi_rpi/data_types_and_variable_winiot.txt · Last modified: 2023/11/23 13:51 by pczekalski
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0