Showing posts with label Type Casting. Show all posts
Showing posts with label Type Casting. Show all posts

Sunday, May 1, 2016

Float to int Datatype Conversion

using System;
namespace TypeConversionApplication 
{
 class ExplicitConversion 
 {
 static void Main(string[] args) 
 {
 float a = 70.0f;
 int b = Convert.ToInt32(a);
 Console.WriteLine(b);
 Console.ReadKey();
 }
 }
}
Output : 70

String to int Datatype Conversion

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

Thursday, April 28, 2016

Decimal to int Datatype Conversion

using System;
namespace TypeConversionApplication 
{
 class ExplicitConversion 
 {
 static void Main(string[] args) 
 {
 decimal a = 10.0M;
 int b = Convert.ToInt32(a);
 Console.WriteLine(b);
 Console.ReadKey();
 }
 }
}
Output : 10

Decimal to Float Datatype Conversion

using System;
namespace TypeConversionApplication 
{
 class ExplicitConversion 
 {
 static void Main(string[] args) 
 {
 decimal a = 20.0M;
 float b = Convert.ToSingle(a);
 Console.WriteLine(b);
 Console.ReadKey();
 }
 }
}
Output : 20

Wednesday, April 27, 2016

int to Float Datatype Conversion

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

int to String Datatype Conversion

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

Tuesday, April 26, 2016

int to Decimal Datatype Conversion

using System;
namespace TypeConversionApplication 
{
 class ExplicitConversion 
 {
 static void Main(string[] args) 
 {
 int a = 20;
 decimal b = Convert.ToDecimal(a);
 Console.WriteLine(b);
 Console.ReadKey();
 }
 }
}
Output : 20
 int to Double Datatype
using System;
namespace TypeConversionApplication 
{
 class ExplicitConversion 
 {
 static void Main(string[] args) 
 {
 int a = 30;
 double b = Convert.ToDouble(a);
 Console.WriteLine(b);
 Console.ReadKey();
 }
 }
}
Output : 30

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