Luau 和 C# 比较

*此内容使用人工智能(Beta)翻译,可能包含错误。若要查看英文页面,请点按 此处

Roblox 使用 Luau 编程语言。以下代码示例和表格显示了 C# 和 Luau 语法之间的一些差异。

行结束

你不需要在 Luau 中使用分号,但它们不会破坏语法。

保留关键字

下表显示了 Luau 的保留关键字映射到其 C# 等价物。请注意,它不显示所有的 C# 关键字。

卢奥C#
and
breakbreak
dodo
ifif
elseelse
elseifelse if
then
end
truetrue
falsefalse
forfor or foreach
function
inin
local
nilnull
not
or
repeat
returnreturn
until
whilewhile

评论

在 Luau 中的评论

-- 单行评论
--[[ 结果输出:
Block comment
--]]
Comments in C#

// Single line comment
/*
Block comment
*/

线程

Luau 中的字符串

-- 多行文字符串
local multiLineString = [[This is a string that,
when printed, appears
on multiple lines]]
-- 连接方式
local s1 = "This is a string "
local s2 = "made with two parts."
local endString = s1 .. s2
Strings in C#

// Multi-line string
string multiLineString1 = "This is a string that,\nwhen printed, appears\n on multiple lines.";
string multiLineString2 = @"This is a string that,
when printed, appears
on multiple lines";
// Concatenation
string s1 = "This is a string ";
string s2 = "made with two parts.";
string endString = s1 + s2;

表格

要了解更多关于 Luau 中的表的信息,请参阅 表格

词典表

您可以在 Luau 中使用表作为词典,与 C# 相同。

Luau 中的词典表

local dictionary = {
val1 = "this",
val2 = "is"
}
print(dictionary.val1) -- 输出“这”
print(dictionary["val1"]) -- 输出“这”
dictionary.val1 = nil -- 从表中删除 'val1'
dictionary["val3"] = "a dictionary" -- Overwrites 'val3' or sets new key-value pair
Dictionary Tables in C#

Dictionary dictionary = new Dictionary()
{
{ "val1", "this" },
{ "val2", "is" }
};
Console.WriteLine(dictionary["val1"]); // Outputs 'this'
dictionary.Remove("val1"); // Removes 'val1' from dictionary
dictionary["val3"] = "a dictionary"; // Overwrites 'val3' or sets new key-value pair
dictionary.Add("val3", "a dictionary"); // Creates a new key-value pair

数字索引的表

您可以在 Luau 中使用表作为 array,与 C# 中一样。索引在 Luau 中开始于 1 ,在 C# 中开始于 0

在 Luau 中数字索引表

