Skip to content

EN CSharp Directive Reference

Codex edited this page Oct 11, 2026 · 2 revisions

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

Connecting the assembly to .csx

Handbook C# · Practical Guide

The script directive adds a ready-made managed assembly for script compilation. The library must exist in the specified path.

Simple example (Example.csx)

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

Example.csx:

#r "Library.dll"
UO.Print(Demo.Number.Value.ToString());

Library.cs:

// PROJECT-EXAMPLE
namespace Demo; public static class Number { public static int Value => 7; }

Library.csproj:

<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><TargetFramework>net10.0</TargetFramework><EnableDefaultCompileItems>false</EnableDefaultCompileItems></PropertyGroup><ItemGroup><Compile Include="Library.cs" /></ItemGroup></Project>
dotnet build Library.csproj -o .

Result:

7

Syntax

#r "path/Library.dll"

First, save the library files listed and run a dotnet build for it .csproj. #r paths are given for Debug/net10.0. For NuGet or a set of dependencies, it is more convenient to immediately use the scenario .csproj with PackageReference.

Library of mathematical functions

#r "ApiLibrary/bin/Debug/net10.0/ApiLibrary.dll"
UO.Print(Demo.Tools.Double(4).ToString());

File ApiLibrary/ApiLibrary.csproj:

<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><TargetFramework>net10.0</TargetFramework></PropertyGroup></Project>

File ApiLibrary/Tools.cs:

namespace Demo; public static class Tools { public static int Double(int value) => value * 2; }

Library of own models

#r "Models/bin/Debug/net10.0/Models.dll"
Demo.Point point = new(2, 3);
UO.Print((point.X + point.Y).ToString());

File Models/Models.csproj:

<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><TargetFramework>net10.0</TargetFramework></PropertyGroup></Project>

File Models/Point.cs:

namespace Demo; public readonly record struct Point(int X, int Y);

Official source: Microsoft Learn.

Clone this wiki locally