Type conversion methods in C#

This post contains a rundown of most well-known techniques for Convert class that are utilized to type change in C#, here you will discover portrayal and examples on every strategy.

In C#.Net, Convert class is utilized for type change, which has numerous techniques for type transformations that can be utilized to change over one sort to another.

These are a couple of transformation techniques (pre-characterized strategies):

  1. Convert.ToByte()
  2. Convert.ToSByte()
  3. Convert.ToInt16()
  4. Convert.ToInt32()
  5. Convert.ToInt64()
  6. Convert.ToUInt16()
  7. Convert.ToUInt32()
  8. Convert.ToUInt64()
  9. Convert.ToChar()
  10. Convert.ToString()
  11. Convert.ToSingle()
  12. Convert.ToDoble()
  13. Convert.ToDecimal()

1) Convert.ToByte()

This technique is utilized to change over string to 8-piece unsigned number.

2) Convert.ToSByte()

This strategy is utilized to change over string to 8-piece marked whole number.

3) Convert.ToInt16()

This technique is utilized to change over string to 16-piece marked whole number.

4) Convert.ToInt32()

This strategy is utilized to change over string to 32-piece marked whole number.

5) Convert.ToInt64()

This strategy is utilized to change over string to 64-piece marked whole number.

6) Convert.ToUInt16()

This technique is utilized to change over string to 16-piece unsigned whole number.

7) Convert.ToUInt32()

This technique is utilized to change over string to 32-piece unsigned whole number.

8) Convert.ToUInt64()

This strategy is utilized to change over string to 64-piece unsigned whole number.

9) Convert.ToChar()

This technique is utilized to change over string to single character.

10) Convert.ToString()

This strategy is utilized to change over number to string.

11) Convert.Single()

This strategy is utilized to change over string to 32-piece skim.

12) Convert.Double()

This strategy is utilized to change over string to 64-piece twofold.

13) Convert.Decimal()

This technique is utilized to change over string to 128-piece decimal.

Think about the program:

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("VAL :" + Convert.ToChar("A"));
            
            Console.WriteLine("VAL :" + Convert.ToByte("255"));
            Console.WriteLine("VAL :" + Convert.ToSByte("127"));

            Console.WriteLine("VAL :" + Convert.ToInt16 ("-12345"));
            Console.WriteLine("VAL :" + Convert.ToInt32 ("-123456"));
            Console.WriteLine("VAL :" + Convert.ToInt64 ("-123456789"));

            Console.WriteLine("VAL :" + Convert.ToUInt16("12345" ));
            Console.WriteLine("VAL :" + Convert.ToUInt32("123456" ));
            Console.WriteLine("VAL :" + Convert.ToUInt64("123456789" ));

            Console.WriteLine("VAL :" + Convert.ToSingle  ("3.14"));
            Console.WriteLine("VAL :" + Convert.ToDouble  ("3.14"));
            Console.WriteLine("VAL :" + Convert.ToDecimal ("3.14"));
            Console.WriteLine(Convert.ToString(125));
        }
    }
}

Output:

Leave a Comment

error: Alert: Content is protected!!