Delegate are reference type but still when trying to pass it to a method it requires a ref keyword.
This reminds me of string that requires ref to be modified within a method because they are immutable, are delegate immutable?
Here is my mini project:
delegate void MyDel();
static void Main(string[] args)
{
MyDel _del = new MyDel(Idle);
while(true)
{
if(_del != null)
_del();
int c = Console.Read();
AI(c,ref _del);
Console.Read();
Console.Read();
}
}
}
static void AI(int c, ref MyDel del)
{
if(c == 'a')
{
del = Attack;
}else if(c == 'i'){
del = Idle;
}else if (c == 's'){
del = SwingSword;
}
else if (c == 'w')
{
del = Attack;
del += SwingSword;
}
else {
del = Idle;
}
}
Without the ref keyword I am back to the swap method case where things happen only inside the method.
Can anyone tell me why delegates need to be passed by reference?