[.NET] Use collection expressions in docs

As of C# 12 we can now use collection expressions to reduce some boilerplate when initializing collections.
This commit is contained in:
Raul Santos 2024-12-21 02:28:59 +01:00
parent 89001f91d2
commit 072ff85f82
No known key found for this signature in database
GPG key ID: B532473AE3A803E4
17 changed files with 73 additions and 73 deletions

View file

@ -853,7 +853,7 @@
print("a", "b", a) # Prints ab[1, 2, 3]
[/gdscript]
[csharp]
var a = new Godot.Collections.Array { 1, 2, 3 };
Godot.Collections.Array a = [1, 2, 3];
GD.Print("a", "b", a); // Prints ab[1, 2, 3]
[/csharp]
[/codeblocks]

View file

@ -17,14 +17,14 @@
print(array[-3]) # Prints "Second"
[/gdscript]
[csharp]
var array = new Godot.Collections.Array{"First", 2, 3, "Last"};
Godot.Collections.Array array = ["First", 2, 3, "Last"];
GD.Print(array[0]); // Prints "First"
GD.Print(array[2]); // Prints 3
GD.Print(array[array.Count - 1]); // Prints "Last"
GD.Print(array[^1]); // Prints "Last"
array[2] = "Second";
array[1] = "Second";
GD.Print(array[1]); // Prints "Second"
GD.Print(array[array.Count - 3]); // Prints "Second"
GD.Print(array[^3]); // Prints "Second"
[/csharp]
[/codeblocks]
[b]Note:[/b] Arrays are always passed by [b]reference[/b]. To get a copy of an array that can be modified independently of the original array, use [method duplicate].
@ -358,7 +358,7 @@
print(array) # Prints [2, 2, 2, 2, 2]
[/gdscript]
[csharp]
var array = new Godot.Collections.Array();
Godot.Collections.Array array = [];
array.Resize(5);
array.Fill(2);
GD.Print(array); // Prints [2, 2, 2, 2, 2]
@ -460,7 +460,7 @@
print(["inside", 7].has("7")) # Prints false
[/gdscript]
[csharp]
var arr = new Godot.Collections.Array { "inside", 7 };
Godot.Collections.Array arr = ["inside", 7];
// By C# convention, this method is renamed to `Contains`.
GD.Print(arr.Contains("inside")); // Prints True
GD.Print(arr.Contains("outside")); // Prints False
@ -573,7 +573,7 @@
print([1, 2, 3.25, "Hi"].pick_random())
[/gdscript]
[csharp]
var array = new Godot.Collections.Array { 1, 2, 3.25f, "Hi" };
Godot.Collections.Array array = [1, 2, 3.25f, "Hi"];
GD.Print(array.PickRandom()); // May print 1, 2, 3.25, or "Hi".
[/csharp]
[/codeblocks]
@ -755,7 +755,7 @@
print(numbers) # Prints [2.5, 5, 8, 10]
[/gdscript]
[csharp]
var numbers = new Godot.Collections.Array { 10, 5, 2.5, 8 };
Godot.Collections.Array numbers = [10, 5, 2.5, 8];
numbers.Sort();
GD.Print(numbers); // Prints [2.5, 5, 8, 10]
[/csharp]
@ -817,8 +817,8 @@
[/gdscript]
[csharp]
// Note that concatenation is not possible with C#'s native Array type.
var array1 = new Godot.Collections.Array{"One", 2};
var array2 = new Godot.Collections.Array{3, "Four"};
Godot.Collections.Array array1 = ["One", 2];
Godot.Collections.Array array2 = [3, "Four"];
GD.Print(array1 + array2); // Prints ["One", 2, 3, "Four"]
[/csharp]
[/codeblocks]

View file

@ -25,16 +25,16 @@
m.mesh = arr_mesh
[/gdscript]
[csharp]
var vertices = new Vector3[]
{
Vector3[] vertices =
[
new Vector3(0, 1, 0),
new Vector3(1, 0, 0),
new Vector3(0, 0, 1),
};
];
// Initialize the ArrayMesh.
var arrMesh = new ArrayMesh();
var arrays = new Godot.Collections.Array();
Godot.Collections.Array arrays = [];
arrays.Resize((int)Mesh.ArrayType.Max);
arrays[(int)Mesh.ArrayType.Vertex] = vertices;

View file

