Skip to content

Commit c9928f6

Browse files
authored
Merge pull request #10433 from raulsntos/dotnet/collection-expressions
[.NET] Use collection expressions
2 parents da1ef85 + cea7873 commit c9928f6

14 files changed

+69
-65
lines changed

tutorials/2d/custom_drawing_in_2d.rst

+5-5
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ like this:
377377
// We are going to paint with this color.
378378
Color godotBlue = new Color("478cbf");
379379
// We pass the array of Vector2 to draw the shape.
380-
DrawPolygon(_head, new Color[]{ godotBlue });
380+
DrawPolygon(_head, [godotBlue]);
381381
}
382382

383383
When running it you should see something like this:
@@ -474,7 +474,7 @@ draw the line, like this:
474474
Color white = Colors.White;
475475
Color godotBlue = new Color("478cbf");
476476

477-
DrawPolygon(_head, new Color[]{ godotBlue });
477+
DrawPolygon(_head, [godotBlue]);
478478

479479
// We draw the while line on top of the previous shape.
480480
DrawPolyline(_mouth, white, _mouthWidth);
@@ -535,7 +535,7 @@ its radius, and the third is its color:
535535
Color godotBlue = new Color("478cbf");
536536
Color grey = new Color("414042");
537537

538-
DrawPolygon(_head, new Color[]{ godotBlue });
538+
DrawPolygon(_head, [godotBlue]);
539539
DrawPolyline(_mouth, white, _mouthWidth);
540540

541541
// Four circles for the 2 eyes: 2 white, 2 grey.
@@ -589,7 +589,7 @@ like this:
589589
Color godotBlue = new Color("478cbf");
590590
Color grey = new Color("414042");
591591

592-
DrawPolygon(_head, new Color[]{ godotBlue });
592+
DrawPolygon(_head, [godotBlue]);
593593
DrawPolyline(_mouth, white, _mouthWidth);
594594
DrawCircle(new Vector2(42.479f, 65.4825f), 9.3905f, white);
595595
DrawCircle(new Vector2(85.524f, 65.4825f), 9.3905f, white);
@@ -652,7 +652,7 @@ to do it, like this:
652652
Color godotBlue = new Color("478cbf");
653653
Color grey = new Color("414042");
654654

655-
DrawPolygon(_head, new Color[]{ godotBlue });
655+
DrawPolygon(_head, [godotBlue]);
656656
DrawPolyline(_mouth, white, _mouthWidth);
657657
DrawCircle(new Vector2(42.479f, 65.4825f), 9.3905f, white);
658658
DrawCircle(new Vector2(85.524f, 65.4825f), 9.3905f, white);

tutorials/3d/procedural_geometry/arraymesh.rst

+11-11
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ Under ``_ready()``, create a new Array.
9494

9595
.. code-tab:: csharp C#
9696

97-
var surfaceArray = new Godot.Collections.Array();
97+
Godot.Collections.Array surfaceArray = [];
9898

9999
This will be the array that we keep our surface information in - it will hold
100100
all the arrays of data that the surface needs. Godot will expect it to be of
@@ -108,7 +108,7 @@ size ``Mesh.ARRAY_MAX``, so resize it accordingly.
108108

109109
.. code-tab:: csharp C#
110110

111-
var surfaceArray = new Godot.Collections.Array();
111+
Godot.Collections.Array surfaceArray = [];
112112
surfaceArray.Resize((int)Mesh.ArrayType.Max);
113113

114114
Next create the arrays for each data type you will use.
@@ -123,10 +123,10 @@ Next create the arrays for each data type you will use.
123123

124124
.. code-tab:: csharp C#
125125

126-
var verts = new List<Vector3>();
127-
var uvs = new List<Vector2>();
128-
var normals = new List<Vector3>();
129-
var indices = new List<int>();
126+
List<Vector3> verts = [];
127+
List<Vector2> uvs = [];
128+
List<Vector3> normals = [];
129+
List<int> indices = [];
130130

