using System;
class Program
{
static void Main()
{
Console.WriteLine("Hello, World!");
}
}
System: Imports system libraries
Main(): Entry point of the program
int number = 5;
float pi = 3.14f;
bool isCool = true;
string name = "Leah";
char letter = 'A';
Basic value types. `int`, `float`, `bool`, `string`, `char`
int a = 10 + 5;
bool test = a > 3 && a < 20;
a++;
a *= 2;
Arithmetic, logical, assignment ops
if (a > 10)
Console.WriteLine("Big number");
switch (a)
{
case 5:
Console.WriteLine("Five");
break;
default:
Console.WriteLine("Something else");
break;
}
for (int i = 0; i < 5; i++)
Console.WriteLine(i);
while (a < 10)
a++;
do
{
Console.WriteLine("Runs once at least");
} while (false);
int[] arr = {1, 2, 3};
List names = new List() {"Alice", "Bob"};
Console.WriteLine(arr[0]);
names.Add("Charlie");
int Add(int x, int y)
{
return x + y;
}
void Greet(string name)
{
Console.WriteLine("Hello " + name);
}
class Dog
{
public string name;
public Dog(string n)
{
name = n;
}
public void Bark()
{
Console.WriteLine(name + " says Woof!");
}
}
Dog myDog = new Dog("Rex");
myDog.Bark();
class Animal
{
public virtual void Speak()
{
Console.WriteLine("Some sound");
}
}
class Cat : Animal
{
public override void Speak()
{
Console.WriteLine("Meow");
}
}
Animal a = new Cat();
a.Speak();
interface IWeapon
{
void Fire();
}
class Gun : IWeapon
{
public void Fire()
{
Console.WriteLine("Bang!");
}
}
enum Direction { Up, Down, Left, Right }
Direction d = Direction.Up;
try
{
int x = 5 / 0;
}
catch (DivideByZeroException)
{
Console.WriteLine("Can't divide by zero!");
}
File.WriteAllText("test.txt", "Hello File");
string text = File.ReadAllText("test.txt");
Use `System.IO` namespace
async TaskLoadData() { await Task.Delay(1000); return 42; }
Async methods return Task or Task<T>
int[] nums = {1, 2, 3, 4};
var even = nums.Where(x => x % 2 == 0).ToList();
Powerful querying using `System.Linq`
public class Player : MonoBehaviour
{
void Start()
{
Debug.Log("Game started");
}
void Update()
{
transform.position += Vector3.forward * Time.deltaTime;
}
}
[Serializable]
class PlayerData
{
public int level;
}
delegate void MyDelegate();
event MyDelegate OnShitHappened;
void CallEvent()
{
OnShitHappened?.Invoke();
}
string? maybeNull = null;
if (maybeNull != null)
Console.WriteLine(maybeNull.Length);
Console.WriteLine(maybeNull?.Length);
Type t = typeof(MyClass);
MethodInfo[] methods = t.GetMethods();
tagdoesnothing on discord