การเปรียบเทียบ Luau และ C#

*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน 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 เป็นเวกเตอร์เหมือนใน C# ดัชนีเริ่มต้นที่ 1 ใน Luau และ 0 ใน C#

ตารางที่จัดเรียงตามตัวเลขใน 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 คุณสามารถเขียนตัวแปรและโลจิสติกในขอบเขตที่แคบกว่าฟังก์ชันหรือคลาสของพวกเขาโดยนิ่งโลจิสติกภายใน และ คีย์เวิร์ดที่คล้ายกับเครื่องหมายเคลือบเกลียวใน 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 ดู โครงสร้างการควบคุม

ในขณะที่และทำซ้ำลูป

ในขณะที่และทำซ้ำลูปใน Luau

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
}