Revision Difference
CSharp_Basics#526570
<cat>Dev</cat>⤶
<cat>Dev.Modding</cat>⤶
<title>CSharp Basics</title>
It can be pretty intimidating to look at C\# code and see a bunch of random symbols and words. Hopefully this page will explain some of the words and what the represent so that
#Comments
**What is all that green text scattered around?**
Comments are not counted as code and exist purely to explain the purpose or function of code around it.
Comments are often written directly above the code they explain, so look right below the green text comments to see what they are explaining.
They are written by putting two backslashes `//` at the start of a line. Anything after those `//` will be treated as comments and ignored by the compiler.
#Compiler
**Hang on, what the heck is a compiler?**
A compiler simply takes your code (a text file) and turns it into an executable program that can run on computers.
For modding Rust there is a special compiler that can take your code, compile it into a program and run it on the fly without needing to restart or pause the server.
# Functions
**In CSharp they are called 'methods'**
A function is simply a grouping of code that will be ran line by line.
Can be executed by writing the name of the function with two parenthesis `()` after it.
```csharp
void MyFunction()
{
//Here is a comment!
Console.WriteLine( "This will be printed in the server console as text!" );
}
//Some hook that will be ran by the modding framework
void Hook()
{
//This will call the function 'MyFunction' above
MyFunction();
}
```
For the examples below, the function `Hook()` represents the function that will be ran by the modding framework at some point.
#What Is Void
**Now all I see is void before every function!**
In CSharp `void` means nothing.
Every function in C# has to return some kind of data. This leads us to a problem: sometimes we don't need any data back from it, as per the example above.
That's where `void` comes in: we put the word in front of the function to tell it we want nothing returned.
It is a little backwards to think about but makes sense if you think of void as `return nothing`.
But what if we actually want to return data...
#Return Types
Let's say we want to add together some numbers and return the result.
In this case, we will put `int` in front of the function (meaning a whole number from -2 Billion -> +2 Billion).
This will tell the compiler that we want to return a number after our function runs.
```csharp
int AddTogetherNumbers()
{
//This will return a 2
return 1 + 1;
}
void Hook()
{
int result = AddTogetherNumbers();
//We have ran the above code and stored the number returned inside a variable called 'result'
//Now lets print off the result
Console.WriteLine( result );
}
```
# Parameters
*Can also be referenced as arguments*
Parameters are data or options passed into the parameter.
Lets say we want to make a function to count the amount of letters in a player's name
```csharp
int CountPlayerName( BasePlayer player )
{
//Get's the player's display name and returns the length of the text as an integer number
return player.displayName.Length;
}
```
#Null
Represents 'Nothing'.