C# Primary Constructor Tips

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

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

  • @DaveRogersEsq
    @DaveRogersEsq 2 месяца назад +3

    Cool. Some good points there. I've only used them a couple of times and don't really understand what they bring to the table other than brevity.

    • @Ardalis
      @Ardalis  2 месяца назад +4

      Mostly brevity, which isn't a bad thing. They're just inconsistent with how everything, even other similar things, work which makes them confusing to many devs.

  • @Ruisrd
    @Ruisrd 2 месяца назад +1

    Hey Steve,
    Great points around this topic, thanks for clarifying this feature, sometimes this sugar syntax can be messy.
    Keep doing this great work 💪🏻

  • @iulian9796
    @iulian9796 2 месяца назад

    Hello Steve. Nice video. Great to see you directly on my homepage :)
    I do have 2 short real life usage questions tho, related to a current microservice hierarchy I am developing.
    Could primary constructors and records be used also for commands, queries and integration bus events or would it be better to just use classes and have the whole record as a record and not one containing references?
    And secondly would be, why use MassTransit when you can very easily (and more performing) use MediatR for local orchestration?

  • @naefp
    @naefp 2 месяца назад +5

    they are nice and i like to use them, but i dont understand why microsoft decided to make them mutable by default. i mean if the primary constructor parameters would always be readonly, i could still assign them to a mutable field or property when needed. instead of the other way around…

    • @Ardalis
      @Ardalis  2 месяца назад

      I agree that would have been an excellent decision, in hindsight. Probably they had reasons. I think they are looking into supporting readonly in C# 13.

  • @michaelakin766
    @michaelakin766 2 месяца назад

    Question, how can we combine your guardclauses and results into a single pattern?

    • @Ardalis
      @Ardalis  2 месяца назад +1

      Results are meant to avoid exceptions; guards throw exceptions. If you want to combine them, you'd need a try-catch in the method that returns Result and in the catch block you'd return Result.Error and provide some details there.

  • @alexlo5655
    @alexlo5655 2 месяца назад +1

    Hi Steve,
    Thank you for publishing the interesting video. However, the source code link is not working correctly. After clicking on the source code link, I was brought to a page saying, "Almost finished... We need to confirm your email address. To complete the subscription process, please click the link in the email we just sent you." Unfortunately, I haven't received any email (I've also checked the spam folder).
    Could you please look into this issue ? Thank you for your help

    • @Ardalis
      @Ardalis  2 месяца назад

      Sorry it's not working for you. It seems to be working overall so I'm not sure what the issue is with MailChimp and your email account.

  • @christpetitjean
    @christpetitjean 2 месяца назад

    What happens with reference types mutation in method and property initialization ? Does it follow reference or is it safe ?

    • @Ardalis
      @Ardalis  2 месяца назад

      You mean something like:
      public class A(Product product)
      {
      private Product _product = product;
      public void DoSomething => _product.UpdateState();
      }
      Will this change the product that was passed into the constructor?
      It will behave exactly as if you had a (normal) constructor and you passed in Product there.

    • @christpetitjean
      @christpetitjean 2 месяца назад

      @Ardalis not exactly. If you call product.update (the one from primary). Did this mutation also impacted _product field ? As they share the same ref, I am wondering what happens. (Mutation can also happens outside the class 😱)