codescracker


c#

C# Type Conversion



« Previous Tutorial Next Tutorial »


Type conversion means, conversion of one data type to another data type. You can also call type conversion as type casting. Type conversion is of these two types in C# programming:

Explicit Type Conversion in C#

Explicit type conversion in C#, are done explicitly by users using the pre-defined functions. Explicit type conversion in C#, requires a cast operator.

Here is an example program, illustrating explicit type conversion in C# programming.

/* C# Type Conversion - Example Program */
		
using System;
namespace ExplicitTypeApplication 
{
   class ExplicitConv 
   {
      static void Main(string[] args) 
      {
         double dbl = 4323.74; 
         int num;
         num = (int)dbl;	// casted double to an int
         Console.WriteLine(i);
         Console.ReadKey();
      }
   }
}

It will display the following output after compile and executed the above C# program:

4323

Implicit Type Conversion in C#

Implicit type conversion in C#, are performed in a type-safe manner. For instance, conversion from smaller to larger integral types.

Methods used in Type Conversion in C#

Here are built-in methods listed, used in type conversion in C# programming:

Method Meaning
ToChar This is used to convert a type to a single Unicode character, where possible
ToBoolean This is used to convert a type to a Boolean value, where possible
ToByte This is used to convert a type to a byte
ToDouble This is used to convert a type to a double type
ToDateTime This is used to convert a type (integer/string type) to date-time structures
ToDecimal This is used to convert a floating point or integer type to a decimal type
ToInt64 This is used to convert a type to a 64-bit integer
ToInt16 This is used to convert a type to a 16-bit integer
ToInt32 This is used to convert a type to a 32-bit integer
ToSbyte This is used to convert a type to a signed byte type
ToString This is used to convert a type to a string
ToUInt64 This is used to convert a type to an unsigned big integer
ToSingle This is used to convert a type to a small floating point number
ToType This is used to convert a type to a specified type
ToUInt32 This is used to convert a type to an unsigned long type
ToUInt16 This is used to convert a type to an unsigned int type

Here is an example program, illustrates type conversion using built-in methods in C# programming. This C# program converts some values type to string type:

/* C# Type Conversion - Example Program */
		
using System;
namespace ConversionMethodsApplication 
{
	class StringConv
	{
		static void Main(string[] args)
		{
			int num = 95;
			float flt = 93.005f;
			double dbl = 9345.7652;
			bool bl = true;
			Console.WriteLine(num.ToString());
			Console.WriteLine(flt.ToString());
			Console.WriteLine(dbl.ToString());
			Console.WriteLine(bl.ToString());
			Console.ReadKey();
		}
	}
}

When the above code will compile and executed, it would produce the following output:

95
93.005
9345.7652
True

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test