Skip to content

EN CSharp Conversions

Codex edited this page Oct 11, 2026 · 2 revisions

Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어

Transformations and parsing of values

Manual C# · Examples UO

Implicit conversion is performed without casting if the types allow it. The explicit is written (Type)value. Casting a number does not replace parsing a string. TryParse allows you to check the text without exception, and checked detects integer overflow.

Simple example (Example.cs)

Save this example separately as Example.cs. Run it independently of the other examples on this page.

Example.cs:

// PROJECT-EXAMPLE
if (int.TryParse("12", out int count))
    System.Console.WriteLine(count + 1);

Result:

13
int number = 42;
double wide = number;
int whole = (int)12.9;
if (int.TryParse("350", out int delay))
{
    await WaitAsync(delay);
}
UO.Print($"{wide}, {whole}");

Use is for type checking and as for reference type conversion, with the result being null if there is a mismatch. Boxing boxes the value type into object, unboxing requires a suitable source type.

object value = "target";
if (value is string name)
{
    UO.Print(name.ToUpperInvariant());
}
long limit = checked((long)int.MaxValue + 1);
UO.Print(limit.ToString());

Official documentation: Value Conversions and Parsing - Microsoft Learn.

Clone this wiki locally