Home » .Net

Type conversion methods in C#

This post contains list of most popular methods of Convert class that are used to type conversion in C#, here you will find description and examples on each methods.

In C#.Net, Convert class is used for type conversion, which has many methods for type conversions that can be used to convert one type to another.

These are the few conversion methods (pre-defined methods):

  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 method is used to convert string to 8-bit unsigned integer.

2) Convert.ToSByte()

This method is used to convert string to 8-bit signed integer.

3) Convert.ToInt16()

This method is used to convert string to 16-bit signed integer.

4) Convert.ToInt32()

This method is used to convert string to 32-bit signed integer.

5) Convert.ToInt64()

This method is used to convert string to 64-bit signed integer.

6) Convert.ToUInt16()

This method is used to convert string to 16-bit unsigned integer.

7) Convert.ToUInt32()

This method is used to convert string to 32-bit unsigned integer.

8) Convert.ToUInt64()

This method is used to convert string to 64-bit unsigned integer.

9) Convert.ToChar()

This method is used to convert string to single character.

10) Convert.ToString()

This method is used to convert number to string.

11) Convert.Single()

This method is used to convert string to 32-bit float.

12) Convert.Double()

This method is used to convert string to 64-bit double.

13) Convert.Decimal()

This method is used to convert string to 128-bit decimal.

Consider 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

VAL :A
VAL :255
VAL :127
VAL :-12345 
VAL :-123456
VAL :-123456789 
VAL :12345
VAL :123456 
VAL :123456789
VAL :3.14 
VAL :3.14 
VAL :3.14 
125 
Press any key to continue . . .





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.