Junior — Middle
What are the methods of data transformation between types in programming?
sobes.tech AI
Answer from AI
In C#, there are several ways to convert data between types:
- Implicit conversion — performed automatically by the compiler when the conversion is safe (e.g., from int to long).
- Explicit conversion (or casting) — requires explicit indication, for example
(int)doubleValue. - Convert methods — the
Convertclass provides methods for converting between primitive types, for exampleConvert.ToInt32(string). - Parse and TryParse methods — for converting strings to numeric types, for example
int.Parse("123")orint.TryParse("123", out int result). - as operator — for safe reference type casting, returns null if it fails.
Example of explicit conversion:
double d = 9.78;
int i = (int)d; // i will be 9