@ -19,7 +19,7 @@
server.listen(4242)
var key = load("key.key") # Your private key.
var cert = load("cert.crt") # Your X509 certificate.
dtls.setup(key, cert)
dtls.setup(TlsOptions.server(key, cert))
func _process(delta):
while server.is_connection_available():
@ -45,19 +45,19 @@
{
private DtlsServer _dtls = new DtlsServer();
private UdpServer _server = new UdpServer();
private Godot.Collections.Array<PacketPeerDtls> _peers = new Godot.Collections.Array<PacketPeerDtls>();
private Godot.Collections.Array<PacketPeerDtls> _peers = [];
public override void _Ready()
{
_server.Listen(4242);
var key = GD.Load<CryptoKey>("key.key"); // Your private key.
var cert = GD.Load<X509Certificate>("cert.crt"); // Your X509 certificate.
_dtls.Setup(key, cert);
_dtls.Setup(TlsOptions.Server(key, cert));
}
public override void _Process(double delta)
{
while (Server.IsConnectionAvailable())
while (_server.IsConnectionAvailable())
{
PacketPeerUdp peer = _server.TakeConnection();
PacketPeerDtls dtlsPeer = _dtls.TakeConnection(peer);

View file

@ -1730,7 +1730,7 @@
DisplayServer.WindowSetMousePassthrough(GetNode<Polygon2D>("Polygon2D").Polygon);
// Reset region to default.
DisplayServer.WindowSetMousePassthrough(new Vector2[] {});
DisplayServer.WindowSetMousePassthrough([]);
[/csharp]
[/codeblocks]
[b]Note:[/b] On Windows, the portion of a window that lies outside the region is not drawn, while on Linux (X11) and macOS it is.

View file

@ -63,7 +63,7 @@
public override string[] _GetRecognizedExtensions()
{
return new string[] { "special", "spec" };
return ["special", "spec"];
}
public override string _GetSaveExtension()
@ -88,14 +88,14 @@
public override Godot.Collections.Array<Godot.Collections.Dictionary> _GetImportOptions(string path, int presetIndex)
{
return new Godot.Collections.Array<Godot.Collections.Dictionary>
{
return
[
new Godot.Collections.Dictionary
{
{ "name", "myOption" },
{ "default_value", false },
}
};
},
];
}
public override Error _Import(string sourceFile, string savePath, Godot.Collections.Dictionary options, Godot.Collections.Array<string> platformVariants, Godot.Collections.Array<string> genFiles)

View file

@ -45,7 +45,7 @@
public override string[] _GetRecognizedExtensions()
{
return new string[] { "csv" };
return ["csv"];
}
}
[/csharp]
@ -62,11 +62,11 @@
[/gdscript]
[csharp]
// This will add a message with msgid "Test 1", msgctxt "context", and msgid_plural "test 1 plurals".
msgidsContextPlural.Add(new Godot.Collections.Array{"Test 1", "context", "test 1 Plurals"});
msgidsContextPlural.Add(["Test 1", "context", "test 1 Plurals"]);
// This will add a message with msgid "A test without context" and msgid_plural "plurals".
msgidsContextPlural.Add(new Godot.Collections.Array{"A test without context", "", "plurals"});
msgidsContextPlural.Add(["A test without context", "", "plurals"]);
// This will add a message with msgid "Only with context" and msgctxt "a friendly context".
msgidsContextPlural.Add(new Godot.Collections.Array{"Only with context", "a friendly context", ""});
msgidsContextPlural.Add(["Only with context", "a friendly context", ""]);
[/csharp]
[/codeblocks]
[b]Note:[/b] If you override parsing logic for standard script types (GDScript, C#, etc.), it would be better to load the [code]path[/code] argument using [method ResourceLoader.load]. This is because built-in scripts are loaded as [Resource] type, not [FileAccess] type. For example:
@ -90,7 +90,7 @@
public override string[] _GetRecognizedExtensions()
{
return new string[] { "gd" };
return ["gd"];
}
[/csharp]
[/codeblocks]

View file

@ -204,7 +204,7 @@
print(polygon) # Prints [(50.0, 50.0), (150.0, 50.0), (150.0, 150.0), (50.0, 150.0)]
[/gdscript]
[csharp]
var polygon = new Vector2[] { new Vector2(0, 0), new Vector2(100, 0), new Vector2(100, 100), new Vector2(0, 100) };
Vector2[] polygon = [new Vector2(0, 0), new Vector2(100, 0), new Vector2(100, 100), new Vector2(0, 100)];
var offset = new Vector2(50, 50);
polygon = new Transform2D(0, offset) * polygon;
GD.Print((Variant)polygon); // Prints [(50, 50), (150, 50), (150, 150), (50, 150)]

View file

@ -156,7 +156,7 @@
[csharp]
var fields = new Godot.Collections.Dictionary { { "username", "user" }, { "password", "pass" } };
string queryString = new HttpClient().QueryStringFromDict(fields);
string[] headers = { "Content-Type: application/x-www-form-urlencoded", $"Content-Length: {queryString.Length}" };
string[] headers = ["Content-Type: application/x-www-form-urlencoded", $"Content-Length: {queryString.Length}"];
var result = new HttpClient().Request(HttpClient.Method.Post, "index.php", headers, queryString);
[/csharp]
[/codeblocks]

View file

@ -16,7 +16,7 @@
[/gdscript]
[csharp]
var newNavigationMesh = new NavigationPolygon();
var boundingOutline = new Vector2[] { new Vector2(0, 0), new Vector2(0, 50), new Vector2(50, 50), new Vector2(50, 0) };
Vector2[] boundingOutline = [new Vector2(0, 0), new Vector2(0, 50), new Vector2(50, 50), new Vector2(50, 0)];
newNavigationMesh.AddOutline(boundingOutline);
NavigationServer2D.BakeFromSourceGeometryData(newNavigationMesh, new NavigationMeshSourceGeometryData2D());
GetNode<NavigationRegion2D>("NavigationRegion2D").NavigationPolygon = newNavigationMesh;
@ -34,9 +34,9 @@
[/gdscript]
[csharp]
var newNavigationMesh = new NavigationPolygon();
var newVertices = new Vector2[] { new Vector2(0, 0), new Vector2(0, 50), new Vector2(50, 50), new Vector2(50, 0) };
Vector2[] newVertices = [new Vector2(0, 0), new Vector2(0, 50), new Vector2(50, 50), new Vector2(50, 0)];
newNavigationMesh.Vertices = newVertices;
var newPolygonIndices = new int[] { 0, 1, 2, 3 };
int[] newPolygonIndices = [0, 1, 2, 3];
newNavigationMesh.AddPolygon(newPolygonIndices);
GetNode<NavigationRegion2D>("NavigationRegion2D").NavigationPolygon = newNavigationMesh;
[/csharp]

View file

@ -59,7 +59,7 @@
var pid = OS.create_process(OS.get_executable_path(), [])
[/gdscript]
[csharp]
var pid = OS.CreateProcess(OS.GetExecutablePath(), new string[] {});
var pid = OS.CreateProcess(OS.GetExecutablePath(), []);
[/csharp]
[/codeblocks]
See [method execute] if you wish to run an external command and retrieve the results.
@ -105,8 +105,8 @@
var exit_code = OS.execute("ls", ["-l", "/tmp"], output)
[/gdscript]
[csharp]
var output = new Godot.Collections.Array();
int exitCode = OS.Execute("ls", new string[] {"-l", "/tmp"}, output);
Godot.Collections.Array output = [];
int exitCode = OS.Execute("ls", ["-l", "/tmp"], output);
[/csharp]
[/codeblocks]
If you wish to access a shell built-in or execute a composite command, a platform-specific shell can be invoked. For example:
@ -116,8 +116,8 @@
OS.execute("CMD.exe", ["/C", "cd %TEMP% && dir"], output)
[/gdscript]
[csharp]
var output = new Godot.Collections.Array();
OS.Execute("CMD.exe", new string[] {"/C", "cd %TEMP% && dir"}, output);
Godot.Collections.Array output = [];
OS.Execute("CMD.exe", ["/C", "cd %TEMP% && dir"], output);
[/csharp]
[/codeblocks]
[b]Note:[/b] This method is implemented on Android, Linux, macOS, and Windows.

View file

@ -61,14 +61,14 @@
public override Godot.Collections.Array<Godot.Collections.Dictionary> _GetPropertyList()
{
return new Godot.Collections.Array<Godot.Collections.Dictionary>()
{
return
[
new Godot.Collections.Dictionary()
{
{ "name", "FakeProperty" },
{ "type", (int)Variant.Type.Int }
}
};
{ "type", (int)Variant.Type.Int },
},
];
}
[/csharp]
[/codeblocks]
@ -137,11 +137,11 @@
}
}
private Godot.Collections.Array<int> _numbers = new();
private Godot.Collections.Array<int> _numbers = [];
public override Godot.Collections.Array<Godot.Collections.Dictionary> _GetPropertyList()
{
var properties = new Godot.Collections.Array<Godot.Collections.Dictionary>();
Godot.Collections.Array<Godot.Collections.Dictionary> properties = [];
for (int i = 0; i < _numberCount; i++)
{
@ -322,14 +322,14 @@
public override Godot.Collections.Array<Godot.Collections.Dictionary> _GetPropertyList()
{
return new Godot.Collections.Array<Godot.Collections.Dictionary>()
{
return
[
new Godot.Collections.Dictionary()
{
{ "name", "FakeProperty" },
{ "type", (int)Variant.Type.Int }
}
};
{ "type", (int)Variant.Type.Int },
},
];
}
[/csharp]
[/codeblocks]
@ -416,19 +416,19 @@
])
[/gdscript]
[csharp]
AddUserSignal("Hurt", new Godot.Collections.Array()
{
AddUserSignal("Hurt",
[
new Godot.Collections.Dictionary()
{
{ "name", "damage" },
{ "type", (int)Variant.Type.Int }
{ "type", (int)Variant.Type.Int },
},
new Godot.Collections.Dictionary()
{
{ "name", "source" },
{ "type", (int)Variant.Type.Object }
}
});
{ "type", (int)Variant.Type.Object },
},
]);
[/csharp]
[/codeblocks]
</description>
@ -494,7 +494,7 @@
[/gdscript]
[csharp]
var node = new Node3D();
node.Callv(Node3D.MethodName.Rotate, new Godot.Collections.Array { new Vector3(1f, 0f, 0f), 1.571f });
node.Callv(Node3D.MethodName.Rotate, [new Vector3(1f, 0f, 0f), 1.571f]);
[/csharp]
[/codeblocks]
[b]Note:[/b] In C#, [param method] must be in snake_case when referring to built-in Godot methods. Prefer using the names exposed in the [code]MethodName[/code] class to avoid allocating a new [StringName] on each call.

