C # - when do I need to override? When do I not need it?

I am using C # with Unity3d.

When I use Monobehaviour and create the Update method, I do not need to use the override keyword.

When I create my own class and redefine the function, I need to use the override keyword.

Why? Why don't I need the override keyword when creating the update method?

+4
source share
2 answers

I think your confusion comes from the fact that Unity is doing something special in relation to these methods ( Update, Start, Awakeetc.). You can declare them closed, and even then they will be called. This cannot be achieved with language unless you use reflection, but I was told that they do not use it, so I don’t know what they are doing. And honestly, it doesn’t matter. Because you may think that this is an exception to the language, that is, these methods will be called if you implement them. This is simple.

For everyone else, you must follow the language. Here is a rough explanation:

You can or should overrideuse the method if it is marked as abstractor virtualin the base class.

abstract, , . virtual, , / .

, ""? . , , . . abstract, virtual override, API .

+3

override, , , virtual. , - - .

, MSDN:

, , .

+1

Source: https://habr.com/ru/post/1548581/


All Articles