[問題] 如何在C#實現JS的 callBack ?

看板C_Sharp作者 (石理克)時間7年前 (2017/06/14 10:59), 編輯推噓3(306)
留言9則, 3人參與, 最新討論串1/1
問題網頁版: https://goo.gl/uPmWK4 問題圖片版: https://goo.gl/lmLtoh 為了從別人的程式碼學習 c# ,我正試著將一個JS程式重寫成C#。 其中這段 eachCell 方法不知道在 C# 中該如何實現... 煩請各位大大給予建議與指教,謝謝! // Get available cells in Grid ========================= Grid.prototype.availableCells = function () { var cells = []; this.eachCell(function (x, y, tile) { if (!tile) { cells.push({ x: x, y: y }); } }); return cells; }; // Call callback for every cell========================= Grid.prototype.eachCell = function (callback) { for (var x = 0; x < this.size; x++) { for (var y = 0; y < this.size; y++) { callback(x, y, this.cells[x][y]); } } }; -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 60.251.182.68 ※ 文章網址: https://www.ptt.cc/bbs/C_Sharp/M.1497409158.A.C46.html

06/14 11:46, , 1F
就是你底下寫的這個啊Grid.prototype.eachCell
06/14 11:46, 1F

06/14 11:47, , 2F
看懂你的問題了 你可以google 委派
06/14 11:47, 2F

06/14 13:02, , 3F
恩 其實不太會用委派解我這狀況
06/14 13:02, 3F

06/14 14:18, , 4F
06/14 14:18, 4F

06/14 14:26, , 5F
不宣告delegate的話可以用Func
06/14 14:26, 5F

06/14 14:26, , 6F

06/14 15:25, , 7F
喔喔!來試試看
06/14 15:25, 7F

06/14 23:33, , 8F
關鍵字 func<> , action<>, linq
06/14 23:33, 8F

06/15 13:11, , 9F
知道怎樣用lambda解了!感謝大家提供指教
06/15 13:11, 9F
文章代碼(AID): #1PGAQ6n6 (C_Sharp)