Extension methods
Forms that compile (7)
Section titled “Forms that compile (7)”| Syntax | Description | Note |
|---|---|---|
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 type | It can be written without holding the type’s definition. |
public static readonly int Max = Base * 2; | Static readonly field | The 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 property | It 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 |
Forms that don’t compile (5)
Section titled “Forms that don’t compile (5)”| Syntax | Description | Error | Reason | Alternative |
|---|---|---|---|---|
public static int Count = 0; (inside a static class) | Reading or writing a mutable static field | TUKI0001 | by design | Make it static readonly (put values that change in a Behaviour field) |
public static int Volume { get; set; } | Reading a static property that has a set | TUKI0001 | by design | Make it get-only, or put it in a field of the Behaviour |
public static readonly int Max; | Reading a static readonly field with no initializer | TUKI0001 | by design | Write the value with = … |
public static readonly int B = A * 2; | Reading another static field from an initializer | TUKI0001 | by design | Write a constant expression (a const can be read) |
Id(int v) and Id<T>(T v) side by side | Calling a name shared by a generic method and another method | TUKI0001 | by design | Give the generic one a different name |
Declaration and calling
Section titled “Declaration and calling”Calling by type name
Section titled “Calling by type 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 }}Adding to a built-in type
Section titled “Adding to a built-in type”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 }}Adding to your own value type
Section titled “Adding to your own value type”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 }}Adding to a runtime type
Section titled “Adding to a runtime type”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 }}Members of a static class
Section titled “Members of a static class”Static readonly field
Section titled “Static readonly field”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" }}Getter-only static property
Section titled “Getter-only static property”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 }}Checking on use
Section titled “Checking on use”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 }}