+ 12

Challenge in C#: method outside class

During a challenge in C#, one of the problems says the following. You can define a method outside the class. True/False? After completing the challenge, the answer shows False. I think the answer should be True because it is possible to define a method outside a class. For example, I can define a method inside a struct instead of inside a class (see the link below to the example code that I created). And a struct is not the same thing as a class. So, what do you think? https://code.sololearn.com/cyEWyd2Fzf8C/#

23rd May 2019, 3:37 PM
r1c5
r1c5 - avatar
3 ответов
+ 7
I agree that a method in C# has to be in a scope (either in a class or a struct). However, the statement saying "outside the class" doesn't mean "outside scope". And also, here are some more differences about struct and class in C#. A struct is a value type, while a class is a reference type. You cannot define a parameterless constructor in a struct, but in a class you can. You cannot initialize an instance field in a struct body, but in a class you can.
23rd May 2019, 4:56 PM
r1c5
r1c5 - avatar
+ 1
I disagree, you can't define a free function like in C. It has to be in a scope. In this case a class is the same as a struct, the only difference is that everything is public.
23rd May 2019, 3:59 PM
nobody
0
r1c5 that's a good point
2nd May 2021, 8:43 AM
Krysto Foxik 🐥
Krysto Foxik 🐥 - avatar