Skip to content

Extension methods

SyntaxDescriptionNote
ExtStaticHelpers.Twice(2)Calling by type name
public static int Twice(this int n)Adding to a built-in type
public static int Double(this ExtPoint p)Adding to your own value type
public static float Flat(this Vector3 v)Adding to a runtime typeIt can be written without holding the type’s definition.
public static readonly int Max = Base * 2;Static readonly fieldThe initializer can be a constant expression or a value creation such as new Vector3(…) (a const can also be read).
public static int Half => Size / 2;Getter-only static propertyIt can have a body, or be a { get; } with an initializer.
Twice(int) / Twice(float) / Twice(int, int)Methods with the same name and different parameters
SyntaxDescriptionErrorReasonAlternative
public static int Count = 0; (inside a static class)Reading or writing a mutable static fieldTUKI0001by designMake it static readonly (put values that change in a Behaviour field)
public static int Volume { get; set; }Reading a static property that has a setTUKI0001by designMake it get-only, or put it in a field of the Behaviour
public static readonly int Max;Reading a static readonly field with no initializerTUKI0001by designWrite the value with = …
public static readonly int B = A * 2;Reading another static field from an initializerTUKI0001by designWrite a constant expression (a const can be read)
Id(int v) and Id<T>(T v) side by sideCalling a name shared by a generic method and another methodTUKI0001by designGive the generic one a different name
using UnityEngine;
using Tsukimi;
public static class ExtStaticHelpers
{
public static int Twice(int n) { return n * 2; }
}
public class ExtStatic : TsukimiBehaviour
{
void Start()
{
Debug.Log(ExtStaticHelpers.Twice(2)); // => 4
}
}
using UnityEngine;
using Tsukimi;
public static class ExtIntHelpers
{
public static int Twice(this int n) { return n * 2; }
}
public class ExtPrimitive : TsukimiBehaviour
{
void Start()
{
Debug.Log(2.Twice()); // => 4
}
}

It can be added to a struct you declared yourself too.

using UnityEngine;
using Tsukimi;
public struct ExtPoint
{
public int X;
}
public static class ExtPointHelpers
{
public static int Double(this ExtPoint p) { return p.X * 2; }
}
public class ExtStruct : TsukimiBehaviour
{
void Start()
{
ExtPoint p = new ExtPoint();
p.X = 2;
Debug.Log(p.Double()); // => 4
}
}

It can be added to types the runtime exposes too.

using UnityEngine;
using Tsukimi;
public static class ExtVectorHelpers
{
public static float Flat(this Vector3 v) { return v.x + v.z; }
}
public class ExtEnvironment : TsukimiBehaviour
{
void Start()
{
Debug.Log(Vector3.one.Flat()); // => 2
}
}

A static class can hold static readonly fields, read with the class name in front.

using UnityEngine;
using Tsukimi;
public static class ExtLimits
{
public const int Base = 5;
public static readonly int Max = Base * 2;
public static readonly Vector3 Lift = new Vector3(0f, 2f, 0f);
public static readonly string Label = "deck";
}
public class ExtReadonly : TsukimiBehaviour
{
void Start()
{
Debug.Log(ExtLimits.Max); // => 10
Debug.Log(ExtLimits.Lift.y); // => 2
Debug.Log(ExtLimits.Label); // => "deck"
}
}

A static class can hold get-only static properties.

using UnityEngine;
using Tsukimi;
public static class ExtGrid
{
public static readonly int Size = 8;
public static int Half => Size / 2;
public static int Last { get { return Size - 1; } }
public static int Cells { get; } = 64;
}
public class ExtGetter : TsukimiBehaviour
{
void Start()
{
Debug.Log(ExtGrid.Half); // => 4
Debug.Log(ExtGrid.Last); // => 7
Debug.Log(ExtGrid.Cells); // => 64
}
}

Methods with the same name and different parameters

Section titled “Methods with the same name and different parameters”

Methods with the same name can sit side by side when the parameter types or counts differ.

using UnityEngine;
using Tsukimi;
public static class ExtOverloads
{
public static int Twice(int n) { return n * 2; }
public static float Twice(float n) { return n * 2f; }
public static int Twice(int n, int times) { return n * 2 * times; }
}
public class ExtOverload : TsukimiBehaviour
{
void Start()
{
Debug.Log(ExtOverloads.Twice(2)); // => 4
Debug.Log(ExtOverloads.Twice(1.5f)); // => 3
Debug.Log(ExtOverloads.Twice(2, 3)); // => 12
}
}

Members of a static class are checked only where they are used (a method that is never called, or a field or property that is never read, is not an error even if it contains a form that doesn’t compile).

using UnityEngine;
using Tsukimi;
public static class ExtTextHelpers
{
public static int Count = 0;
public static int Twice(int n) { return n * 2; }
public static int Parse(string s)
{
try { return int.Parse(s); } catch { return 0; }
}
}
public class ExtCheckedOnUse : TsukimiBehaviour
{
void Start()
{
Debug.Log(ExtTextHelpers.Twice(2)); // => 4
}
}