131131
Once you have filled your data arrays with your geometry you can create a mesh
132132
by adding each array to ``surface_array`` and then committing to the mesh.
@@ -196,14 +196,14 @@ Put together, the full code looks like:
196196
{
197197
public override void _Ready()
198198
{
199-
var surfaceArray = new Godot.Collections.Array();
199+
Godot.Collections.Array surfaceArray = [];
200200
surfaceArray.Resize((int)Mesh.ArrayType.Max);
201201

202202
// C# arrays cannot be resized or expanded, so use Lists to create geometry.
203-
var verts = new List<Vector3>();
204-
var uvs = new List<Vector2>();
205-
var normals = new List<Vector3>();
206-
var indices = new List<int>();
203+
List<Vector3> verts = [];
204+
List<Vector2> uvs = [];
205+
List<Vector3> normals = [];
206+
List<int> indices = [];
207207

208208
/***********************************
209209
* Insert code here to generate mesh.

tutorials/best_practices/data_preferences.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ tree structures.
259259
{
260260
private TreeNode _parent = null;
261261

262-
private List<TreeNode> _children = new();
262+
private List<TreeNode> _children = [];
263263

264264
public override void _Notification(int what)
265265
{

tutorials/best_practices/godot_interfaces.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ access.
112112
{
113113
if (EnemyScn == null)
114114
{
115-
return new string[] { "Must initialize property 'EnemyScn'." };
115+
return ["Must initialize property 'EnemyScn'."];
116116
}
117-
return Array.Empty<string>();
117+
return [];
118118
}
119119
}
120120

tutorials/math/random_number_generation.rst

+5-5
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ to do it for us:
212212
.. code-tab:: csharp
213213

214214
// Use Godot's Array type instead of a BCL type so we can use `PickRandom()` on it.
215-
private Godot.Collections.Array<string> _fruits = new Godot.Collections.Array<string> { "apple", "orange", "pear", "banana" };
215+
private Godot.Collections.Array<string> _fruits = ["apple", "orange", "pear", "banana"];
216216

217217
public override void _Ready()
218218
{
@@ -273,7 +273,7 @@ prevent repetition:
273273

274274
.. code-tab:: csharp
275275

276-
private string[] _fruits = { "apple", "orange", "pear", "banana" };
276+
private string[] _fruits = ["apple", "orange", "pear", "banana"];
277277
private string _lastFruit = "";
278278

279279
public override void _Ready()
@@ -450,8 +450,8 @@ get a value from another array as follows:
450450
// Prints a random element using the weighted index that is returned by `RandWeighted()`.
451451
// Here, "apple" will be returned twice as rarely as "orange" and "pear".
452452
// "banana" is twice as common as "orange" and "pear", and four times as common as "apple".
453-
string[] fruits = { "apple", "orange", "pear", "banana" };
454-
float[] probabilities = { 0.5, 1, 1, 2 };
453+
string[] fruits = ["apple", "orange", "pear", "banana"];
454+
float[] probabilities = [0.5f, 1, 1, 2];
455455

456456
var random = new RandomNumberGenerator();
457457
GD.Print(fruits[random.RandWeighted(probabilities)]);
@@ -501,7 +501,7 @@ ends up empty. When that happens, you reinitialize it to its default value:
501501

502502
.. code-tab:: csharp
503503

504-
private Godot.Collections.Array<string> _fruits = new() { "apple", "orange", "pear", "banana" };
504+
private Godot.Collections.Array<string> _fruits = ["apple", "orange", "pear", "banana"];
505505
// A copy of the fruits array so we can restore the original value into `fruits`.
506506
private Godot.Collections.Array<string> _fruitsFull;
507507

tutorials/navigation/navigation_using_navigationmeshes.rst

+11-11
Original file line numberDiff line numberDiff line change
@@ -434,13 +434,13 @@ The following script uses the NavigationServer to parse source geometry from the
434434
// If we did not parse a TileMap with navigation mesh cells we may now only
435435
// have obstruction outlines so add at least one traversable outline
436436
// so the obstructions outlines have something to "cut" into.
437-
_sourceGeometry.AddTraversableOutline(new Vector2[]
438-
{
437+
_sourceGeometry.AddTraversableOutline(
438+
[
439439
new Vector2(0.0f, 0.0f),
440440
new Vector2(500.0f, 0.0f),
441441
new Vector2(500.0f, 500.0f),
442442
new Vector2(0.0f, 500.0f),
443-
});
443+
]);
444444

445445
// Bake the navigation mesh on a thread with the source geometry data.
446446
NavigationServer2D.BakeFromSourceGeometryDataAsync(_navigationMesh, _sourceGeometry, _callbackBaking);
@@ -615,16 +615,16 @@ The following script uses the NavigationServer to update a navigation region wit
615615
NavigationServer2D.RegionSetMap(_regionRid, GetWorld2D().NavigationMap);
616616

617617
// Add vertices for a convex polygon.
618-
_navigationMesh.Vertices = new Vector2[]
619-
{
618+
_navigationMesh.Vertices =
619+
[
620620
new Vector2(0, 0),
621621
new Vector2(100.0f, 0),
622622
new Vector2(100.0f, 100.0f),
623623
new Vector2(0, 100.0f),
624-
};
624+
];
625625

626626
// Add indices for the polygon.
627-
_navigationMesh.AddPolygon(new int[] { 0, 1, 2, 3 });
627+
_navigationMesh.AddPolygon([0, 1, 2, 3]);
628628

629629
NavigationServer2D.RegionSetNavigationPolygon(_regionRid, _navigationMesh);
630630
}
@@ -680,16 +680,16 @@ The following script uses the NavigationServer to update a navigation region wit
680680
NavigationServer3D.RegionSetMap(_regionRid, GetWorld3D().NavigationMap);
681681

682682
// Add vertices for a convex polygon.
683-
_navigationMesh.Vertices = new Vector3[]
684-
{
683+
_navigationMesh.Vertices =
684+
[
685685
new Vector3(-1.0f, 0.0f, 1.0f),
686686
new Vector3(1.0f, 0.0f, 1.0f),
687687
new Vector3(1.0f, 0.0f, -1.0f),
688688
new Vector3(-1.0f, 0.0f, -1.0f),
689-
};
689+
];
690690

691691
// Add indices for the polygon.
692-
_navigationMesh.AddPolygon(new int[] { 0, 1, 2, 3 });
692+
_navigationMesh.AddPolygon([0, 1, 2, 3]);
693693

694694
NavigationServer3D.RegionSetNavigationMesh(_regionRid, _navigationMesh);
695695
}

tutorials/navigation/navigation_using_navigationobstacles.rst

+12-12
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,13 @@ Obstacles are not involved in the source geometry parsing so adding them just be
8181

8282
.. code-tab:: csharp 2D C#
8383

84-
Vector2[] obstacleOutline = new Vector2[]
85-
{
84+
Vector2[] obstacleOutline
85+
[
8686
new Vector2(-50, -50),
8787
new Vector2(50, -50),
8888
new Vector2(50, 50),
8989
new Vector2(-50, 50),
90-
};
90+
];
9191

9292
var navigationMesh = new NavigationPolygon();
9393
var sourceGeometry = new NavigationMeshSourceGeometryData2D();
@@ -123,13 +123,13 @@ Obstacles are not involved in the source geometry parsing so adding them just be
123123

124124
.. code-tab:: csharp 3D C#
125125

126-
Vector3[] obstacleOutline = new Vector3[]
127-
{
126+
Vector3[] obstacleOutline =
127+
[
128128
new Vector3(-5, 0, -5),
129129
new Vector3(5, 0, -5),
130130
new Vector3(5, 0, 5),
131131
new Vector3(-5, 0, 5),
132-
};
132+
];
133133

134134
var navigationMesh = new NavigationMesh();
135135
var sourceGeometry = new NavigationMeshSourceGeometryData3D();
@@ -234,13 +234,13 @@ For static use an array of ``vertices`` is required.
234234
NavigationServer2D.ObstacleSetRadius(newObstacleRid, 5.0f);
235235

236236
// Use obstacle static by adding a square that pushes agents out.
237-
Vector2[] outline = new Vector2[]
238-
{
237+
Vector2[] outline =
238+
[
239239
new Vector2(-100, -100),
240240
new Vector2(100, -100),
241241
new Vector2(100, 100),
242242
new Vector2(-100, 100),
243-
};
243+
];
244244
NavigationServer2D.ObstacleSetVertices(newObstacleRid, outline);
245245

246246
// Enable the obstacle.
@@ -280,13 +280,13 @@ For static use an array of ``vertices`` is required.
280280
NavigationServer3D.ObstacleSetRadius(newObstacleRid, 5.0f);
281281

282282
// Use obstacle static by adding a square that pushes agents out.
283-
Vector3[] outline = new Vector3[]
284-
{
283+
Vector3[] outline =
284+
[
285285
new Vector3(-5, 0, -5),
286286
new Vector3(5, 0, -5),
287287
new Vector3(5, 0, 5),
288288
new Vector3(-5, 0, 5),
289-
};
289+
];
290290
NavigationServer3D.ObstacleSetVertices(newObstacleRid, outline);
291291
// Set the obstacle height on the y-axis.
292292
NavigationServer3D.ObstacleSetHeight(newObstacleRid, 1.0f);

tutorials/navigation/navigation_using_navigationservers.rst

+4-4
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,14 @@ Afterwards the function waits for the next physics frame before continuing with
169169
// Create a procedural navigation mesh for the region.
170170
var newNavigationMesh = new NavigationMesh()
171171
{
172-
Vertices = new[]
173-
{
172+
Vertices =
173+
[
174174
new Vector3(0.0f, 0.0f, 0.0f),
175175
new Vector3(9.0f, 0.0f, 0.0f),
176176
new Vector3(0.0f, 0.0f, 9.0f),
177-
},
177+
],
178178
};
179-
int[] polygon = new[] { 0, 1, 2 };
179+
int[] polygon = [0, 1, 2];
180180
newNavigationMesh.AddPolygon(polygon);
181181
NavigationServer3D.RegionSetNavigationMesh(region, newNavigationMesh);
182182

tutorials/networking/http_client_class.rst

+5-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,11 @@ It will connect and fetch a website.
139139
Debug.Assert(http.GetStatus() == HTTPClient.Status.Connected); // Check if the connection was made successfully.
140140

141141
// Some headers.
142-
string[] headers = { "User-Agent: Pirulo/1.0 (Godot)", "Accept: */*" };
142+
string[] headers =
143+
[
144+
"User-Agent: Pirulo/1.0 (Godot)",
145+
"Accept: */*",
146+
];
143147

144148
err = http.Request(HTTPClient.Method.Get, "/ChangeLog-5.php", headers); // Request a page from the site.
145149
Debug.Assert(err == Error.Ok); // Make sure all is OK.

tutorials/networking/http_request_class.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ But what if you need to send data to the server? Here is a common way of doing i
127127
.. code-tab:: csharp
128128

129129
string json = Json.Stringify(dataToSend);
130-
string[] headers = new string[] { "Content-Type: application/json" };
130+
string[] headers = ["Content-Type: application/json"];
131131
HttpRequest httpRequest = GetNode<HttpRequest>("HTTPRequest");
132132
httpRequest.Request(url, headers, HttpClient.Method.Post, json);
133133

@@ -147,7 +147,7 @@ For example, to set a custom user agent (the HTTP ``User-Agent`` header) you cou
147147
.. code-tab:: csharp
148148

149149
HttpRequest httpRequest = GetNode<HttpRequest>("HTTPRequest");
150-
httpRequest.Request("https://api.github.com/repos/godotengine/godot/releases/latest", new string[] { "User-Agent: YourCustomUserAgent" });
150+
httpRequest.Request("https://api.github.com/repos/godotengine/godot/releases/latest", ["User-Agent: YourCustomUserAgent"]);
151151

152152
.. danger::
153153

tutorials/physics/ray-casting.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ from a CharacterBody2D or any other collision object node:
224224
{
225225
var spaceState = GetWorld2D().DirectSpaceState;
226226
var query = PhysicsRayQueryParameters2D.Create(globalPosition, playerPosition);
227-
query.Exclude = new Godot.Collections.Array<Rid> { GetRid() };
227+
query.Exclude = [GetRid()];
228228
var result = spaceState.IntersectRay(query);
229229
}
230230
}
@@ -263,7 +263,7 @@ member variable. The array of exceptions can be supplied as the last argument as
263263
{
264264
var spaceState = GetWorld2D().DirectSpaceState;
265265
var query = PhysicsRayQueryParameters2D.Create(globalPosition, targetPosition,
266-
CollisionMask, new Godot.Collections.Array<Rid> { GetRid() });
266+
CollisionMask, [GetRid()]);
267267
var result = spaceState.IntersectRay(query);
268268
}
269269
}

