Skip to content

EN CSharp Variables

Codex edited this page Oct 11, 2026 · 2 revisions

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

Variables, Constants and Scope

Manual C# · Examples UO

A variable must be declared before being used and given a value before being read. The local name is visible within its block; parameter - inside the method. const specifies a compile-time value, readonly limits the field assignment to a constructor or declaration.

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
int count = 2;
count += 3;
const int Limit = 10;
System.Console.WriteLine($"{count}/{Limit}");

Result:

5/10
const int MaximumAttempts = 3;
int completed = 0;
for (int attempt = 0; attempt < MaximumAttempts; attempt++)
{
    completed++;
}
UO.Print($"Completed: {completed}");

A static field belongs to its type and is shared by its instances within one execution. Each run of .csx, .cs or .csproj isolates user-defined types and their static fields; caching compiled code does not preserve those fields across runs. Static data in shared .NET and host libraries remains shared: keep character-specific data out of it.

sealed class Options
{
    public readonly int Delay;
    public Options(int delay) => Delay = delay;
}

var options = new Options(100);
await WaitAsync(options.Delay);

Official Documentation: Variables, Constants, and Scope - Microsoft Learn.

Clone this wiki locally