local npcAttributes = {"strong", "intelligent"}
print(npcAttributes[1]) -- 输出 '强'
print(#npcAttributes) -- 输出列表的大小
-- 添加到列表
table.insert(npcAttributes, "humble")
-- 另一种方式…
npcAttributes[#npcAttributes+1] = "humble"
-- 在列表的开头插入
table.insert(npcAttributes, 1, "brave")
-- 在给定的索引中删除项目
table.remove(npcAttributes, 3)
Numerically-Indexed Tables in C#

List npcAttributes = new List{"strong", "intelligent"};
Console.WriteLine(npcAttributes[0]); // Outputs 'strong'
Console.WriteLine(npcAttributes.Count); // Outputs the size of the list
// Append to the list
npcAttributes.Add("humble");
// Another way...
npcAttributes.Insert(npcAttributes.Count, "humble");
// Insert at the beginning of the list
npcAttributes.Insert(0, "brave");
// Remove item at a given index
npcAttributes.Remove(2);

运营商

条件运营者

操作员卢奥C#
等于====
大于>>
小于<<
大于或等于>=>=
小于或等于<=<=
不等于~=!=
and&&
Oror||

算术运算器

LuauC#
添加++
减法--
乘法**
分区//
模块%%
指数乘法^**

变量

在 Luau 中,变量在宣言时不指定其类型。虽然 Luau 变量没有访问修改器,但您可以用下划线为隐私人变量前缀,以便于阅读。

Luau 中的变量

local stringVariable = "value"
-- “公共”声明
local variableName
-- “私有”声明 - 以相同的方式解析
local _variableName
Variables in C#

string stringVariable = "value";
// Public declaration
public string variableName
// Private declaration
string variableName;

范围

在 Luau 中,您可以在更紧密的范围内写变量和逻辑,比其函数或类,通过在 doend 关键字内嵌逻辑来类似于 C# 中的扭曲括号 {} 。了解更多详情,请参阅范围

在 Luau 中定义范围

local outerVar = 'Outer scope text'
do
-- 修改“outerVar”
outerVar = 'Inner scope modified text'
-- 介绍本地变量
local innerVar = 'Inner scope text'
print('1: ' .. outerVar) -- 打印“1:内部范围修改文本”
print('2: ' .. innerVar) -- 打印“2:内部范围文本”
end
print('3: ' .. outerVar) -- 打印“3:”内部修改文本
-- Attempting to print 'innerVar' here would fail
Scoping in C#

var outerVar = "Outer scope text";
{
// Modify 'outerVar'
outerVar = "Inner scope modified text";
// Introduce a local variable
var innerVar = "Inner scope text";
Console.WriteLine("1: " + outerVar); // prints "1: Inner scope modified text"
Console.WriteLine("2: " + innerVar); // prints "2: Inner scope text"
}
Console.WriteLine("3: " + outerVar); // prints "3: "Inner scope modified text"
// Attempting to print 'innerVar' here would fail

条件语句

在 Luau 中的条件语句

-- 一个条件
if boolExpression then
doSomething()
end
-- 多个条件
if not boolExpression then
doSomething()
elseif otherBoolExpression then
doSomething()
else
doSomething()
end
Conditional Statements in C#

// One condition
if (boolExpression) {
doSomething();
}
// Multiple conditions
if (!boolExpression) {
doSomething();
}
else if (otherBoolExpression) {
doSomething();
}
else {
doSomething();
}

条件运营者

在 Luau 中的条件运营者

local max = if x > y then x else y
Conditional Operator in C#

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

循环

要了解 Luau 中循环的更多信息,请参阅 控制结构

while 和重复循环

在 Luau 中使用 while 和重复循环

while boolExpression do
doSomething()
end
repeat
doSomething()
until not boolExpression
While and Repeat Loops in C#

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

对于循环

在 Luau 中的通用循环

-- 向前循环
for i = 1, 10 do
doSomething()
end
-- 反向循环
for i = 10, 1, -1 do
doSomething()
end
Generic For Loops in C#

// Forward loop
for (int i = 1; i <= 10; i++) {
doSomething();
}
// Reverse loop
for (int i = 10; i >= 1; i--) {
doSomething();
}
在 Luau 上的循环过表

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
For Loops Over Lists in C#

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 中函数的更多信息,请参阅 函数

通用函数

在 Luau 中的通用函数

-- 通用函数
local function increment(number)
return number + 1
end
Generic Functions in C#

// Generic function
int increment(int number) {
return number + 1;
}

变量参数数量

Luau中变量参数数量

-- 变量参数数量
local function variableArguments(...)
print(...)
end
Variable Argument Number in C#

// Variable argument number
void variableArguments(params string[] inventoryItems) {
for (item in inventoryItems) {
Console.WriteLine(item);
}
}

命名参数

在 Luau 中命名参数

-- 命名参数
local function namedArguments(args)
return args.name .. "'s birthday: " .. args.dob
end
namedArguments{name="Bob", dob="4/1/2000"}
Named Arguments in C#

// Named arguments
string namedArguments(string name, string dob) {
return name + "'s birthday: " + dob;
}
namedArguments(name: "Bob", dob: "4/1/2000");

尝试捕获结构

在 Luau 尝试/捕捉结构

local function fireWeapon()
if not weaponEquipped then
error("No weapon equipped!")
end
-- 继续……
end
local success, errorMessage = pcall(fireWeapon)
if not success then
print(errorMessage)
end
Try/Catch Structures in C#

void fireWeapon() {
if (!weaponEquipped) {
// Use a user-defined exception
throw new InvalidWeaponException("No weapon equipped!");
}
// Proceed...
}
try {
fireWeapon();
} catch (InvalidWeaponException ex) {
// An error was raised
}