DataTypes in C#

Posted by VIJI Sunday, July 5, 2009

What is DataType?

A data type in a programming language is a set of data with values having predefined characteristics. It describes how the value will be stored for the type and how it will be processed.

DataTypes in c#:

The C# programming language is a strongly typed language. All variables must be declared with a valid data type.

Types of DataTypes:

There are two types of datatype:

1. Built In data type

2. User Defined data type

Built In data type:

The built in data types are provided by the c# .NET frameword. The data types are reserved keywords and cannot be used as a variable name.

Built in data types supported in c# are:

byte - 0 to 255 characters

sbyte(short byte) - (-128 to 127)

short - 32,768 to 32,767

ushort(unsigned short) - 0 to 65,535

int -2,147,483,648 to 2,147,483,647

uint(unsigned int) 0 to 4,294,967,295

long -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

ulong 0 to 18,446,744,073,709,551,615

float - (-3.402823e38 to 3.402823e38 )

double - ( -1.79769313486232e308 to 1.79769313486232e308 )

decimal ( -79228162514264337593543950335 to 79228162514264337593543950335 )

char - A Unicode character.

string - A string of Unicode characters.

bool - True or False.

object - An object.

User Defined data type

The user defined data type, will be defined by the user.


Class or Interface

Enumerations - user defined integer type

The data types can further be classified into

a) Value Types

b) Reference Types

Value Types:

The value type variables store the actual data in the variable. Each variable maintains its own copy of data.


int x = 10;

double y = 10.12;

char z = 's';

The following value types are supported in c#:

* int
* uint
* bool
* char
* decimal
* float
* double
* sbyte
* byte
* char
* short
* ushort
* ulong
* long
* decimal

* structs
* enumerations

Reference Types

Reference types variables store the reference of the actual data.

The following reference types are supported in c#.

* string - represents a string of unicode characters.

* object - Represents a general purpose type. In C#, all predefined and user-defined types inherit from the object type or System.Object class.

* class

* interface

* delegate


string s = "TestString";

Although string is a reference type, the equality operators (== and !=) are defined to compare the values of string objects, not references.

public delegate void TestDelegate(string message);

The delegate keyword is used to declare a reference type that can be used to encapsulate a named or an anonymous method

Data Type Conversions:

C# supports two types of conversions.

1. Implicit conversions

2. Explicit conversions.

Implicit conversions are direct conversion. It includes method invoking and assignment.

For example:

int iVal = 34;
long lVal = intValue;

Explicit conversions includes type casting. By using a cast expression for conversion.

For example:

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

0 Responses to DataTypes in C#

Post a Comment