View file

@ -369,7 +369,7 @@
print(array.hex_encode()) # Prints: 0b2eff
[/gdscript]
[csharp]
var array = new byte[] {11, 46, 255};
byte[] array = [11, 46, 255];
GD.Print(array.HexEncode()); // Prints: 0b2eff
[/csharp]
[/codeblocks]

View file

@ -54,13 +54,13 @@
[/gdscript]
[csharp]
var polygonPathFinder = new PolygonPathFinder();
var points = new Vector2[]
{
Vector2[] points =
[
new Vector2(0.0f, 0.0f),
new Vector2(1.0f, 0.0f),
new Vector2(0.0f, 1.0f)
};
var connections = new int[] { 0, 1, 1, 2, 2, 0 };
];
int[] connections = [0, 1, 1, 2, 2, 0];
polygonPathFinder.Setup(points, connections);
GD.Print(polygonPathFinder.IsPointInside(new Vector2(0.2f, 0.2f))); // Prints True
GD.Print(polygonPathFinder.IsPointInside(new Vector2(1.0f, 1.0f))); // Prints False
@ -91,13 +91,13 @@
[/gdscript]
[csharp]
var polygonPathFinder = new PolygonPathFinder();
var points = new Vector2[]
{
Vector2[] points =
[
new Vector2(0.0f, 0.0f),
new Vector2(1.0f, 0.0f),
new Vector2(0.0f, 1.0f)
};
var connections = new int[] { 0, 1, 1, 2, 2, 0 };
];
int[] connections = [0, 1, 1, 2, 2, 0];
polygonPathFinder.Setup(points, connections);
[/csharp]
[/codeblocks]

