Algorithm


  1. Implicit Conversion:

    • Automatically performed by the compiler when there is no risk of data loss.
  2. Explicit Conversion (Casting):

    • Programmer explicitly specifies the conversion using casting syntax.
  3. Convert Class Methods:

    • Convert class provides various methods like ToInt32, ToString, etc.
  4. Parse and TryParse Methods:

    • Parse converts a string representation to a specific type.
    • TryParse attempts the conversion and returns a Boolean indicating success.
  5. ToString Method:

    • Converts a value to its string representation.
  6. Math Functions for Type Conversion:

    • Using math functions for converting between numeric types.
  7. Enum Type Conversion:

    • Enums can be converted to and from their underlying integral types.

 

Code Examples

#1 Implicit Type Conversion in C# programming

Code - C# Programming

using System;

namespace MyApplication {
  class Program {
    static void Main(string[] args) {
      int numInt = 500;

      // get type of numInt
      Type n = numInt.GetType();

      // Implicit Conversion
      double numDouble = numInt;

      // get type of numDouble
      Type n1 = numDouble.GetType();

      // Value before conversion
      Console.WriteLine("numInt value: "+numInt);
      Console.WriteLine("numInt Type: " + n);

      // Value after conversion
      Console.WriteLine("numDouble value: "+numDouble);
      Console.WriteLine("numDouble Type: " + n1);
      Console.ReadLine();
    }
  }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
numInt value: 500
numInt Type: System.Int32,br> numDouble value: 500
numDouble Type: System.Double

#2 Explicit Type Conversion in C#

Code - C# Programming

using System;

namespace MyApplication {
  class Program {
    static void Main(string[] args) {

      double numDouble = 1.23;

      // Explicit casting
      int numInt = (int) numDouble;  

      // Value before conversion
      Console.WriteLine("Original double Value: "+numDouble);

      // Value before conversion
      Console.WriteLine("Converted int Value: "+numInt);
      Console.ReadLine();
    }
  }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Original double value: 1.23
Converted int value: 1

#3 Type Conversion using Parse() in C#

Code - C# Programming

using System;

namespace Conversion {
  class Program {
   
    static void Main(string[] args) {

      string n = "100";

      // converting string to int type
      int a = int.Parse(n);
      Console.WriteLine("Original string value: "+n);
      Console.WriteLine("Converted int value: "+a);
      Console.ReadLine();
    }
  }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Original string value: 100
Converted int value: 100

#4 Convert int to String and Double in C#

Code - C# Programming

using System;

using System;
namespace Conversion {
  class Program {
    static void Main(string[] args) {

      // create int variable
      int num = 100;
      Console.WriteLine("int value: " + num);

      // convert int to string
      string str = Convert.ToString(num);
      Console.WriteLine("string value: " + str);

      // convert int to Double
      Double doubleNum = Convert.ToDouble(num);
      Console.WriteLine("Double value: " + doubleNum);

      Console.ReadLine();
    }
  }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
int value: 100
string value: 100
Double value: 100

#5 Convert string to Double and vice-versa in C#

Code - C# Programming

using System;

namespace Conversion {
  class Program {
    static void Main(string[] args) {

      // create string variable
      string str = "99.99";
      Console.WriteLine("Original string value: " + str);

      // convert string to double
      Double newDouble = Convert.ToDouble(str);
      Console.WriteLine("Converted Double value: " + newDouble);

      // create double variable
      double num = 88.9;
      Console.WriteLine("Original double value: " + num);

      // converting double to string 
      string newString = Convert.ToString(num);
      Console.WriteLine("Converted string value: " + newString);

      Console.ReadLine();
    }
  }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Original string value: 99.99
Converted Double value: 99.99
Original double value: 88.9
Converted string value: 88.9

#6 Convert int to Boolean in C#

Code - C# Programming

using System;

namespace Conversion {
  class Program {
    static void Main(string[] args) {

      // create int variables
      int num1 = 0;
      int num2 = 1;

      // convert int to Boolean
      Boolean bool1 = Convert.ToBoolean(num1);
      Boolean bool2 = Convert.ToBoolean(num2);

      Console.WriteLine("Boolean value of 0 is: " + bool1);
      Console.WriteLine("Boolean value of 1 is: " + bool2);

      Console.ReadLine();
    }
  }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Boolean value of 0 is: False
Boolean value of 1 is: True
Advertisements

Demonstration


C# Program Type Conversion