Sobes.tech
Junior — Middle

Millised on andmetüüpide vahelise andmete teisendamise meetodid programmeerimises?

sobes.tech AI

Vastus AI-lt

In C# there are several ways to convert data between types:

  • Implicit conversion — automatically performed by the compiler when the conversion is safe (e.g., from int to long).
  • Explicit conversion (casting) — requires explicit indication, e.g., (int)doubleValue.
  • Convert methods — the Convert class provides methods for converting between basic types, e.g., Convert.ToInt32(string).
  • Parse and TryParse methods — for converting strings to numeric types, e.g., int.Parse("123") or int.TryParse("123", out int result).
  • as operator — for safe reference type conversion, returns null if it fails.

Example of explicit conversion:

double d = 9.78;
int i = (int)d; // i will be 9