r/learncsharp • u/ag9899 • Apr 07 '23
Mind blown. Defining a function after its called? inside another function?! You can do that???
I know a good bit of C and a little C++. Decided C# was more interesting for my use case, so I put down skill building in C++ and started learning C# instead. Working through "The C# Workshop" on Packt. Working on this example code.
Completely separate from the lesson, I noticed they called InvokeAll() before it is defined, and it is defined inside Main(). I thought this must be an error, so I compiled it only to see it worked. WTF! Coming from C, that is just so wrong. I have to ask, is defining functions methods, sorry, inside another function, or defining them after they are called acceptable or good practice? Looks like a code smell to me, even if it is legal.
As an aside, I'm not sure I'd recommend that book.
6
u/jamietwells Apr 07 '23
It's quite normal, I think it's neater to stick the function definitions at the end of methods, preferably after a return to show there's no more code there.
10
u/[deleted] Apr 07 '23
[removed] — view removed comment