Automatic type conversion with Implicit and Explicit operators in C#

Поделиться
HTML-код
  • Опубликовано: 18 дек 2024

Комментарии • 49

  • @nickchapsas
    @nickchapsas  3 года назад +12

    Thanks to ABP for sponsoring this video. Check it out here: bit.ly/3f26Mlf

    • @Ehomer0815
      @Ehomer0815 3 года назад +2

      Hey Nick,
      thanks for your awesome video!
      Can you make a video series about abp.io framework ? This would be awesome :)

    • @lemurza5236
      @lemurza5236 3 года назад +1

      @@Ehomer0815 Yeah I would really like to see that!

    • @TheChadC1
      @TheChadC1 3 года назад +2

      ABP is pretty awesome

  • @dahliaspumpski5837
    @dahliaspumpski5837 3 года назад +6

    Thanks for the heads up about mapping! First use case I thought of when watching this but I did think it was a bad code smell. Thank you! Very informative.

    • @KeyboardKrieger
      @KeyboardKrieger 3 года назад

      Same here xD

    • @aravindpk9736
      @aravindpk9736 Год назад

      I am still not understanding or able to come to an agreement (lack of some explanations) about why Mappers are still considerable than this, for use cases like ViewModel to DataModel conversions, One Db to other Db model conversions etc? Especially when Mapper tools like AutoMapper uses reflection which may be costlier than simply casting it. I dont know about Mapsters behind the scene mapping approach, looking forward for your thoughts/feedback to this?

  • @devincarlson8866
    @devincarlson8866 3 года назад +37

    “Completely random number” 😂

    • @Uni-Coder
      @Uni-Coder 3 года назад +2

      int rand () { return 420; }

  • @QuickZ_
    @QuickZ_ 10 месяцев назад

    well explained as always with only the necassary info while including enough to make it make sense.

  • @ltybchxtyrj
    @ltybchxtyrj 2 года назад

    ty. I needed some other explanation after reeding Rihter.

  • @LeoninMinecraft
    @LeoninMinecraft 2 года назад +1

    this really helped explain the concept to me, thanks for the education!

  • @sunnypatel1045
    @sunnypatel1045 3 года назад +1

    Thank you for the video Nick. I learn something new everyday from you!

  • @squirrel1620
    @squirrel1620 3 года назад +1

    Damn that makes so much sense. Nice explaination and demo

  • @cn-ml
    @cn-ml 3 года назад +1

    4:50, im starting to have doubts that nicks random number generator is biased

  • @kevinli9212
    @kevinli9212 3 года назад +1

    So what would be a useful and appropriate use case for these operators?

  • @adoulou340
    @adoulou340 3 года назад +1

    i liked before watching the video, keep coding, Thanks.

  • @lucasmoreira3237
    @lucasmoreira3237 3 года назад +1

    Amazing lessons. Thank you!

  • @CharlesBurnsPrime
    @CharlesBurnsPrime 2 года назад +2

    It would be helpful to link value object videos as well (mentioned really in this video).

  • @DJDoena
    @DJDoena 3 года назад +3

    The main issue with implicit and explicit casting operators is that you usually end up with a different reference in memory.
    when you have an instance of Milk that derives from Liquid and it's in a IEnumerable then you can test-cast the object to Milk and change its properties and the object in the list will change because it is the same memory reference.
    When Milk does not derive from Liquid but you have cast operators for them then adding Milk to a List will not actually add the Milk instance you have in your variable. Any further change to the object will not be mirrored in the Liquid object in the list.

    • @macurvello
      @macurvello 3 года назад +1

      Good point. The feature seems very convenient but I would be very uneasy to use it because of problems like these. I think you're masking behavior that might cause dangerous inconsistencies. On top of that they'd be pretty hard to debug.
      Maybe if you use it responsibly with value types only like the first example in the video that might be fine. But still one should be very careful when making such a decision.
      I do appreciate the video though!

  • @flyingmadpakke
    @flyingmadpakke 3 года назад +1

    What is your opinion on having an implicit conversion between a class and a builder of that class?
    For example, string and StringBuilder. When I have something like that, I always have an implicit conversion from the builder to the class, but string and StringBuilder don't have this and so I wonder if there is a reason for not having it?

  • @jackkendall6420
    @jackkendall6420 3 года назад +3

    Very interesting stuff. Though I would feel a bit wary about using lots of implicit conversions all over the place -- could easily get confusing for other people in the codebase. Though maybe I'm off-base?

    • @nickchapsas
      @nickchapsas  3 года назад +2

      You are correct. Implicit/explicit operators have value when the conversion is obvious, like I showed on the first example. If the conversion isn't obvious then don't use them.

    • @NoGamer256
      @NoGamer256 3 года назад +2

      ​@@nickchapsas Speaking of examples of poor implicit operator implementation in one of codebases i have worked with there is a struct "Currency" (a decimal value and a string currency symbol primitive wrapper) that defines Currency(decimal) implicit operator.
      Guess what? It returns the amount as my national currency and you could say that it is reasonable after all.
      Unless somebody tries to do var currency = new Currency(10m,"EUR"); currency += 10. You would expect it to be 20 Euro but instead you get an exception that you can't add two different currencies. I can't even count how many times i've seen this mistake.

  • @phillipeaam
    @phillipeaam Год назад

    Wow! Very nice! Thanks for the content =]

  • @SzpkRomana
    @SzpkRomana 2 года назад

    how do you get variables visible in main window 6:34 ? Is it possible in visual studio 22?

  • @carlosalarcon2737
    @carlosalarcon2737 3 года назад

    Perfect! Could you do a video on Rider and how to install and use analyzers? Many thanks!!

    • @johnnic431
      @johnnic431 3 года назад

      I would get the JetBrains Toolbox from www.jetbrains.com/toolbox-app/ . On windows, it will be an icon on the lower right side of the task bar Once you have toolbox, you can install Rider from the interface easily, and keep it up to date from there as well!

  • @lexdxw1989
    @lexdxw1989 Год назад

    Really cool.

  • @kebabfoto
    @kebabfoto 3 года назад

    what is the previous video called that explains what a guid is? :) thanks in advance to anyone who could tell me

  • @Picikak03
    @Picikak03 3 года назад

    1:35 - "you can check that video if you want"
    what video? there's no link to the video

  • @TheChadC1
    @TheChadC1 3 года назад

    I agree that its not the objects responsibility to do mappings, but are there any performance benefits over using something like automapper or mapster? just trying to understand why people would use it for mapping other than ease of use

    • @nickchapsas
      @nickchapsas  3 года назад +1

      There aren't any performance benefits over mapster (codegen) since a static call and an instance call should have the same performance. Ease of use is why people tend to use it which is the pitfall IMO.

  • @GeospatialTechnologies
    @GeospatialTechnologies 3 года назад

    Can I use it for converting the video clip to the URL of the web page?

  • @jinalpatel9154
    @jinalpatel9154 3 года назад

    I think this is useful but in context of use case when domain entity convert to dto , I think we are crossing layer and this is not good.

  • @brentsteyn6671
    @brentsteyn6671 3 года назад

    There is sooo many stuff in remember in c#

  • @willemschipper7736
    @willemschipper7736 3 года назад

    Fresh haircut
    also interesting video

  • @MindVortex23
    @MindVortex23 3 года назад

    When you try to be professional but your inner meme lord comes out 4:45

  • @roflex2
    @roflex2 3 года назад

    I wonder if I can finally make a function take a generic "numeric" type using this.

    • @NoGamer256
      @NoGamer256 3 года назад

      Nope, unless you wrap it with a class/struct with certain interface (like INumericOperation) and use implicit operators. I wouldn't do it, and to make it work 100% it would need type checking and conversions probably. Just a thought of top of my head though. Oh and you can't obviously implicitly convert to an interface so you have to return concrete type but that's not a big issue.

  • @elpe21
    @elpe21 3 года назад

    Nice explanations but how you presented the code / jumping
    was a bit chaotic for me.

  • @clearlyunwell
    @clearlyunwell 3 года назад

    👍🏽

  • @liaryde
    @liaryde 3 года назад

    Random number, 420 dude. Just waiting for 69 for the memes 😆

    • @macurvello
      @macurvello 3 года назад

      7:40

    • @liaryde
      @liaryde 3 года назад

      @@macurvello I didn’t watch the whole video... would have been better if I had 😂

  • @11r3start11
    @11r3start11 3 года назад +1

    > many devs don't know about implicit/explicit type conversion operators
    > don't use them as they're confusing
    as soon as it's not good from a practical perspective it's quite useless knowledge, similar to goto and many other niche stuff...

    • @nickchapsas
      @nickchapsas  3 года назад +3

      It is good from a practical perspective, it's just that you need critical thinking to understand of if it's a good idea to add one or not. Value objects are the perfect candidate for them and it's 99% of the usage I'm making for this feature.