Tutorials Navigation
C# - Type Conversion
Tutorial Name: C# - Type Conversion
Category: PC Tutorials
Submitted By: Nissan
Date Added:
Comments: 2
Views: 660
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(3,656)
- 02. How to: Matrix Numbers | Batch File(1,954)
- 03. How to Password Protect Files on Windows(865)
- 04. How to play Socom 2/3/ and Combined Assault on PC(6,879)
- 05. Modern Warfare 2 Vac Ban Bypass Tutorial(6,243)
- 06. How to embed an image on TheTechGame(3,140)
- 07. [PC] NIOH 2 OTHER USER SAVE RESIGN(13,088)
- 08. Host bot lobbies! Full Tutorial!(11,611)
- 09. Unban yourself [Plutonium BO2](14,308)
- 10. Fall Guys - How to Change Your Name Color on Fall Guys(8,410)
- 11. Best Crosshair Settings for Valorant(6,550)
- 12. Othercide The Surgeon Boss Guide(2,568)
- 13. Othercide Remembrances Unlock Guide(4,517)
- 14. Othercide Beginners Tips and Tricks(2,734)
- 15. How to Fix Grounded Crashes, Loading Time, Low FPS and Other(4,881)
"C# - Type Conversion" :: Login/Create an Account :: 2 comments