s&box

From Lua to C#

What is this guide

This is meant to give an idea of the major differences between Lua and C#.

Comments

-- Lua comments start with two minus signs --[[ Lua multiline comment example --]]
// C# comments start with two slashes, like most other languages /* C# multiline comment example */

Global Variables

-- you can define globals anywhere in Lua globalVar = 100
public class MyClass { // in C# globals need to be in a class public static int GlobalVar = 100; }

Variable Types

-- variables in Lua don't have a static type and can store any data local a = 100 a = "Hello" a = function() print( "Hi" ) end
var a = 100; // translated as "int a = 100;" because 100 is an int a = "Hello"; // error: can't assign a string to an int

Type conversion

local a = 123 local b = tostring( a ) -- "123" local c = tonumber( b ) -- 123
var a = 123; var b = a.ToString(); // "123" var c = int.Parse(b); // 123, will throw an exception if 'b' isn't int-ish // operator 'as' converts an object reference or returns 'null' on failure var vehicle = new Car() as Vehicle; // downcast Car reference to Vehicle var vehicle = vehicle as Car; // upcast Vehicle reference to Car

Numbers

local a = 100 a = 5.5 a = -300.1
// integer types are sbyte, byte, short, ushort, int, uint, long, ulong int a = -300; uint b = 333333; // decimal types are float, double, decimal double d = -500.888;

Strings

local str = "Hello" local str2 = 'Hello2' local multilineStr = [[ Hello Multiline ]] print(#str) -- 5, string length local concated = "I have " .. 3 .. " apples!" local formatted = string.format( "I have %d apples!", 3 )
var str = "Hello"; var singleChar = 'a'; // single quotes are for a single character, not a string var multilineStr = @" Hello Multiline "; // putting '@' before a string literal makes it a verbatim string literal Console.WriteLine(str.Length); // 5, string length var concated = "I have " + 3 + " apples!"; // {0} and {1} here correspond to the order of value arguments // argument number 0 is '3' and argument number 1 is '2' var formatted = string.Format("I have {0} apples and {1} oranges!", 3, 2); // putting '$' before a string allows interpolating values into it using '{value}' var interpolated = $"I have {3} apples!";

Collections

local stuff = { 1, 2, 3, "Oh", "Hi", "Mark", { myFunc = function() end } }
var numbers = new int[3] { 1, 2, 3 }; var strings = new List<string> { "Oh", "Hi", "Mark" }; var functions = new Dictionary<string, Action> { ["myFunc"] = () => { } };

Member Access

In Lua anything is accessible from anywhere as long as you have a reference.

-- myFile.lua myTable = { field = 100 }
-- otherFile.lua print( myTable.field ) -- 100

In C# things can be public, private, protected and internal.

// MyClass.cs public class MyClass { public static void PublicMethod() { ... } protected static void ProtectedMethod() { ... } private static void PrivateMethod() { ... } }
// OtherClass.cs public class OtherClass : MyClass { public static void Test() { MyClass.PublicMethod(); // OK MyClass.ProtectedMethod(); // we inherit from MyClass so OK MyClass.PrivateMethod(); // error: PrivateMethod is private } }

Page Links


Special Pages


Wikis

?

Render Time: 70ms

DB GetPage 45
Generate Html 6
SaveChanges (1) 17
Render Body 0
Render Sidebar 0