Working With Data Types

First thing a programmer looks for is what kind of data types a programming languages has and how to use them. In this part, I will cover C# data types and how to use them in a program. 

Basic Data Types

Most of the data type in c# are taken from C and C++. This tables lists data types, their description, and a sample example. 

Data Type Description Example
object  The base type of all types object obj = null;
string  String type - sequence of Unicode characters string str = "Mahesh";
sbyte 8-bit signed integral type sbyte val = 12;
short  16-bit signed integral type short val = 12;
int 32-bit signed integral type int val = 12;
long 64-bit signed integral type long val1 = 12;
long val2 = 34L;
bool Boolean type; a bool value is either true or false bool val1 = true;
bool val2 = false;
char Character type; a char value is a Unicode character char val = 'h';
byte  8-bit unsigned integral type byte val1 = 12;
byte val2 = 34U;
ushort 16-bit unsigned integral type ushort val1 = 12;
ushort val2 = 34U;
uint 32-bit unsigned integral type uint val1 = 12;
uint val2 = 34U;
ulong  64-bit unsigned integral type ulong val1 = 12;
ulong val2 = 34U;
ulong val3 = 56L;
ulong val4 =78UL;
float  Single-precision floating point type float val = 1.23F;
double  Double-precision floating point type double val1 = 1.23;
double val2 = 4.56D;

decimal

 Precise decimal type with 28 significant digits decimal val= 1.23M;

Types in C#

C# supports two kinds of types: value types and reference types.

Types  Description
Value Types  Includes simple data types such as int, char, bool, enums
Reference Types Includes object, class, interface, delegate, and array types

Value Types-  Value type objects direct contain the actual data in a variables. With value types, the variables each have their own copy of the data, and it is not possible for operations on one to affect the other.

int i = 10;

Reference Types- Reference type variables stores the reference of the actual data. With reference types, it is possible for two variables to reference the same object, and thus possible for operations on one variable to affect the object referenced by the other variable.

MyClass cls1 = new MyClass();

Data Type Conversions

C# supports two types of conversions. Implicit conversions and explicit conversions. 

Implicit conversions are direct conversion. For example:

int iVal = 34;
long lVal = intValue;

Explicit conversions includes type casting. conversion. For example:

long lVal = 123456;
int iVal = (int) lVal;

Up Next
    Ebook Download
    View all
    Learn
    View all