Revision Difference
From_Lua_to_CSharp#528892
<cat>Dev.Intro</cat>
<title>From Lua to C#</title>
<warning>This article is a work in progress. Please consider not making any changes to it at the moment.</warning>
# What is this guide
This is a C# starter guide meant for developers who are well familiar with Lua. There are some comparisons and code examples that will help you understand how to do things using C#.
## Why S&Box uses C# instead of Lua
While Lua is flexible to an extent, easy to learn and build simple systems with, it lacks certain capabilities for creating solid scalable projects which are offered by C#. Lua was the best option at the time when Garry's Mod came around, but now it's the time to make a step further.
# Comparison
Not all of these code examples will compile and run as-is, some constructs are missing or simplified for less distraction and easier understanding of covered topics.
# Top-level statements
Things you can write in a file on the top level
## Lua example
```lua
-- In lua you can have any code in a file on the top level,
-- it will get executed when the file is processed.
print('Hello!')
function foo()
return 100
end
print(foo())
myGlobalTable = {
hello = 'hello'
}
```
## C# example
```csharp
// Currently in C# all you can have on the top level is imports
// and type defenitions (classes, interfaces, enums, etc., possibly
// scoped in namespaces).
// You'll need to wrap all of your code into types, classes and their
// methods in order to actually do something. It's simple - on the top
// of the file you can only define things, but not allocate variables
// or execute expressions.
enum MyEnum
{
One,
Two
}
namespace Boo
{
public class Foo
{
public int myInt;
}
}
// The below code isn't allowed as a top-level expression and will not
// compile
int a = 100;
Something.DoStuff();
```
# Local and global variables
## Lua example
```lua
-- Global variales are accessed from anywhere in the code
-- Global variales are accessed from anywhere in the code
globalVar = 100
⤶
-- Local variables are accessed in the current lexical scope
⤶
-- Local variables are accessed in the current lexical scope
local localVar = 100
⤶
-- You can also use 'do' and 'end' on their own to delimit⤶
-- an arbitrary scoped code section⤶
do⤶
local scopedVar = 200⤶
end⤶
```
## C# example
```csharp
// MyClass.cs
// No global variables, if you want a globally-accessed variable
// you'll have to define it as a public static class member
public class MyClass
{
public static int staticVar = 100;
public static void DoStuff()
{
// This variable is local to the DoStuff method scope
int a = 200;
// You can also use '{' and '}' on their own to delimit
// an arbitrary scoped code section
{
// This variable is local to the current arbitrary scope
int a = 300;
}
Console.WriteLine(a); // 200
}
}
```
```csharp
// Example.cs
// We need to import the class in order to access its members
using MyClass;
public class Example
{
public void DoMoreStuff()
{
// This can be accessed from anywhere like this
int boo = MyClass.staticVar;
// This variable is local to the DoMoreStuff method scope
int a = 100;
}
}
```
⤶
# Accessibility⤶
In Lua you have global and local variables and table fields as ways of accesing variables.⤶
⤶
In C# you have more deliberate control over what can be accessed from where with namespaces, classes, access modifiers, local scopes, readonly modifier and more.⤶
⤶
## Lua example⤶
⤶
```lua⤶
-- Can be accessed from anywhere⤶
globalVar = 100⤶
⤶
-- globalTable, globalTable.a, globalTable.b can also be accessed⤶
-- from anywhere⤶
globalTable = {⤶
a = 100⤶
}⤶
⤶
globalTable.b = 200⤶
⤶
-- boo can be accessed in its definition scope (current file,⤶
-- function, condition or other code block)⤶
local boo = 300⤶
⤶
```⤶
⤶
## C# example⤶
⤶
```csharp⤶
⤶
// This is how you define a namespace⤶
namespace MyStuff⤶
{⤶
// Namespaces can be nested⤶
namespace BestStuff⤶
{⤶
// internal means that this class can be accessed from anywhere in⤶
// the current assembly, but not from other assemblies⤶
internal class Foo⤶
{⤶
// private means that this member is only accessible within class Foo⤶
private int privateMember = 100;⤶
⤶
// protected means that this member is only accessible within class Foo⤶
// and its decendants (classes that inherit from Foo)⤶
protected int protectedMember = 200;⤶
⤶
// public means that this member can be accessed from any other code⤶
public int publicMember = 300;⤶
⤶
// readonly in this case means that this field value can not be changed⤶
// after initialization⤶
public readonly int readonlyMember = 400;⤶
}⤶
}⤶
}⤶
```