tutorials/scripting/c_sharp/c_sharp_exports.rst

+6-6
Original file line numberDiff line numberDiff line change
@@ -518,12 +518,12 @@ The default value of Godot arrays is null. A different default can be specified:
518518
.. code-block:: csharp
519519
520520
[Export]
521-
public Godot.Collections.Array<string> CharacterNames { get; set; } = new Godot.Collections.Array<string>
522-
{
521+
public Godot.Collections.Array<string> CharacterNames { get; set; } =
522+
[
523523
"Rebecca",
524524
"Mary",
525525
"Leah",
526-
};
526+
];
527527
528528
Arrays with specified types which inherit from resource can be set by
529529
drag-and-dropping multiple files from the FileSystem dock.
@@ -588,11 +588,11 @@ The default value of C# arrays is null. A different default can be specified:
588588
.. code-block:: csharp
589589
590590
[Export]
591-
public Vector3[] Vectors { get; set; } = new Vector3[]
592-
{
591+
public Vector3[] Vectors { get; set; } =
592+
[
593593
new Vector3(1, 2, 3),
594594
new Vector3(3, 2, 1),
595-
}
595+
];
596596
597597
Setting exported variables from a tool script
598598
---------------------------------------------

tutorials/scripting/cross_language_scripting.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ to said method.
212212
// Outputs "Hello there!" twice, once per line.
213213
myGDScriptNode.Call("print_n_times", "Hello there!", 2);
214214
215-
string[] arr = new string[] { "a", "b", "c" };
215+
string[] arr = ["a", "b", "c"];
216216
// Output: "a", "b", "c" (one per line).
217217
myGDScriptNode.Call("print_array", arr);
218218
// Output: "1", "2", "3" (one per line).

0 commit comments

Comments
 (0)