Showing posts with label c sharp. Show all posts
Showing posts with label c sharp. Show all posts

Tuesday, May 3, 2016

UnBoxing

The unboxing is an explicit conversion from the type object to a value or from an interface type to a value that implements interfaces. The unboxing consists of:
  • Checking the object instance to make sure that it is a boxed value of the given value type.
  • Copy the value from the instance variable into the value type variable.
Let us have a look on the example:
using System;

using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
  
namespace ConsoleApplication1  
{  
    class unboxing  
    {  
        public static int a
        {  
            get;  
            set;  
        }  
        public static void Main ( )  
        {   
            int i = 10;   
            object j = i;   
   i = (int) j;       //statement for unboxing
Console.WriteLine ("Enter the value: ", a);  
            a= Convert.ToInt32 (Console.ReadLine ( ));  
  
            if (i <= a)  
            {  
                i += a;  
                Console.WriteLine ("\n The sum is: ", i);  
            }  
            else  
            {  
                Console.WriteLine ("\nWrong answer…");  
            }  
            Console.ReadLine ( );  
        }    
    }    
}

Monday, May 2, 2016

Boxing

Boxing is a process of converting a value type to the object type or to any other interface type which is implemented by this value type. When CLR boxes a value type it wraps the value inside a System.Object and stores it on the managed heap. Boxing is done implicitly. It is used to store the value types in the garbage collected heap. It allocates an object instance on the heap and copies the value into the new object. For example:
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
  
namespace ConsoleApplication1  
{  
    class boxing  
    {  
public static int a
        {  
            get;  
            set;  
        }  
        public static void Main ( )  
        {   
            int i = 10;   
            object j = i;   //statement for boxing
  
            Console.WriteLine ("Enter the value: ", a);  
            a= Convert.ToInt32 (Console.ReadLine ( ));  
  
            if (i <= a)  
            {  
                i += a;  
                Console.WriteLine ("\n The sum is: ", i);  
            }  
            else  
            {  
                Console.WriteLine ("\nWrong answer…");  
            }  
            Console.ReadLine ( );  
        }    
    }    
} 

Monday, April 25, 2016

int to Char Datatype Conversion

using System;
namespace TypeConversionApplication 
{
 class ExplicitConversion 
 {
 static void Main(string[] args) 
 {
 int a = 66;
 char b = Convert.ToChar(a);
 Console.WriteLine(b);
 Console.ReadKey();
 }
 }
}
Output :DB

Sunday, April 24, 2016

int to Boolean Datatype Conversion

The conversion of one data type into another data type is the Type Conversion or Type Casting. Let us look at some examples:

int to Boolean Datatype Conversion

using System;
namespace TypeConversionApplication 
{
 class ExplicitConversion 
 {
 static void Main(string[] args) 
 {
 int a = 0;
 bool b = Convert.ToBoolean(a);
 Console.WriteLine(b);
 Console.ReadKey();
 }
 }
} 

Output : False

Thursday, April 21, 2016

Explicit Conversion

In this conversion it requires cast operator. The casting is required when the information is lost or the conversion or the conversion is not succeeded. It converts integer values into higher or lower range, converting base class instance to derived class. Let us have a look on example:
using System;
namespace ConsoleApplication1
{
 class Explicit 
 {
 static void Main (string [] args) 
 {
 double d = 10.50; 
 int i;
 
 // casting double to int.
 i = (int) d;
 Console.WriteLine (i);
 Console.ReadKey ();
 }
 }
}
Output : 10

Wednesday, April 20, 2016

Type Casting

The type casting is the type conversion of one data type into another data type. In C# there are two types of type casting:

Implicit Conversion

In this conversion no special syntax is required because this conversion is type safe and no data will be lost. For example, Convert small to large integer types, converting from derived class to base class
using System;
class A
{
 public int a;
 public static implicit operator B (A m)
 {
 B w = new B ( );
 w.a = m.a * 2;
 return w;
 }
}

class B
{
 public int a;
 public static implicit operator A (B w)
 {
 A m = new A ( );
 m.a = w.a / 2;
 return m;
 }
}

class Program
{
 static void Main ( )
 {
 A m = new A ( );
 m.a = 10;
 Console.WriteLine (m.a);

// Implicit conversion from A to B.
 B w = m;
 Console.WriteLine (w.a);

// Implicit conversion from B to A.
 A m2 = w;
 Console.WriteLine (m2.a);
 }
}
Output:
10
20
10