• dejected_warp_core@lemmy.world
    link
    fedilink
    arrow-up
    0
    ·
    1 month ago

    Eh, I haven’t touched C# since 2001. I agree that the more verbose style is more explicit, and so more readable. That said, I can figure most of the new style out from context.

    • => is clearly a closure declaration operator, similar to JavaScript.
    • x ??= y is shorthand for “assign y to x if x is not set, and return x” which is kind of nice.

    There must also be some shorthand going on for getter properties being the same as methods w/o an arglist (or even a ()).

    The only part that has me stumped is the unary question-mark operator: private static Singleton? _instance = null; I can think of a half-dozen things that could be, but I cannot decide what it’s doing that the original question-mark-free version isn’t.

      • fibojoly@sh.itjust.works
        link
        fedilink
        arrow-up
        0
        ·
        1 month ago

        My favourite operator to go with the question mark is the exclamation mark. I remember it as the “I swear to God I’m not null!” operator.

    • KindaABigDyl@programming.dev
      link
      fedilink
      arrow-up
      0
      ·
      1 month ago

      As others said, it means nullable, but to put it in more intuitive, less-jargony way - it’s a question mark bc you don’t know if the value is actually there or not. It could be a Singleton, but it isn’t until you check if there is a value. Whereas if you have, idk, int a no question mark, then you’re saying you actually have data.

      Essentially with C# 8, they “removed” null and reused the idea of null references in creating what is essentially an Option like in other languages. You either have some data of some type, or none (a null reference, in this case). By default, everything has to be there. Then when you need null, e.g. you may not have something initialized or an operation could fail, you explicitly grab for it. Thus it reduces null pointer bugs. If you don’t need nullability, you can ensure that you don’t accidentally write in an issue. It safety checks statements and parameters.

    • UnderpantsWeevil@lemmy.world
      link
      fedilink
      English
      arrow-up
      0
      ·
      1 month ago

      I agree that the more verbose style is more explicit, and so more readable.

      On its face, its readable. But when you’re dealing with 10,000 lines of code in a file, it becomes this ugly morass of extra nothingness to scroll through.

      The only part that has me stumped is the unary question-mark operator: private static Singleton? _instance = null;

      It transforms a strict variable into a nullable variable. I most commonly see it with primitive types.

      So, for instance

      int myInt = null

      is an illegal assignment, but

      int? myInt = null

      works fine.

      Why does a public class instantiation need this? Idfk. That might be extraneous. But I wouldn’t throw away the whole code block rewrite over that one character.