Skip to content

Commit b847924

Browse files
committed
添加数组,索引的访问文档
1 parent 7e89629 commit b847924

File tree

8 files changed

+262
-8
lines changed

8 files changed

+262
-8
lines changed

doc/unity/en/tutorial/js2cs.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,40 @@ Done easily!
111111
> 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.
112112
113113

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:
118+
119+
```csharp
120+
void Start() {
121+
var env = new Puerts.ScriptEnv(new Puerts.BackendV8());
122+
env.Eval(@"
123+
// Create a C# array
124+
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+
114148
----------------------------
115149
### typeof and operator overload
116150
In addition to these special usages, there are two more situations to introduce: typeof function and operator overload:

doc/unity/en/tutorial/lang-comparison.md

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,21 @@ func(obj)
136136

137137
---
138138

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.
142+
143+
| Language | Read (C# `arr[0]`) | Write (C# `arr[0] = val`) |
144+
|----------|--------------------|-----------------------------|
145+
| **JavaScript** | `arr.get_Item(0)` | `arr.set_Item(0, val)` |
146+
| **Lua** | `arr:get_Item(0)` | `arr:set_Item(0, val)` |
147+
| **Python** | `arr.get_Item(0)` | `arr.set_Item(0, val)` |
148+
149+
> ⚠️ 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.).
150+
151+
---
152+
153+
## 11. typeof
140154

141155
| Language | Syntax |
142156
|----------|--------|
@@ -146,7 +160,7 @@ func(obj)
146160

147161
---
148162

149-
## 11. null Representation
163+
## 12. null Representation
150164

151165
| Language | Script-side null | Notes |
152166
|----------|-----------------|-------|
@@ -156,7 +170,7 @@ func(obj)
156170

157171
---
158172

159-
## 12. Callbacks / Lambda
173+
## 13. Callbacks / Lambda
160174

161175
| Language | Example |
162176
|----------|---------|
@@ -174,7 +188,7 @@ fn("hello");
174188

175189
---
176190

177-
## 13. Throwing Exceptions
191+
## 14. Throwing Exceptions
178192

179193
| Language | Syntax |
180194
|----------|--------|
@@ -196,6 +210,8 @@ fn("hello");
196210
| ref/out create | `puer.$ref()` | `{}` (table) | `[]` (list) |
197211
| ref/out retrieve | `puer.$unref()` | `[1]` | `[0]` |
198212
| async/await | `puer.$promise(task)` |||
213+
| Indexer read `[]` | `obj.get_Item(idx)` | `obj:get_Item(idx)` | `obj.get_Item(idx)` |
214+
| Indexer write `[]` | `obj.set_Item(idx, val)` | `obj:set_Item(idx, val)` | `obj.set_Item(idx, val)` |
199215

200216
---
201217

doc/unity/en/tutorial/lua2cs.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,44 @@ GenericTestClass.Inner()
209209
print(GenericTestClass.Inner.stringProp) -- 'hello'
210210
```
211211

