Hello all, In this article I have tried to cover topics like data type , their conversion, value & reference type.The concepts of  Boxing and Unboxing is also discussed in this article.

 C# is a strongly typed language; therefore every variable and object must have a declared type.

 Proper utilization of correct data types allows developers to make the most of the language

 

Data type in C#

 

There are two types of data type in C#:

 

1. Primitive (Predefine)

2. Non-Primitive (User Defined)

 

Primitive data types are further divided as:

 

byte

short

int

float

double

long

char

bool

datetime

string

object

etc

 

Non-primitive data types are further divided as:

 

class

struct

enum

interface

delegate

array

 

In .NET Microsoft has divided data types in two parts:

 

1.  Value Type (Fixed in size)

2.  Reference Type (Not fixed in size)

 

In application context, value types are stored in stack but reference types are stored in managed heap.  

Value Type

  • Value types are fixed in size.
  • Value types are made in system stack.
  • Actual values of data are stored in stack.
  • If you assign a value of a variable to another it will create two copies.

All primitive data type except string and object are example of value types.

For ex: basic type like int , float, bool etc

 Object is a super type. It can store any type and any size of data. Object is called super type because it helps in inheritance.

 struct and enum are value type.

 

.  Reference Type 

  • Reference types are not fixed in size.
  • They are maintained in system managed heap but it also uses stack to store reference of heap.
  • Two primitive types (string and object) and non-primitive data types (class, interface & delegate) are examples of reference type.

CLR manages heap (large memory area). Heap address is accessed from stack. In reference type reference is used for processing using both managed heap and stack (operational entity).

 

Type Conversions

 

Conversion is based on type compatibility and data compatibility.

 There are two types of conversions:

 1.  Implicit Conversion

2.  Explicit Conversion

 

Implicit Conversion

 

In implicit conversion the compiler will make conversion for us without asking.

 

char -> int -> float is an example of data compatibility.

 

Complier checks for type compatibility at compilation.  

 

Practical demonstration of implicit conversion

 

using System;

 

namespace implicit_conversion

{

    class Program

    {

        static void Main(string[] args)

        {

            int num1 =20000;

            int num2 =50000;

            long total;

            // In this the int values are implicitly converted to long data type;

            //you need not to tell compiler to do the conversion, it automatically does.

            total = num1 + num2;

  Console.WriteLine(“Total is : ” + total);

            Console.ReadLine();

        }

    }

}

 

Explicit Conversion

 

In explicit conversion we specifically ask the compiler to convert the value into another data type.

 CLR checks for data compatibility at runtime.

 Explicit conversion is carried out using casts. When we cast one type to another, we deliberately force the compiler to make the transformation.

 You should never expect that the cast would give you best or correct result. Casts are potentially unsafe. Casting of big data type into small may lead to loosing of data.

 

Practical demonstration of explicit conversion

 

using System;

 

namespace explicit_conversion

{

    class Program

    {

        static void Main(string[] args)

        {

            int num = 65;

           

            char alpha;

            alpha = (char)num;

            // In this the int values are explicitly converted to char data type;

            //you have to tell compiler to do the conversion, it uses casting.

           

            Console.WriteLine(“alphabet is: ” + alpha);

            Console.ReadLine();

        }

    }

}

 

Note: You can also use Explicit Cast Operator () and unboxing for explicit conversion.

 

Microsoft .NET provides three ways of type conversion:

 

1.  Parsing

2.  Convert Class

3.  Explicit Cast Operator ()

 

Parsing

 

Parsing is used to convert string type data to primitive value type. For this we use parse methods with value types.

 

Practical demonstration of parsing

 

using System;

 

namespace parsing

{

    class Program

    {

        static void Main(string[] args)

        {

            //using parsing

 

            int number;

            float weight;

 

            Console.Write(“Enter any number : “);

            number = int.Parse(Console.ReadLine());

 

            Console.Write(“Enter your weight : “);

            weight = float.Parse(Console.ReadLine());

 

            Console.WriteLine(“You have entered : ” + number);

            Console.WriteLine(“You weight is : ” + weight);

 

            Console.ReadLine();

        }

    }

}

 

Convert Class

 

One primitive type to another primitive type.

 

This class contains different static methods like ToInt32(), ToInt16(), ToString(), ToDateTime() etc used in type conversion.

 

Practical demonstration of Convert class

 

using System;

 

namespace convert_conversion

{

    class Program

    {

        static void Main(string[] args)

        {

            // example of using convert class

 

            string num = “23”;

            int number = Convert.ToInt32(num);

 

            int age = 24;

            string vote = Convert.ToString(age);

 

            Console.WriteLine(“Your number is : ” + number);

            Console.WriteLine(“Your voting age is : ” + age);

 

            Console.ReadLine();

        }

    }

}

 

Explicit Cast Operator ()

 

In general this operator is used with non-primitive types to up level or down level casting. But it can also used with any type having type compatibility and data type compatibility.

 

using System;

 

namespace explicit_cast_conversion

{

    class Program

    {

        static void Main(string[] args)

        {

            int num1, num2;

            float avg;

            num1 = 10;

            num2 = 21;

            avg = (float)(num1 + num2) / 2;

 

            Console.WriteLine(“average is : ” + avg);

            Console.ReadLine();

        }

    }

}

 

Boxing and unboxing

 

Boxing and unboxing is an important concept in C# type system. With Boxing and unboxing one can link between value-types and reference-types by allowing any value of a value-type to be converted to and from type object.

 

Boxing  

  • Boxing is a mechanism in which value type is converted into reference type.
  • It is implicit conversion process in which object type (super type) is used.
  • In this process type and value both are stored in object type

Unboxing  

  • Unboxing is a mechanism in which reference type is converted into value.
  • It is explicit conversion process.

Program to show boxing and unboxing:

 

using System;

 

namespace boxing

{

    class Program

    {

        static void Main(string[] args)

        {

            int i = 10;

            object o = i;             // boxing

            int j = (int)o;          // unboxing

 

            Console.WriteLine(“value of o object : ” + o);

            Console.WriteLine(“Value of j : ” + j);

 

            Console.ReadLine();

        }

    }

}

 

Hope you have got some idea about data types, value type and reference type and boxing and unboxing. Your feedback and constructive contributions are welcome.  Please feel free to contact me for feedback or comments you may have about this article.

Reach me at my mail id- Prabhanshugupta03@gmail.com.

 

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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