C# TUTORIAL
//Basic structure explained:
<visibility> <return type> <name>(<parameters>)
{
<function code>
}
// <visibility> = Public/Private, can be omitted, indefined = private
// <return type> = Data Type, type of data to be returned (ie int)
// <name> = Name of Function
// <parameters> = 0+ parameters
// Standard Func with no return value
public void DoStuff()
{
Console.WriteLine("I'm doing something...");
}
// Standard Func with two parameters, that returns value of type 'int'
public int AddNumbers(int number1, int number2)
{
// Call as : int result = AddNumbers(10, 5);
// then type Console.WriteLine(result);
int result = number1 + number2;
return result;
}
// Standard Func with default return value 0
public int AddNumbers(int number1, int number2)
{
// Its not necessary that the if will evaluate to true, and return result
// so we use default return 0, otherwise we ll get an error.
int result = number1 + number2;
if(result > 10)
{
return result;
}
// Standard Function with ref modifier
static void Main(string[] args)
{
int number = 20;
// This wont work because we pass a copy of
// number in 'AddFive'
AddFive(number);
Console.WriteLine(number);
Console.ReadKey();
}
static void AddFive(int number)
{
number = number + 5;
}
return 0;
}
///
// The correct way to do this:
static void Main(string[] args)
{
int number = 20;
AddFive(ref number);
Console.WriteLine(number);
Console.ReadKey();
}
static void AddFive(ref int number)
{
number = number + 5;
}
//The params modifier
//So far, all of our functions have accepted a fixed amount of parameters. However, in some cases, you
//might need a function which takes an arbitrary number of parameters. This could of course be done by
//accepting an array or a list as a parameter, like this:
static void GreetPersons(string[] names) { }
//However, calling it would be a bit clumsy. In the shortest form, it would look like this:
GreetPersons(new string[] { "John", "Jane", "Tarzan" });
//It is acceptable, but it can be done even smarter, with the params keyword:
static void GreetPersons(params string[] names) { }
//Calling it would then look like this:
GreetPersons("John", "Jane", "Tarzan");
//Another advantage of using the params approach, is that you are allowed to pass zero parameters to it as well.
//Functions with params can even take other parameters as well, as long as the parameter with the params keyword
//are the last one. Besides that, only one parameter using the params keyword can be used per function. Here is
//a last and more complete example:
static void Main(string[] args)
{
GreetPersons(0);
GreetPersons(25, "John", "Jane", "Tarzan");
Console.ReadKey();
}
static void GreetPersons(int someUnusedParameter, params string[] names)
{
foreach(string name in names)
Console.WriteLine("Hello, " + name);
}
1. Try/catch structures - Trycatch.cs
2. Functions / Methods - FuncMeth.cs
///////////////////////////////////////////////////////
// General format
try {
// Command
}
catch(Exception e) // Here e will be the string of the error
{
MessageBox.Show("alalala " + e.Message)
}
///////////////////////////////////////////////////////
// Function Example
private void deleteFolder(string folder)
{
//////
// Input: folder path
// Output: None, unless there's an exception
// Usage: deleteFolder("/path/to...")
// Notes: If the folder is already deleted, display message.
//
try {
Directory.Delete(folder,true);
}
catch(Exception e)
{
MessageBox.Show("Production Folder already deleted\n" + e.Message)
}
// how to run scripts in c#:
step 1: Locate the compilers : C:\Windows\Microsoft.NET\Framework\**
step 2: Decide which version of .NET you want to compile it with
step 3: Go into relevant .net version cd C:\Windows\Microsoft.NET\Framework\v3.5
step 4: Inside that folder we should have -> csc.eve
step 5: c# scripts need to be pre-compiled like C:\Windows\Microsoft.NET\Framework\v3.5\csc.exe testmd.cs
step 6: Run with testmd <param1> <param2>