You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/unity/en/tutorial/js2cs.md
+34Lines changed: 34 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -111,6 +111,40 @@ Done easily!
111
111
> It should be noted that you may think, "TypeScript supports generics, why not use them?" Unfortunately, TypeScript generics are only a compile-time concept. At runtime, JavaScript is still running, so puer.$generic is still needed to handle them.
112
112
113
113
114
+
----------------------------
115
+
### Array & Indexer Access
116
+
117
+
C#'s `[]` operator (including array indexing, List indexing, Dictionary indexing, and any custom indexer) **cannot** be used directly with `[]` syntax in JS. You must use `get_Item()` / `set_Item()` methods instead:
let arr = CS.System.Array.CreateInstance(puer.$typeof(CS.System.Int32), 3);
125
+
arr.set_Item(0, 42); // equivalent to C#: arr[0] = 42
126
+
let val = arr.get_Item(0); // equivalent to C#: val = arr[0]
127
+
console.log(val); // 42
128
+
129
+
// Same for List<T>
130
+
let List = puer.$generic(CS.System.Collections.Generic.List$1, CS.System.Int32);
131
+
let lst = new List();
132
+
lst.Add(10);
133
+
let first = lst.get_Item(0); // equivalent to C#: lst[0]
134
+
lst.set_Item(0, 20); // equivalent to C#: lst[0] = 20
135
+
136
+
// Same for Dictionary<TKey, TValue>
137
+
let Dict = puer.$generic(CS.System.Collections.Generic.Dictionary$2, CS.System.String, CS.System.Int32);
138
+
let dict = new Dict();
139
+
dict.set_Item('key', 100); // equivalent to C#: dict['key'] = 100
140
+
let v = dict.get_Item('key'); // equivalent to C#: v = dict['key']
141
+
");
142
+
env.Dispose();
143
+
}
144
+
```
145
+
146
+
> ⚠️ **Important**: This is a key difference between JS and C#. JS's `[]` operator only works on native JS objects. For C# objects, you must use `get_Item()` / `set_Item()` methods for indexed access.
147
+
114
148
----------------------------
115
149
### typeof and operator overload
116
150
In addition to these special usages, there are two more situations to introduce: typeof function and operator overload:
Copy file name to clipboardExpand all lines: doc/unity/en/tutorial/lang-comparison.md
+20-4Lines changed: 20 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -136,7 +136,21 @@ func(obj)
136
136
137
137
---
138
138
139
-
## 10. typeof
139
+
## 10. Array & Indexer Access (`[]` Operator)
140
+
141
+
C#'s `[]` operator (arrays, Lists, Dictionaries, custom indexers) **cannot** be used directly with `[]` in any of the three languages. You must use `get_Item()` / `set_Item()` methods instead.
> ⚠️ Lua uses colon `:` syntax (instance method), while JS and Python use dot `.` syntax. This rule applies to all C# types with indexers (arrays, List, Dictionary, etc.).
C#'s `[]` operator (including array indexing, List indexing, Dictionary indexing, and any custom indexer) **cannot** be used directly with `[]` syntax in Lua. You must use `get_Item()` / `set_Item()` methods instead (note the colon syntax):
local arr = CS.System.Array.CreateInstance(typeof(CS.System.Int32), 3)
227
+
arr:set_Item(0, 42) -- equivalent to C#: arr[0] = 42
228
+
local val = arr:get_Item(0) -- equivalent to C#: val = arr[0]
229
+
print(val) -- 42
230
+
231
+
-- Same for List<T>
232
+
local List = puerts.generic(CS.System.Collections.Generic.List_1, CS.System.Int32)
233
+
local lst = List()
234
+
lst:Add(10)
235
+
local first = lst:get_Item(0) -- equivalent to C#: lst[0]
236
+
lst:set_Item(0, 20) -- equivalent to C#: lst[0] = 20
237
+
238
+
-- Same for Dictionary<TKey, TValue>
239
+
local Dict = puerts.generic(CS.System.Collections.Generic.Dictionary_2, CS.System.String, CS.System.Int32)
240
+
local dict = Dict()
241
+
dict:set_Item('key', 100) -- equivalent to C#: dict['key'] = 100
242
+
local v = dict:get_Item('key') -- equivalent to C#: v = dict['key']
243
+
");
244
+
env.Dispose();
245
+
}
246
+
```
247
+
248
+
> ⚠️ **Important**: Since `get_Item` / `set_Item` are instance methods, you must use **colon syntax**`:` in Lua. Lua's native `[]` only works on Lua tables. For indexed access on C# objects, you must use these two methods.
Copy file name to clipboardExpand all lines: doc/unity/en/tutorial/python2cs.md
+39Lines changed: 39 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -248,6 +248,45 @@ print(Inner.i) # 3
248
248
249
249
> 💡 When using `puerts.load_type()` to access nested types, use the C# reflection `+` separator format (e.g. `OuterClass+InnerClass`).
250
250
251
+
----------------------------
252
+
### Array & Indexer Access
253
+
254
+
C#'s `[]` operator (including array indexing, List indexing, Dictionary indexing, and any custom indexer) **cannot** be used directly with `[]` syntax in Python. You must use `get_Item()` / `set_Item()` methods instead:
255
+
256
+
```csharp
257
+
void Start() {
258
+
var env = new Puerts.ScriptEnv(new Puerts.BackendPython());
259
+
env.Eval(@"
260
+
exec('''
261
+
import System
262
+
from System.Collections.Generic import List_1, Dictionary_2
d.set_Item('key', 100) # equivalent to C#: dict['key'] = 100
281
+
v = d.get_Item('key') # equivalent to C#: v = dict['key']
282
+
''')
283
+
");
284
+
env.Dispose();
285
+
}
286
+
```
287
+
288
+
> ⚠️ **Important**: Although Python's native `[]` operator works on lists and dicts, for indexed access on C# objects, you must use `get_Item()` / `set_Item()` methods. This rule is consistent across JS, Lua, and Python in PuerTS.
0 commit comments