Repository navigation
EN CSharp Classes
Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어
A class describes the state and behavior of an object. new creates an instance, the constructor sets the initial state, fields store data, properties control access, methods perform actions. this denotes the current instance.
Save this example separately as Example.cs. Run it independently of the other examples on this page.
Example.cs:
// PROJECT-EXAMPLE
var player = new Player();
System.Console.WriteLine(player.Name);
class Player { public string Name = "Archer"; }Result:
Archer
sealed class Report
{
public string Name { get; }
public int Count { get; private set; }
public Report(string name) => Name = name;
public void Add() => Count++;
public override string ToString() => $"{Name}: {Count}";
}
var report = new Report("Attempts");
report.Add();
report.Add();
UO.Print(report.ToString());In a regular .cs project, it is convenient to separate types into files. .csx can declare its own types, but the code inside the class does not automatically obtain the UO object: pass it API or the desired delegate explicitly.
sealed class MessageWriter
{
private readonly Action<string> write;
public MessageWriter(Action<string> write) => this.write = write;
public void Send(string text) => write(text);
}
var writer = new MessageWriter(text => UO.Print(text));
writer.Send("A class uses the current session API");Official documentation: Classes, Objects, and Constructors - Microsoft Learn.
ClassicUO — Bad Newbie & C# Scripts IDE