delegate inside our outside class?
Should I create a delegate inside or outside a class? Is there a difference?
11 Replies
Being inside a class, you can give it more access modifiers
But unless accessing it from the declaring class, you'll need the full name PlayerShip.ADelegate
you can put delegates outside of classes?
Like how you can have classes inside classes
i haven't been using them at all lol
i've never seen a use for them
They are useful. It's like a redirect or a shortcut to piece of code, a pointer to particular methods
Yeah, I know when it's inside a class and when I want to use it from other class I use class name dot variable name e.g.
System.Random
or UnityEngine.Random
I was asking though if there is a difference if I put a delegate outside class, like in the 1st lineIt's the same as declaring a class inside another class, vs directly in a namespace
The general advice is not to have public nested types, and that includes delegates and enums
So avoid public nested delegates
Do you mean public delegates inside a class? So I should put them in a namespace like in the 1st line?
Yes
Nested means defined inside another type
Okay, so I guess this is the only difference? Different way of accessing them?
Yeah, pretty much. The other is that nested types can be private/protected, which can be handy. That's the one time I would nest one
Alright, thanks for answering!