View file

@ -571,7 +571,7 @@
print("---".join(fruits)) # Prints "Apple---Orange---Pear---Kiwi"
[/gdscript]
[csharp]
var fruits = new string[] {"Apple", "Orange", "Pear", "Kiwi"};
string[] fruits = ["Apple", "Orange", "Pear", "Kiwi"];
// In C#, this method is static.
GD.Print(string.Join(", ", fruits)); // Prints "Apple, Orange, Pear, Kiwi"

View file

@ -546,7 +546,7 @@
print("---".join(fruits)) # Prints "Apple---Orange---Pear---Kiwi"
[/gdscript]
[csharp]
var fruits = new string[] {"Apple", "Orange", "Pear", "Kiwi"};
string[] fruits = ["Apple", "Orange", "Pear", "Kiwi"];
// In C#, this method is static.
GD.Print(string.Join(", ", fruits)); // Prints "Apple, Orange, Pear, Kiwi"

View file

@ -641,13 +641,13 @@
[/gdscript]
[csharp]
// Set region, using Path2D node.
GetNode&lt;Window&gt;("Window").MousePassthrough = GetNode&lt;Path2D&gt;("Path2D").Curve.GetBakedPoints();
GetNode&lt;Window&gt;("Window").MousePassthroughPolygon = GetNode&lt;Path2D&gt;("Path2D").Curve.GetBakedPoints();
// Set region, using Polygon2D node.
GetNode&lt;Window&gt;("Window").MousePassthrough = GetNode&lt;Polygon2D&gt;("Polygon2D").Polygon;
GetNode&lt;Window&gt;("Window").MousePassthroughPolygon = GetNode&lt;Polygon2D&gt;("Polygon2D").Polygon;
// Reset region to default.
GetNode&lt;Window&gt;("Window").MousePassthrough = new Vector2[] {};
GetNode&lt;Window&gt;("Window").MousePassthroughPolygon = [];
[/csharp]
[/codeblocks]
[b]Note:[/b] This property is ignored if [member mouse_passthrough] is set to [code]true[/code].