- 2

How does C++ allow the use of static methods in a non static class when C#.net does not allow this ?

Cpp vs C#

9th May 2021, 4:33 AM
Sanjay Kamath
Sanjay Kamath - avatar
6 odpowiedzi
+ 2
Sanjay Kamath oh you meant "instance methods in static classes are not allowed". You were saying the opposite. Well, yeah... apparently you cannot declare non-static members inside static classes in C#. As to 'why' you can do it in C++, I think it's because in C++, static classes isn't really a thing. Classes declared inside other classes are more like a type declared in the namespace of that class ('static' keyword cannot be used). In other words, the outer class only serves as a path to that type, and the inner class is not really a member of the outer class, only a type declared in it's namespace. I'm not really sure if that's the reason, or if what I just said makes sense. But that's what I think.
12th May 2021, 4:48 PM
XXX
XXX - avatar
+ 3
What do you mean? Both C++ and C# allow static methods in classes.... Also, the code you've written in your question description looks unrelated to your question. Please remove it.
9th May 2021, 4:39 AM
XXX
XXX - avatar
+ 3
confusing questions get confusing answers. as XXX pointed out, both language have static methods. maybe you meant something else?
9th May 2021, 5:48 AM
Apollo-Roboto
Apollo-Roboto - avatar
+ 1
AFAIK C# does not support static methods in non static classes, C++ does.
12th May 2021, 10:38 AM
Sanjay Kamath
Sanjay Kamath - avatar
+ 1
XXX The reverse is possible in C# but is it possible in C++. https://code.sololearn.com/c9DBQ7r45L3A/?ref=app
12th May 2021, 12:40 PM
Sanjay Kamath
Sanjay Kamath - avatar