Skip to content

EN CSharp Structure

Codex edited this page Oct 11, 2026 · 2 revisions

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

Program and script structure

Manual C# · Examples UO

A C#-program consists of type declarations, methods, expressions and operators. using connects the namespace; namespace organizes types; the class stores data and behavior. The method code is located inside { }, regular statements end with ;.

Simple example (Example.csx)

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

Example.csx:

UO.Print("Ready");
await WaitAsync(10);
UO.Print("Done");

Result:

Ready
Done

Simple example (Program.cs + Minimal.csproj)

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

Program.cs:

// PROJECT-EXAMPLE
System.Console.WriteLine("Ready");

Minimal.csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
    <EnableDefaultCompileItems>false</EnableDefaultCompileItems>
  </PropertyGroup>
  <ItemGroup><Compile Include="Program.cs" /></ItemGroup>
</Project>
dotnet run --project Minimal.csproj

Result (Minimal.csproj):

Ready

Simple scenario .csx

The embedded script receives UO, Args, Cancellation and WaitAsync. The statements are executed from top to bottom. Class Program is not needed.

string message = "Script started";
UO.Print(message);
await WaitAsync(100);
UO.Print($"X={UO.GetX()}, Y={UO.GetY()}");

Script with custom functions

Declaring a function does not execute it. Calling ReportPosition() executes its body.

void ReportPosition()
{
    int x = UO.GetX();
    int y = UO.GetY();
    UO.Print($"Position: {x}, {y}");
}

ReportPosition();
await WaitAsync(100);
ReportPosition();

Normal program .cs

In a typical project, the entry point is Main or top-level code. Namespaces, multiple files and custom libraries are designed as a standard C#-project.

// PROJECT-EXAMPLE
using System;

namespace UserTools;

internal static class Program
{
    private static int Main(string[] args)
    {
        Console.WriteLine(args.Length > 0 ? args[0] : "Ready");
        return 0;
    }
}

Project file

.csproj describes the target platform, sources, dependencies, and compilation options. .cs stores the code. .csx denotes a script for a scripted run. Changing the extension does not automatically change the structure or entry point of the program.

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
</Project>

Typical structure: MyScript.csproj, Program.cs, Models/, Services/. In a small scenario, one .csx is enough. In the gaming class, API is passed explicitly through a parameter or field; an arbitrary regular project does not receive a global UO by itself.

Official documentation: Program and Script Structure - Microsoft Learn.

In ordinary .cs or .csproj code, Main or top-level statements take precedence even when the assembly also contains ICSharpScript. Without an executable entry point, provide one public, non-abstract ICSharpScript class with a public parameterless constructor. Ordinary code accesses the game API through a supplied context or CSharpScriptContext.Current; it does not receive an undeclared global UO variable.

Clone this wiki locally