Predefined Data Types

There are two categories of data types in C#.

1. Value Type: Directly store the value. Stored in a memory known as the stack. Predefined value types are integer types, floating-point types. decimal type, boolean type, character type.

2. Reference Type: Store the reference of a value. Stored in a memory known as the managed heap. Predefined reference types supported in C# are object (CTS Type – System.Object) and string (CTS Type – System.String).

Common Type System (CTS) Types is the standard that specifies how type definition and specific value of Types are represented in computer memory. It is intended to allow program to be written in different programming language to easily share information. a Type can be described as a definition of a set of values (for example, “all integers between 0 and 10”), and the allowable operations on those values (for example, addition and subtraction).

Boxing and unboxing

Boxing converting value types to reference types is known as boxing.Boxing is of two types: Implicit and Explicit.

Example :-
Int32 X = 10;
Object O = X; //Implicit boxing
//Object O = (Object) X; //Explicit boxing
Console.WriteLine("The Object O = {0}", O); //prints out 10

Unboxing mean unbox a reference type back to value type. Unboxing require a explicit cast.

Int32 X = 10;
Object O = X; // Implicit Boxing
X = (Int) O; //Explicit Unboxing

Truth of string type: Despite the style of assignment, string is a reference type. A string object is allocated on the heap, not the stack, and when we assign one string variable to the another string, we get two references to the same string in memory. However, with strings there are some difference from the usual behavior for reference types. For example: strings are immutable. You make changes to one of these strings, this will create an entirely new string object, leaving the other string unchanged.

using System;
class StringExample
   string s1 = "a string";
   string s2 = s1;
   Console.WriteLine("s1 is " + s1);
   Console.WriteLine("s2 is " + s2);
   s1 = "another string";
   Console.WriteLine("s1 is now " + s1);
   Console.WriteLine("s2 is now " + s2);
   return 0;
Output: s1 is a string
        s2 is a string
        s1 is now another string
        s2 is now a string

About Rajan
I am Rajan Garg working as a Senior Software Engineer in a company. I am working on the Microsoft technologies.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: