Luau 和 C# 的比較

*此內容是使用 AI(Beta 測試版)翻譯,可能含有錯誤。若要以英文檢視此頁面,請按一下這裡

Roblox 使用 Luau 程式語言。以下的程式碼範例和表格顯示了 C# 和 Luau 語法之間的一些差異。

行結尾

在 Luau 中不需要分號,但它們不會破壞語法。

保留關鍵字

以下表格將 Luau 的保留關鍵字映射到它們的 C# 等效項。請注意,這不是所有 C# 關鍵字的列表。

LuauC#
and
breakbreak
dodo
ifif
elseelse
elseifelse if
then
end
truetrue
falsefalse
forforforeach
function
inin
local
nilnull
not
or
repeat
returnreturn
until
whilewhile

註解

Luau中的註解

-- 單行註解
--[[ 結果輸出:
區塊註解
--]]
C#中的註解

// 單行註解
/*
區塊註解
*/

字串

Luau中的字串

-- 多行字串
local multiLineString = [[這是一個字串,
當打印時,顯示
在多行上]]
-- 字串連接
local s1 = "這是一個字串 "
local s2 = "由兩個部分組成。"
local endString = s1 .. s2
C#中的字串

// 多行字串
string multiLineString1 = "這是一個字串,\n當打印時,顯示\n在多行上。";
string multiLineString2 = @"這是一個字串,
當打印時,顯示
在多行上";
// 字串連接
string s1 = "這是一個字串 ";
string s2 = "由兩個部分組成。";
string endString = s1 + s2;

表格

要了解更多有關 Luau 中表格的內容,請參見 Tables

字典表格

您可以在 Luau 中像在 C# 中那樣使用表格作為字典。

Luau中的字典表格

local dictionary = {
val1 = "這個",
val2 = "是"
}
print(dictionary.val1) -- 輸出 '這個'
print(dictionary["val1"]) -- 輸出 '這個'
dictionary.val1 = nil -- 從表格中移除 'val1'
dictionary["val3"] = "一個字典" -- 覆蓋 'val3' 或設定新的鍵值對
C#中的字典表格

Dictionary dictionary = new Dictionary()
{
{ "val1", "這個" },
{ "val2", "是" }
};
Console.WriteLine(dictionary["val1"]); // 輸出 '這個'
dictionary.Remove("val1"); // 從字典中移除 'val1'
dictionary["val3"] = "一個字典"; // 覆蓋 'val3' 或設定新的鍵值對
dictionary.Add("val3", "一個字典"); // 創建新鍵值對

數字索引表格

您可以像在 C# 中那樣使用表格作為數組。Luau 中的索引從 1 開始,而 C# 中的索引從 0 開始。

Luau中的數字索引表格

local npcAttributes = {"強大的", "聰明的"}
print(npcAttributes[1]) -- 輸出 '強大的'
print(#npcAttributes) -- 輸出列表的大小
-- 附加到列表
table.insert(npcAttributes, "謙遜的")
-- 另一種方法...
npcAttributes[#npcAttributes+1] = "謙遜的"
-- 在列表的開頭插入
table.insert(npcAttributes, 1, "勇敢的")
-- 移除給定索引的項目
table.remove(npcAttributes, 3)
C#中的數字索引表格

List npcAttributes = new List{"強大的", "聰明的"};
Console.WriteLine(npcAttributes[0]); // 輸出 '強大的'
Console.WriteLine(npcAttributes.Count); // 輸出列表的大小
// 附加到列表
npcAttributes.Add("謙遜的");
// 另一種方法...
npcAttributes.Insert(npcAttributes.Count, "謙遜的");
// 在列表的開頭插入
npcAttributes.Insert(0, "勇敢的");
// 移除給定索引的項目
npcAttributes.Remove(2);

運算符

條件運算符

運算符LuauC#
等於====
大於>>
小於<<
大於或等於>=>=
小於或等於<=<=
不等於~=!=
and&&
or||

算術運算符

LuauC#
加法++
減法--
乘法**
除法//
模數%%
指數^**

變數

在 Luau 中,變數在宣告時不需要指定其類型。Luau 變數沒有訪問修飾符,儘管您可以在 "private" 變數前加上底線以提高可讀性。

Luau中的變數

local stringVariable = "值"
-- "公共" 宣告
local variableName
-- "私有" 宣告 - 解析方式相同
local _variableName
C#中的變數

string stringVariable = "值";
// 公共宣告
public string variableName
// 私有宣告
string variableName;

範圍

在 Luau 中,您可以將變數和邏輯寫在比其函數或類更緊湊的範圍內,通過將邏輯嵌套在 doend 關鍵字中,類似於 C# 中的花括號 {}。有關更多詳細資訊,請參見 Scope

Luau中的範圍

local outerVar = '外部範圍文本'
do
-- 修改 'outerVar'
outerVar = '內部範圍修改文本'
-- 引入一個局部變數
local innerVar = '內部範圍文本'
print('1: ' .. outerVar) -- 打印 "1: 內部範圍修改文本"
print('2: ' .. innerVar) -- 打印 "2: 內部範圍文本"
end
print('3: ' .. outerVar) -- 打印 "3: 內部範圍修改文本"
-- 嘗試在這裡打印 'innerVar' 將會失敗
C#中的範圍

var outerVar = "外部範圍文本";
{
// 修改 'outerVar'
outerVar = "內部範圍修改文本";
// 引入一個局部變數
var innerVar = "內部範圍文本";
Console.WriteLine("1: " + outerVar); // 打印 "1: 內部範圍修改文本"
Console.WriteLine("2: " + innerVar); // 打印 "2: 內部範圍文本"
}
Console.WriteLine("3: " + outerVar); // 打印 "3: 內部範圍修改文本"
// 嘗試在這裡打印 'innerVar' 將會失敗

條件語句

Luau中的條件語句

-- 一個條件
if boolExpression then
doSomething()
end
-- 多個條件
if not boolExpression then
doSomething()
elseif otherBoolExpression then
doSomething()
else
doSomething()
end
C#中的條件語句

// 一個條件
if (boolExpression) {
doSomething();
}
// 多個條件
if (!boolExpression) {
doSomething();
}
else if (otherBoolExpression) {
doSomething();
}
else {
doSomething();
}

條件運算符

Luau中的條件運算符

local max = if x > y then x else y
C#中的條件運算符

int max = (x > y) ? x : y;

循環

要了解更多有關 Luau 中循環的內容,請參見 Control Structures

While 和 repeat 循環

Luau中的While和Repeat循環

while boolExpression do
doSomething()
end
repeat
doSomething()
until not boolExpression
C#中的While和Repeat循環

while (boolExpression) {
doSomething();
}
do {
doSomething();
} while (boolExpression)

For 循環

Luau中的通用For循環

-- 正向循環
for i = 1, 10 do
doSomething()
end
-- 反向循環
for i = 10, 1, -1 do
doSomething()
end
C#中的通用For循環

// 正向循環
for (int i = 1; i <= 10; i++) {
doSomething();
}
// 反向循環
for (int i = 10; i >= 1; i--) {
doSomething();
}
Luau中的表格For循環

local abcList = {"a", "b", "c"}
for i, v in ipairs(abcList) do
print(v)
end
local abcDictionary = { a=1, b=2, c=3 }
for k, v in pairs(abcDictionary) do
print(k, v)
end
C#中的列表For循環

List<string> abcList = new List<string>{"a", "b", "c"};
foreach (string v in abcList) {
Console.WriteLine(v);
}
Dictionary<string, int> abcDictionary = new Dictionary<string, int>
{ {"a", 1}, {"b", 2}, {"c", 3} };
foreach (KeyValuePair<string, int> entry in abcDictionary) {
Console.WriteLine(entry.Key + " " + entry.Value);
}

Luau 也支持 通用迭代,進一步簡化了使用表格的操作。

函數

要了解更多有關 Luau 中函數的內容,請參見 Functions

通用函數

Luau中的通用函數

-- 通用函數
local function increment(number)
return number + 1
end
C#中的通用函數

// 通用函數
int increment(int number) {
return number + 1;
}

可變參數數量

Luau中的可變參數數量

-- 可變參數數量
local function variableArguments(...)
print(...)
end
C#中的可變參數數量

// 可變參數數量
void variableArguments(params string[] inventoryItems) {
for (item in inventoryItems) {
Console.WriteLine(item);
}
}

命名參數

Luau中的命名參數

-- 命名參數
local function namedArguments(args)
return args.name .. "的生日: " .. args.dob
end
namedArguments{name="Bob", dob="4/1/2000"}
C#中的命名參數

// 命名參數
string namedArguments(string name, string dob) {
return name + "的生日: " + dob;
}
namedArguments(name: "Bob", dob: "4/1/2000");

嘗試-捕獲結構

Luau中的嘗試-捕獲結構

local function fireWeapon()
if not weaponEquipped then
error("未裝備武器!")
end
-- 繼續...
end
local success, errorMessage = pcall(fireWeapon)
if not success then
print(errorMessage)
end
C#中的嘗試-捕獲結構

void fireWeapon() {
if (!weaponEquipped) {
// 使用用戶定義的異常
throw new InvalidWeaponException("未裝備武器!");
}
// 繼續...
}
try {
fireWeapon();
} catch (InvalidWeaponException ex) {
// 發生了一個錯誤
}
©2026 Roblox Corporation、Roblox、Roblox 標誌及 Powering Imagination 是我們在美國及其他國家地區的部分註冊與未註冊商標。