Data Types in C# 



C# supports two types of data, viz. Value types and Reference types. Value and Reference types differ into two characteristics: where they are stored in memory, and how they behave in the context of the assignment statements. Value types are stored on the stack, and assignment statements between two values variables result in two separate, but identical, copies of the value in memory. Reference types are stored on the heap, and an assignment statement between two reference variables results in two references to a single value at one location in memory. Value types include signed and unsigned.

Signed

Data Types    size
int 4 bytes
sbyte 1 byte
short 2 bytes
long 8 bytes
float 4 bytes
double 8 bytes

Unsigned 

Data Typpes Size
byte 1 bytes
ushort 2 bytes
uint 4 bytes
ulong 8 bytes

Others

Data Types Size
char 1 byte
bool 1 byte
decimal 16 bytes
emum

---

struct

---


Reference types include class, interface, delegate, and array types.There are no long doubles and unions in C#. Rules for building identifiers are same as in C and structures are made more powerful. All the data types are of fixed sizes and will be the same size on any system. This removes some of the problems traditionally associated with porting C & C++ code between architectures, and is pretty vital for writing distributed applications.

The following is a simple program in which we have initialized an int, a float, a char and a bool variable. Next we displayed the values and types of the variables.

using System ;
class myclass
{

static void Main ( string [ ] args )
{

int i = 20 ;
float f = 3.14f ;
char ch = 'p' ;
bool b = true ;
Console.WriteLine ( "Value of i: {0}, Type is {1} ", i, typeof ( int ) ) ; 
Console.WriteLine ( "Value of f: {0}, Type is {1} ", f, typeof ( float ) ) ; 
Console.WriteLine ( "Value of ch: {0}, Type is {1} ", ch, typeof ( char ) ) ; 
Console.WriteLine ( "Value of b: {0}, Type is {1} ", b, typeof ( bool ) ) ; 

}

}

The typeof operator is used to obtain the System.Type object for a type.

 

All the value types are nothing but simple types. Simple types are nothing but predefined structures. The simple types are identified through reserved keywords, but these reserved words are simply aliases for the predefined structures in the System namespace. For eg int (keyword) is nothing but an alias for System.Int32 structure.

C# also supports user defined structures like any other language does. Structures are prefaced with the keyword struct.

Because a simple type aliases a structure, every simple type has members. For example, int has the members declared in System.Int32 and the members inherited from System.Object, because this structure inherits from System.Object, and the following statements are permitted.

 

int n = 10 ;
string s = n.ToString ( ) ;
string s = 10.ToString ( ) ;

 

Here, ToString( ) function of object class gets called. We cannot inherit from these structures and they are sealed.

For that matter everything is derived from the object class

 


Everything is an Object 


Java made an attempt to represent everything as an object. However, problems were faced when primitive data types appeared in the scene. Java separated primitive data types from objects. Java treats primitive data types as variables and they do not have object-like behavior. Java provides wrapper classes to convert the primitive data types into objects.
 

In C++ primitive data types were simple variables not objects. C++ did not provide any way to convert the variables into objects.
 

C# uses a third way. In C#, the primitive types are treated as objects as and when necessary.
 

For using the value type variable as an object we are not required to use wrapper classes. C#’s type system is unified such that value of any type can be treated as an object. We can convert any value type to reference type and vice versa using boxing and unboxing. This is discussed later.
 

Every type in C# directly or indirectly derives from the object class type, and object is the ultimate base class of all types.

 


Default Constructors


 

All value types implicitly declare a public parameterless constructor called the default constructor. The default constructor returns a zero-initialized instance known as the default value for the value type.
 

Following are the default values for simple type, and struct-type
 

Type  Default Value
 sbyte, byte, short, ushort, int, long, and ulong   0
 char   '\x0000'
 float   0.0f
 double   0.0d
 decimal   0.0m
 bool   False
 struct type

Value produced by setting all value types to their default values and reference types to null
 

Like any other constructor, the default constructor of a value type is invoked using the new operator. Because every value type implicitly has a public parameterless constructor, it is not possible for a struct type to contain an explicit declaration of a parameterless constructor. A struct type is however permitted to declare parameterized constructors.