Events
- Publishers fires of an event, all subscribers get notified that event was fired.
- Publisher is not aware of Subscribers.
EventHandler is simple Delegate.
public delegate void EventHandler(object sender, [EventAgrs](#eventargs) e )
It is dotNet standard. Not necessary to use. One may chose to define own custom delegate instead.
Delegate vs Events
A delegate is a type that defines a method signature. This allows you to pass methods as arguments to other methods. like a variable that holds a reference to a method, instead of a value.
An event, is essentially a special type of delegate that allows classes to communicate with each other in a loosely coupled way .
public event System.EventHandler OnKeyPress;
OnKeyPress(this, EventArgs.Empty); //Call it when needed
- Above code will throw error because there are no subscribers. Check for Null
OnKeyPress?.Invoke(this, EventArgs.Empty)
- In order to subscribe, we have to define a function that will recieve that event.
- Function signature needs to be same as event.
private void Fn_OnKeyPress(object sender, EventArgs e){}
- Finally, in order to subscribe, we use +=
OnKeyPress += Fn_OnKeyPress;
Do not forget to detach using -=
EventArgs
public event System.EventHandler<OnKeyPressEventArgs> OnKeyPress;
public class OnKeyPressEventArgs : EventArgs {
public int keyNum;
}
OnKeyPress?.Invoke(this, new OnKeyPressEventArgs{keyNum=11});
private void Fn_OnKeyPress(object sender, OnKeyPressEventArgs e){}
Delegates
- Stores a function in a variable
- Delegates are essentially function signatures, without any implementation
public delegate void SimpleDelegate();
private SimpleDelegate simpleDelegateFunction;
private void MySimpleDelegateFunction()
{ Debug.Log("Hello"); }
private void SecondSimpleDelegateFunction()
{ Debug.Log("bye"); }
simpleDelegateFunction = MySimpleDelegateFunction;
simpleDelegateFunction();
simpleDelegateFunction = SecondSimpleDelegateFunction;
simpleDelegateFunction();
Multicasting
simpleDelegateFunction = MySimpleDelegateFunction;
simpleDelegateFunction += SecondSimpleDelegateFunction;
simpleDelegateFunction();
Predefined Delegates
Delegates for PubSub
public delegate void OnKeyPressEventDelegate(int i);
Events with custom delegates
- instead of EventHandler delegate
public event OnKeyPressEventDelegate OnKeyPressEvent;
OnKeyPressEvent?.Invoke(11);
Action
Action is the default delegate. It retuns void. It has a generic version, which can be defined with different types.
//Action = delegate void System.Action()
pubic event Action OnKeyOne;
public event Action<bool> OnKeyTwo;
public event Action<int, float, bool> OnKeyThree;
OnKeyTwo?.Invoke(true);
Csharp syntax
The out Operator in C#
- pass arguments to methods by reference.
- It’s particularly useful when a method needs to
- return multiple values
- void copying large data structures.
int width = 5;
int calculatedArea;
CalculateArea(width, out calculatedArea);
Auto-Implemented Properties
public class EnergyComponent
{
public float MaxEnergy { get; set; }
public float currentEnergy { get; set; } = 10f;
public EnergyComponent(float maxEnergy)
{
MaxEnergy = maxEnergy;
}
}
To make objects immutable:
only
a get accessor, or- set accessor as
private
. init
accessors ? @TODO
Misc C#
- null!: the “null-forgiving” operator (reference). When an expression can’t be null but the compiler can’t tell.
- Instances of a class can always be null
- Instances of a struct need to be wrapped in Nullable
( FooStruct? myStruct). - When wrapping in a Nullable, use HasValue and Value.
- not != null and myStruct!.
[Export]
private FooObject _object = null!;