S&box Wiki

Revision Difference

From_Lua_to_CSharp#530485

<cat>Code.Misc</cat>⤶ <title>From Lua to C#</title>⤶ ⤶ <deprecated>[this page](https://wiki.facepunch.com/sbox/Learning_Resources)</deprecated>⤶ ⤶ <warning>⤶ This page won't teach you how to use C#. In order to effectively use C#, please check out [this page](https://wiki.facepunch.com/sbox/Learning_Resources).⤶ </warning>⤶ ⤶ # What is this page⤶ This is meant to give an idea of some major differences between Lua and C#.⤶ ⤶ This isn't a complete guide to C# and it doesn't cover many of the topics. ⤶ It's meant to be **brief, simple and illustrative**.⤶ ⤶ If you want to learn C# in more depth, please check out [this page](https://wiki.facepunch.com/sbox/Learning_Resources).⤶ ⤶ # Terminology⤶ ⤶ ## Tables⤶ Tables do not exist in C#: although they are not exactly a replacement, in most cases you will use *classes* instead.⤶ ⤶ ## Variables⤶ In C# only variables defined in methods are called so. ⤶ If it's defined in a class it is called a *field* instead.⤶ ⤶ Example:⤶ ```csharp⤶ public class Player⤶ {⤶ private int _health; // field⤶ ⤶ public void Damage( int amount, int times )⤶ {⤶ var totalAmount = amount * times; // variable⤶ }⤶ }⤶ ```⤶ ⤶ ## Functions ⤶ In C# they are called *methods* instead.⤶ ⤶ # Comments⤶ ```lua⤶ -- Lua comments start with two minus signs⤶ ⤶ --[[⤶ Lua multiline comment⤶ example⤶ --]]⤶ ```⤶ ⤶ ```csharp⤶ // C# comments start with two slashes, like most other languages⤶ ⤶ /*⤶ C# multiline comment⤶ example⤶ */⤶ ```⤶ ⤶ # Typing System⤶ Lua uses dynamic typing. That means variables do not have a specific type and can be assigned any type of value.⤶ ⤶ ```lua⤶ -- variables in Lua don't have a static type and can store any data⤶ local a = 100⤶ a = "Hello"⤶ a = function() print( "Hi" ) end⤶ ```⤶ ⤶ C# uses static typing. That means a variable has a known type that is set during its declaration. Only compatible values can be assigned. For example, you cannot assign a string value to an int variable.⤶ ```csharp⤶ int a = 100;⤶ a = "Hello"; // error: can't assign a string to an int⤶ ```⤶ ⤶ There are multiple built in data types in C#. You can view all of them [here](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/built-in-types)⤶ ⤶ You can also use the `var` keyword for the type to be inferred. This is considered bad practice to be used constantly and should only be used when defining explicit types.⤶ ⤶ ```csharp⤶ var veh = new Vehicle(); // veh will have the type of Vehicle⤶ ```⤶ ⤶ ## Type Conversion⤶ ```lua⤶ local a = 123⤶ local b = tostring( a ) -- "123"⤶ local c = tonumber( b ) -- 123⤶ ```⤶ ⤶ ```csharp⤶ int a = 123;⤶ string b = a.ToString(); // "123"⤶ int 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⤶ Vehicle vehicle = new Car() as Vehicle; // downcast Car reference to Vehicle⤶ Car car = vehicle as Car; // upcast Vehicle reference to Car⤶ ```⤶ ⤶ # Strings⤶ ```lua⤶ 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 )⤶ ```⤶ ⤶ ```csharp⤶ string str = "Hello";⤶ char singleChar = 'a'; // single quotes are for a single character, not a string⤶ ⤶ // While there are many ways to concatenate a string, this is the preferred method.⤶ // putting '$' before a string allows interpolating values into it using '{value}'⤶ int appleCount = 3;⤶ string interpolated = $"I have {appleCount} apples!"; ⤶ ```⤶ ⤶ # Loops⤶ ```lua⤶ for i = 1, 10, 2 do⤶ print( i ) -- 1, 3, 5, 7, 9⤶ end⤶ ⤶ local a = 10⤶ ⤶ repeat⤶ print( a ) -- 10, 8, 6, 4, 2, 0⤶ a = a - 2⤶ until a < 0⤶ ⤶ local t = {⤶ a = 5,⤶ b = 10,⤶ c = 15⤶ }⤶ ⤶ for k, v in pairs( t ) do⤶ print( k .. ' = ' .. v ) -- b = 10, a = 5, c = 15⤶ end⤶ ```⤶ ⤶ ```csharp⤶ for ( int i = 1; i < 10; i += 2 )⤶ {⤶ Console.WriteLine( i ); // 1, 3, 5, 7, 9⤶ }⤶ ⤶ int a = 10;⤶ do⤶ {⤶ Console.WriteLine( a ); // 10, 8, 6, 4, 2, 0⤶ a -= 2;⤶ } while ( a >= 0 );⤶ ⤶ Dictionary<string, int> t = new Dictionary<string, int>⤶ {⤶ { "a", 5 },⤶ { "b", 10 },⤶ { "c", 15 },⤶ };⤶ ⤶ foreach ( KeyValuePair<string, int> kv in t )⤶ {⤶ Console.WriteLine( $"{kv.Key} = {kv.Value}" ); //a = 5, b = 10, c = 15⤶ }⤶ ```