Repository navigation
EN CSharp Inheritance Interfaces
Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어
A class has one base class and can implement multiple interfaces. virtual allows override, override implements it, abstract requires implementation by the heir, sealed restricts inheritance. base refers to the underlying implementation; new hides the member and does not replace override.
Save this example separately as Example.cs. Run it independently of the other examples on this page.
Example.cs:
// PROJECT-EXAMPLE
ILabel item = new Item();
System.Console.WriteLine(item.Label);
interface ILabel { string Label { get; } }
class Item : ILabel { public string Label => "Ore"; }Result:
Ore
interface IDescription
{
string Describe();
}
abstract class Job : IDescription
{
public abstract string Describe();
}
sealed class GatherJob : Job
{
public override string Describe() => "Gathering job";
}
IDescription job = new GatherJob();
UO.Print(job.Describe());class NamedJob
{
public string Name { get; }
public NamedJob(string name) => Name = name;
public virtual string Report() => Name;
}
sealed class TimedJob : NamedJob
{
public TimedJob(string name) : base(name) { }
public override string Report() => base.Report() + " — timed";
}
UO.Print(new TimedJob("Search").Report());Interfaces describe a contract, not a finished game operation. Explicit implementation of the interface allows you to separate members of the same name. Modern C# supports default implementations and static abstract members of interfaces in suitable generic contracts.
Official Documentation: Inheritance, Interfaces, and Polymorphism - Microsoft Learn.
ClassicUO — Bad Newbie & C# Scripts IDE