Tutorials Navigation
C# - Type Conversion
Tutorial Name: C# - Type Conversion
Category: PC Tutorials
Submitted By: Nissan
Date Added:
Comments: 2
Views: 646
Related Forum: PC Building Forum
Share:
C# - Type Conversion
Type conversion is converting one type of data to another type. It is also known as Type Casting. In C#, type casting has two forms:
Implicit type conversion - These conversions are performed by C# in a type-safe manner. For example, are conversions from smaller to larger integral types and conversions from derived classes to base classes.
Explicit type conversion - These conversions are done explicitly by users using the pre-defined functions. Explicit conversions require a cast operator.
The following example shows an explicit type conversion:
using System;
namespace TypeConversionApplication
{
class ExplicitConversion
{
static void Main(string[] args)
{
double d = 5673.74;
int i;
// cast double to int.
i = (int)d;
Console.WriteLine(i);
Console.ReadKey();
}
}
}
When the above code is compiled and executed, it produces the following result:
5673
C# Type Conversion Methods
C# provides the following built-in type conversion methods:
[ Register or Signin to view external links. ]
The following example converts various value types to string type:
using System;
namespace TypeConversionApplication
{
class StringConversion
{
static void Main(string[] args)
{
int i = 75;
float f = 53.005f;
double d = 2345.7652;
bool b = true;
Console.WriteLine(i.ToString());
Console.WriteLine(f.ToString());
Console.WriteLine(d.ToString());
Console.WriteLine(b.ToString());
Console.ReadKey();
}
}
}
When the above code is compiled and executed, it produces the following result:
75
53.005
2345.7652
True
Ratings
Comments
Related Tutorials
- 01. Emulating Xbox 360 on PC for Running COD4 With Mods(2,116)
- 02. How to: Matrix Numbers | Batch File(1,589)
- 03. How to Password Protect Files on Windows(765)
- 04. How to play Socom 2/3/ and Combined Assault on PC(5,178)
- 05. Modern Warfare 2 Vac Ban Bypass Tutorial(5,044)
- 06. How to embed an image on TheTechGame(2,779)
- 07. [PC] NIOH 2 OTHER USER SAVE RESIGN(12,337)
- 08. Host bot lobbies! Full Tutorial!(9,576)
- 09. Unban yourself [Plutonium BO2](13,467)
- 10. Fall Guys - How to Change Your Name Color on Fall Guys(8,025)
- 11. Best Crosshair Settings for Valorant(6,168)
- 12. Othercide The Surgeon Boss Guide(2,228)
- 13. Othercide Remembrances Unlock Guide(3,928)
- 14. Othercide Beginners Tips and Tricks(2,446)
- 15. How to Fix Grounded Crashes, Loading Time, Low FPS and Other(4,364)
"C# - Type Conversion" :: Login/Create an Account :: 2 comments