212+
----------------------------
213+
### Array & Indexer Access
214+
215+
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):
216+
217+
```csharp
218+
void Start() {
219+
var env = new Puerts.ScriptEnv(new Puerts.BackendLua());
220+
env.Eval(@"
221+
local CS = require('csharp')
222+
local puerts = require('puerts')
223+
local typeof = puerts.typeof
224+
225+
-- Create a C# array
226+
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.
249+
212250
----------------------------
213251
### typeof
214252

doc/unity/en/tutorial/python2cs.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,45 @@ print(Inner.i) # 3
248248
249249
> 💡 When using `puerts.load_type()` to access nested types, use the C# reflection `+` separator format (e.g. `OuterClass+InnerClass`).
250250
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
263+
264+
# Create a C# array
265+
arr = System.Array.CreateInstance(puerts.typeof(System.Int32), 3)
266+
arr.set_Item(0, 42) # equivalent to C#: arr[0] = 42
267+
val = arr.get_Item(0) # equivalent to C#: val = arr[0]
268+
print(val) # 42
269+
270+
# Same for List<T>
271+
ListInt = puerts.generic(List_1, System.Int32)
272+
lst = ListInt()
273+
lst.Add(10)
274+
first = lst.get_Item(0) # equivalent to C#: lst[0]
275+
lst.set_Item(0, 20) # equivalent to C#: lst[0] = 20
276+
277+
# Same for Dictionary<TKey, TValue>
278+
DictStrInt = puerts.generic(Dictionary_2, System.String, System.Int32)
279+
d = DictStrInt()
280+
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.
289+
251290
----------------------------
252291
### typeof
253292

doc/unity/zhcn/tutorial/js2cs.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,40 @@ void Start() {
111111

112112
> 需要注意的是,可能你会想“Typescript明明支持泛型,为什么不用上呢?“。遗憾的是,Typescript泛型只是一个编译时的概念,在实际运行的时候还是运行的是Javascript,因此还是需要puer.$generic来处理。
113113
114+
----------------------------
115+
### 数组与索引器访问
116+
117+
C# 中的 `[]` 操作符(包括数组索引、List 索引、Dictionary 索引以及任何自定义索引器)在 JS 中**不能**直接使用 `[]` 语法访问,必须使用 `get_Item()` / `set_Item()` 方法:
118+
119+
```csharp
120+
void Start() {
121+
var env = new Puerts.ScriptEnv(new Puerts.BackendV8());
122+
env.Eval(@"
123+
// 创建 C# 数组
124+
let arr = CS.System.Array.CreateInstance(puer.$typeof(CS.System.Int32), 3);
125+
arr.set_Item(0, 42); // 等价于 C# 的 arr[0] = 42
126+
let val = arr.get_Item(0); // 等价于 C# 的 val = arr[0]
127+
console.log(val); // 42
128+
129+
// 同样适用于 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); // 等价于 C# 的 lst[0]
134+
lst.set_Item(0, 20); // 等价于 C# 的 lst[0] = 20
135+
136+
// 同样适用于 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); // 等价于 C# 的 dict['key'] = 100
140+
let v = dict.get_Item('key'); // 等价于 C# 的 v = dict['key']
141+
");
142+
env.Dispose();
143+
}
144+
```
145+
146+
> ⚠️ **重要**:这是 JS 和 C# 之间的一个关键差异。JS 的 `[]` 操作符只能用于 JS 原生对象,对于 C# 对象的索引访问必须通过 `get_Item()` / `set_Item()` 方法。
147+
114148
----------------------------
115149
### typeof与运算符重载
116150
除了这上述特殊的用法之外,还要介绍两种情况:typeof函数和运算符重载:

doc/unity/zhcn/tutorial/lang-comparison.md

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,21 @@ func(obj)
136136

137137
---
138138

139-
## 10. typeof
139+
## 10. 数组与索引器访问(`[]` 操作符)
140+
141+
C# 的 `[]` 操作符(数组、List、Dictionary、自定义索引器)在三种语言中都**不能**直接使用 `[]`,必须使用 `get_Item()` / `set_Item()` 方法。
142+
143+
| 语言 | 读取(C# `arr[0]`| 写入(C# `arr[0] = val`|
144+
|------|---------------------|--------------------------|
145+
| **JavaScript** | `arr.get_Item(0)` | `arr.set_Item(0, val)` |
146+
| **Lua** | `arr:get_Item(0)` | `arr:set_Item(0, val)` |
147+
| **Python** | `arr.get_Item(0)` | `arr.set_Item(0, val)` |
148+
149+
> ⚠️ Lua 使用冒号 `:` 语法(实例方法),JS 和 Python 使用点号 `.` 语法。此规则适用于所有带索引器的 C# 类型(数组、List、Dictionary 等)。
150+
151+
---
152+
153+
## 11. typeof
140154

141155
| 语言 | 语法 |
142156
|------|------|
@@ -146,7 +160,7 @@ func(obj)
146160

147161
---
148162

149-
## 11. null 表示
163+
## 12. null 表示
150164

151165
| 语言 | 脚本侧 null | 说明 |
152166
|------|-------------|------|
@@ -156,7 +170,7 @@ func(obj)
156170

157171
---
158172

159-
## 12. 回调函数 / Lambda
173+
## 13. 回调函数 / Lambda
160174

161175
| 语言 | 示例 |
162176
|------|------|
@@ -174,7 +188,7 @@ fn("hello");
174188

175189
---
176190

177-
## 13. 异常抛出
191+
## 14. 异常抛出
178192

179193
| 语言 | 语法 |
180194
|------|------|
@@ -196,6 +210,8 @@ fn("hello");
196210
| ref/out 创建 | `puer.$ref()` | `{}` (table) | `[]` (list) |
197211
| ref/out 取值 | `puer.$unref()` | `[1]` | `[0]` |
198212
| async/await | `puer.$promise(task)` |||
213+
| 索引器读取 `[]` | `obj.get_Item(idx)` | `obj:get_Item(idx)` | `obj.get_Item(idx)` |
214+
| 索引器写入 `[]` | `obj.set_Item(idx, val)` | `obj:set_Item(idx, val)` | `obj.set_Item(idx, val)` |
199215

200216
---
201217

doc/unity/zhcn/tutorial/lua2cs.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,44 @@ GenericTestClass.Inner()
209209
print(GenericTestClass.Inner.stringProp) -- 'hello'
210210
```
211211

212+
----------------------------
213+
### 数组与索引器访问
214+
215+
C# 中的 `[]` 操作符(包括数组索引、List 索引、Dictionary 索引以及任何自定义索引器)在 Lua 中**不能**直接使用 `[]` 语法访问,必须使用 `get_Item()` / `set_Item()` 方法(注意使用冒号语法):
216+
217+
```csharp
218+
void Start() {
219+
var env = new Puerts.ScriptEnv(new Puerts.BackendLua());
220+
env.Eval(@"
221+
local CS = require('csharp')
222+
local puerts = require('puerts')
223+
local typeof = puerts.typeof
224+
225+
-- 创建 C# 数组
226+
local arr = CS.System.Array.CreateInstance(typeof(CS.System.Int32), 3)
227+
arr:set_Item(0, 42) -- 等价于 C# 的 arr[0] = 42
228+
local val = arr:get_Item(0) -- 等价于 C# 的 val = arr[0]
229+
print(val) -- 42
230+
231+
-- 同样适用于 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) -- 等价于 C# 的 lst[0]
236+
lst:set_Item(0, 20) -- 等价于 C# 的 lst[0] = 20
237+
238+
-- 同样适用于 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) -- 等价于 C# 的 dict['key'] = 100
242+
local v = dict:get_Item('key') -- 等价于 C# 的 v = dict['key']
243+
");
244+
env.Dispose();
245+
}
246+
```
247+
248+
> ⚠️ **重要**:由于 `get_Item` / `set_Item` 是实例方法,在 Lua 中必须使用**冒号语法** `:` 调用。Lua 原生的 `[]` 只适用于 Lua table,对 C# 对象的索引访问必须通过这两个方法。
249+
212250
----------------------------
213251
### typeof
214252

doc/unity/zhcn/tutorial/python2cs.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,45 @@ print(Inner.i) # 3
276276
277277
> 💡 使用 `puerts.load_type()` 访问嵌套类型时,使用 C# 反射的 `+` 分隔符格式(如 `OuterClass+InnerClass`)。
278278
279+
----------------------------
280+
### 数组与索引器访问
281+
282+
C# 中的 `[]` 操作符(包括数组索引、List 索引、Dictionary 索引以及任何自定义索引器)在 Python 中**不能**直接使用 `[]` 语法访问,必须使用 `get_Item()` / `set_Item()` 方法:
283+
284+
```csharp
285+
void Start() {
286+
var env = new Puerts.ScriptEnv(new Puerts.BackendPython());
287+
env.Eval(@"
288+
exec('''
289+
import System
290+
from System.Collections.Generic import List_1, Dictionary_2
291+
292+
# 创建 C# 数组
293+
arr = System.Array.CreateInstance(puerts.typeof(System.Int32), 3)
294+
arr.set_Item(0, 42) # 等价于 C# 的 arr[0] = 42
295+
val = arr.get_Item(0) # 等价于 C# 的 val = arr[0]
296+
print(val) # 42
297+
298+
# 同样适用于 List<T>
299+
ListInt = puerts.generic(List_1, System.Int32)
300+
lst = ListInt()
301+
lst.Add(10)
302+
first = lst.get_Item(0) # 等价于 C# 的 lst[0]
303+
lst.set_Item(0, 20) # 等价于 C# 的 lst[0] = 20
304+
305+
# 同样适用于 Dictionary<TKey, TValue>
306+
DictStrInt = puerts.generic(Dictionary_2, System.String, System.Int32)
307+
d = DictStrInt()
308+
d.set_Item('key', 100) # 等价于 C# 的 dict['key'] = 100
309+
v = d.get_Item('key') # 等价于 C# 的 v = dict['key']
310+
''')
311+
");
312+
env.Dispose();
313+
}
314+
```
315+
316+
> ⚠️ **重要**:尽管 Python 原生的 `[]` 运算符用于列表和字典,但对于 C# 对象的索引访问,必须通过 `get_Item()` / `set_Item()` 方法。这一点与 JS 和 Lua 中的规则一致。
317+
279318
----------------------------
280319
### typeof
281320

0 commit comments

Comments
 (0)