Тёмный

Learn C#: Method Hiding (and Why You Will Want To Avoid It) 

Zoran Horvat
Подписаться 31 тыс.
Просмотров 4,9 тыс.
50% 1

Опубликовано:

 

27 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 41   
@oolong2
@oolong2 7 месяцев назад
I've only ever used "property hiding" in a derived generic class. Where the base object has a property of type "object" or "SomeOtherBaseClass" but the derived generic version "Derived" would represent that same property as T. It doesn't change how the property works it only casts it to a more specific type.
@joaogabrielv.m328
@joaogabrielv.m328 7 месяцев назад
"new void" seems like a new way to give programmers a PTSD LOL Great video, man
@jongeduard
@jongeduard 7 месяцев назад
Unrelated to method hiding, but System.Void is actually a real type, which is used for representing the void internally in dotnet, even though C# does not allow instantiating this type. This is useful for certain usages in Reflection.
@Sindrijo
@Sindrijo 7 месяцев назад
DONT DO IT, YOU WILL REGRET IT.
@zoran-horvat
@zoran-horvat 7 месяцев назад
Precisely.
@Sindrijo
@Sindrijo 7 месяцев назад
I feel like you should have mentioned explicit interface implementation. 1. You can implement a method explicitly only in the interface without resorting to method hiding (because of name clashes). 2. You cannot access this method which would otherwise be hiding a base method through any other means than via the interface. 3. Polymorphism is observed when using the objects via the interface contracts.
@zoran-horvat
@zoran-horvat 7 месяцев назад
Yes, but that is another topic. In this video, I only wanted to show hiding as an isolated concept.
@colinhannah2210
@colinhannah2210 7 месяцев назад
I hit the opposite problem recently with the base and derived classes both implementing INotifyPropertyChanged, used to trigger update events in bound WPF user interfaces. We had to refactor away the derived class implementing INotifyPropertyChanged to fix the binding again. Quite subtle.
@chandrahaslanka
@chandrahaslanka 6 месяцев назад
Thank you so much for this explanation. I come from Java world and I have been racking my brain for days trying to figure out why on earth is this even required. It's completely counterintuitive. And as you mentioned, it is a bad idea overall.
@fluffydoggo
@fluffydoggo 7 месяцев назад
Heres the pattern that revolves around my only use case: public class MyClass(object value) { public object Value { get; } = value; } public class Derived(string value) : MyClass(value) { new public string Value => (string)base.Value; } However I'd rarely write code like this, as I would put MyClass into an interface unless its unfeasible. For example: public interface IConfigurationBinding { object Value { get; } void Serialize(BinaryWriter writer) void Deserialize(BinaryReader reader); } public interface IConfigurationBinding : IConfigurationBinding { new T Value { get; } } public class Int32ConfigurationBinding : IConfigurationBinding { public int Value { get; private set; } object IConfigurationBinding.Value => Value; public void Serialize(BinaryWriter writer) => writer.Write(Value); public void Deserialize(BinaryReader reader) => Value = reader.ReadInt32(); } This way, I can have a list of IConfigurationBindings without knowing their type and I can serialize and deserialize them. I can also check if its a specific type using the type interface. This may be useful in situations where you might read/write the representations of the value differently, or it even can store multiple values. A great example is an implementation using Ticks of DateTime and DateTimeOffset. A class wouldnt work as I could only have one concrete inplementation. The implementing class would implement both IConfigurationBinding, IConfigurationBinding, and IConfigurationBinding. Wish there was a way to change a property nullability without hiding the property as that has come up on occassion.
@vitahvitah5823
@vitahvitah5823 7 месяцев назад
Very nice explanation. Thanks
@ataadevs
@ataadevs 7 месяцев назад
I did it once, and the manager change the base method to abstract and then used 'override' keyword in the child class. So I learnt my lesson in the hard way
@zoran-horvat
@zoran-horvat 7 месяцев назад
Sure. You would use method hiding only when everything else fails.
@evancombs5159
@evancombs5159 7 месяцев назад
So this begs the questions as to why was this feature included in the first place? Is there a valid use case?
@zoran-horvat
@zoran-horvat 7 месяцев назад
Backward compatibility in seriously forced cases. For example, the generic IEnumerable uses it. I reckon it makes sense if breaking with the name could make rioters with torches and pitchforks appear at your home address. Otherwise, make a breaking change and go clean.
@sagarviradiya
@sagarviradiya 7 месяцев назад
you sounds mysterious!!!
@nickbarton3191
@nickbarton3191 7 месяцев назад
I didn't know the subtly with the Interface, but we don't allow method hiding, just hideous. Doesn't it break LSP?
@zoran-horvat
@zoran-horvat 7 месяцев назад
It doesn't break the LSP per se, but a concrete implementation could. Like with any other method.
@nickbarton3191
@nickbarton3191 7 месяцев назад
@@zoran-horvat Say that again? If I implemented a new or hiding method which doesn't function like the base method, then running the base class unit tests on an object of that derived class would fail, right? Kind of the benchmark of LSP.
@zoran-horvat
@zoran-horvat 7 месяцев назад
@@nickbarton3191 You must define what "doesn't function like the base method" means. LSP is about proving properties, so you must say which property stops being provable after changing a method. Some method changes might break it; others will preserve it. Very specifically, what makes you think in advance that a unit test would fail if it encountered a changed method implementation? Shouldn't that conclusion depend on the assertion?
@nickbarton3191
@nickbarton3191 7 месяцев назад
@@zoran-horvat Yes, I suppose it depends on the specific test, but changing behaviour risks in any new or overriden method risks that. Maybe that's what you were getting at.
@zoran-horvat
@zoran-horvat 7 месяцев назад
@@nickbarton3191 I am trying to get you to a clear view of method implementations. There is no such thing as "risk" when writing a method. There is a specification of preconditions it must accept as given (and is free to fail when the caller did not oblige); the list of postconditions it must guarantee (that is what we assert in unit tests!); and the list of class invariants it must keep satisfied. Any implementation that satisfies these specifications is by all means valid and no unit test must ever fail on it.
@Dr-Zed
@Dr-Zed 7 месяцев назад
Thanks, I really hate it. But at least you can't attach properties to functions like you can in JavaScript. Yes, JS is that cursed!
@Cool-Game-Dev
@Cool-Game-Dev 7 месяцев назад
var thing = new void();
@zoran-horvat
@zoran-horvat 7 месяцев назад
That would twist so many brains.
@EugeneS88-RU
@EugeneS88-RU 7 месяцев назад
Its really head acke
@zoran-horvat
@zoran-horvat 7 месяцев назад
And that is the reason enough to never do it.
@rankarat
@rankarat 7 месяцев назад
Use AI voice in these videos, your channel will explode. Your content is great, Your voice...not so much, i hate to write it and i feel uncomfortable with what i wrote, but its true and stops from this channel to become what it should become.
@jongeduard
@jongeduard 7 месяцев назад
Sorry, I feel hard disagree here. I am really tired of all those automatic voices on YT which all sound the same. I really appreciate it when people represent themselves personally. And issues with audio can just be solved by using better audio equipement.
Далее
Avoid This Common Mistake in DDD Modeling
10:17
Просмотров 9 тыс.
Boolean Is Not Your Friend
12:45
Просмотров 43 тыс.
новое испытание
00:40
Просмотров 329 тыс.
Бокс - Финты Дмитрия Бивола
00:31
Master The L in SOLID
15:57
Просмотров 8 тыс.
Nothing is really cool in Kotlin
7:43
Просмотров 7 тыс.
Await Async Tasks Are Getting Awesome in .NET 9!
9:24
5 Sure Signs You're No Longer Junior Programmer
13:21
17 Pieces of C# Syntax That Make Your Code Short
12:41
Naming Things in Code
7:25
Просмотров 2,2 млн
Premature Optimization
12:39
Просмотров 825 тыс.