Timeline for Why was C# made with "new" and "virtual+override" keywords unlike Java?
Current License: CC BY-SA 3.0
14 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Dec 11, 2018 at 2:15 | comment | added | Soroush Falahati |
Also, this should be added that new doesn't necessarily hide the method or property of the base class. It works only when you do know the real type of object. If a part of code accepts, for example, the base class as a parameter or the value, it stills calls the base class's method, even tho it is hidden in the child class. new use cases are little and far between, however, when you need them, they are super useful.
|
|
Dec 11, 2018 at 2:11 | comment | added | Soroush Falahati |
Another good use case for new is changing the signature of a property or method. A most useful example that I used multiple times was to change the type of a property to a subclass of the base class's property. For example hiding IXXResolver Resolver {get;} with new DefaultResolver Resolver {get => base.Resolver as DefaultResolver; } . It fits perfectly. It does still respect the contract with the base class and also makes it easier to access type-specific properties of a subclass's type (or interface implementation) if you already know the type of the child class.
|
|
Jun 20, 2014 at 10:09 | comment | added | deworde | @svick Correct, which is why it's normally a compiler warning. But having the ability to "new" covers edge conditions (such as the one given), and even better, makes it really obvious that you're doing something weird when you come to diagnose the inevitable bug. "Why's this class and only this class buggy... ah-hah, a "new", let's go test where the SuperClass is used". | |
Jun 18, 2014 at 18:28 | comment | added | supercat |
@svick: What would you think of defining a new function but tagging it Obsolete("Use XX or YY depending upon requirements", true) and refactoring code which expects the derived-class meaning to use a slightly-renamed alternative? That should avoid hidden bugs.
|
|
Jun 18, 2014 at 17:18 | comment | added | pdr | @C.Champagne: Any tool can be used badly and this one is a particularly sharp tool -- you can cut yourself easily. But that's not a reason to remove the tool from the toolbox and remove an option from a more talented API designer. | |
Jun 18, 2014 at 17:08 | comment | added | C.Champagne | @pdr I don't know C# very well but I think understood the difference and that is why I used "almost". That is for me the biggest difference between those languages. The thing is that it breaks polymorphism, that is why I don't feel enthousiastic. | |
Jun 18, 2014 at 17:02 | comment | added | Bobson | @C.Champagne - It's the difference between "All child classes should behave like the base class specifies" vs "All child classes can do what they want". One gives you much more control. | |
Jun 18, 2014 at 16:55 | comment | added | pdr |
@C.Champagne: You're still confusing override with new . They're not the same; not even close.
|
|
Jun 18, 2014 at 16:47 | comment | added | C.Champagne |
Maybe am I wrong but you can do (almost) all of this with Java. The thing is that the philosophy is the opposite. In Java, everything is overridable by default but you can blacklist the methods you don't want to override with the final keyword.
|
|
Jun 18, 2014 at 16:46 | comment | added | pdr |
One excellent use of new is in WebViewPage<TModel> in the MVC framework. But I have also been thrown by a bug involving new for hours, so I don't think it's an unreasonable question.
|
|
Jun 18, 2014 at 16:01 | comment | added | Bobson | @svick - Potentially, yes. In our particular scenario, that would never occur, but that's why I added the caveat. | |
Jun 18, 2014 at 15:57 | comment | added | svick |
I think that intentionally using new this way is a bug waiting to happen. If I call a method on some instance, I expect it to always do the same thing, even if I cast the instance to its base class. But that's not how new ed methods behave.
|
|
Jun 18, 2014 at 15:51 | history | edited | Bobson | CC BY-SA 3.0 |
Adding example.
|
Jun 18, 2014 at 15:42 | history | answered | Bobson | CC BY-SA 3.0 |