Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. I hate recent C# versions!

I hate recent C# versions!

Scheduled Pinned Locked Moved The Lounge
csharpquestion
92 Posts 48 Posters 25 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Chris Maunder

    No one is forcing you to use those new features unless you're working on a codebase that uses those new features. Things like

    public readonly double Distance => Math.Sqrt(X * X + Y * Y);

    are, for me, a matter of taste. Things like

    public static bool IsLetterOrSeparator(this char c) =>
    c is (>= 'a' and <= 'z') or (>= 'A' and <= 'Z') or '.' or ',';

    give me stomach acid. A dev has saved a few keystrokes at the expense of structure. This

    static Quadrant GetQuadrant(Point point) => point switch
    {
    (0, 0) => Quadrant.Origin,
    var (x, y) when x > 0 && y > 0 => Quadrant.One,
    var (x, y) when x < 0 && y > 0 => Quadrant.Two,
    var (x, y) when x < 0 && y < 0 => Quadrant.Three,
    var (x, y) when x > 0 && y < 0 => Quadrant.Four,
    var (_, _) => Quadrant.OnBorder,
    _ => Quadrant.Unknown
    };

    is meant to provide neat, compact code but I worry that for someone new to C# it becomes a stumbling block. My feeling is that it should be easy to switch between languages. Truly I wish there were only one language, but us humans are tribal and so that will never happen (and of course situations where a language needs to have specifics for the platform, hardware, compiler or programming methodology). Even so, making a language simpler is better, and adding syntactic sugar for the sake of it simply diverges the language. In many instances adding new features can converge languages. Javascript gaining the coalesce operator, C# getting the null-check. This is All Good. But like good art, they should add what they need and no more.

    cheers Chris Maunder

    Richard DeemingR Offline
    Richard DeemingR Offline
    Richard Deeming
    wrote on last edited by
    #70

    Chris Maunder wrote:

    public readonly double Distance => Math.Sqrt(X * X + Y * Y);

    That would give you a CS0106 compiler error. :) You would need either:

    public readonly double Distance = Math.Sqrt(X * X + Y * Y);

    or:

    public double Distance => Math.Sqrt(X * X + Y * Y);
    Wrong: See William's post below.

    Chris Maunder wrote:

    c is (>= 'a' and <= 'z') or (>= 'A' and <= 'Z')

    Well, of course, anyone serious about performance would write that as:

    (uint)((c | 0x20) - 'a') <= 'z' - 'a'

    :laugh: Source: Performance Improvements in .NET 7 - .NET Blog[^])


    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

    "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

    C W P 3 Replies Last reply
    0
    • Richard DeemingR Richard Deeming

      Chris Maunder wrote:

      public readonly double Distance => Math.Sqrt(X * X + Y * Y);

      That would give you a CS0106 compiler error. :) You would need either:

      public readonly double Distance = Math.Sqrt(X * X + Y * Y);

      or:

      public double Distance => Math.Sqrt(X * X + Y * Y);
      Wrong: See William's post below.

      Chris Maunder wrote:

      c is (>= 'a' and <= 'z') or (>= 'A' and <= 'Z')

      Well, of course, anyone serious about performance would write that as:

      (uint)((c | 0x20) - 'a') <= 'z' - 'a'

      :laugh: Source: Performance Improvements in .NET 7 - .NET Blog[^])


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      C Offline
      C Offline
      Chris Maunder
      wrote on last edited by
      #71

      Richard Deeming wrote:

      That would give you a CS0106 compiler error

      That was copy and pasted from [What's new in C# 8.0 - C# Guide | Microsoft Docs](https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8) :-D

      cheers Chris Maunder

      1 Reply Last reply
      0
      • B Behzad Sedighzadeh

        Am I the only one who hates recent addings to the language? Some examples: ?? Named/optional arguments () ?[] discards :confused:

        (_, _, area) = city.GetCityInformation(cityName);

        Switch expressions The list can go on and on. They are trying to make programming much easier and at the same time are making the syntax more and more unreadable:mad::mad:

        Behzad

        S Offline
        S Offline
        simpelman
        wrote on last edited by
        #72

        What about the ternary operator? You could argue that it doesn't make code more readable and not use it, but I guess you got somehow accustomed to it. (By the way: what is not to like about optional arguments or naming arguments?)

        M 1 Reply Last reply
        0
        • P PIEBALDconsult

          Which is not a with statement.

          N Offline
          N Offline
          NiL
          wrote on last edited by
          #73

          And what is a with statement

          P 1 Reply Last reply
          0
          • B Behzad Sedighzadeh

            Am I the only one who hates recent addings to the language? Some examples: ?? Named/optional arguments () ?[] discards :confused:

            (_, _, area) = city.GetCityInformation(cityName);

            Switch expressions The list can go on and on. They are trying to make programming much easier and at the same time are making the syntax more and more unreadable:mad::mad:

            Behzad

            S Offline
            S Offline
            Steve Naidamast
            wrote on last edited by
            #74

            Though I know C# very well, I stick with VB.NET simply because Microsoft is no longer updating the language with all the screwy constructs they keep adding to C#. Both languages are highly mature and no longer really need any new additions and have been that way for quite some time. However, Microsoft can't seem to let anything be even if it doesn't require MS engineers mucking about with it...

            Steve Naidamast Sr. Software Engineer Black Falcon Software, Inc. blackfalconsoftware@outlook.com

            1 Reply Last reply
            0
            • R RustyF

              If you don’t like more succinct code I would just stick with COBOL

              S Offline
              S Offline
              simpelman
              wrote on last edited by
              #75

              Yes! One language to rule them all

              1 Reply Last reply
              0
              • B Behzad Sedighzadeh

                Am I the only one who hates recent addings to the language? Some examples: ?? Named/optional arguments () ?[] discards :confused:

                (_, _, area) = city.GetCityInformation(cityName);

                Switch expressions The list can go on and on. They are trying to make programming much easier and at the same time are making the syntax more and more unreadable:mad::mad:

                Behzad

                S Offline
                S Offline
                Samuel Estrella C
                wrote on last edited by
                #76

                Hello, it’s true.

                1 Reply Last reply
                0
                • B Behzad Sedighzadeh

                  Am I the only one who hates recent addings to the language? Some examples: ?? Named/optional arguments () ?[] discards :confused:

                  (_, _, area) = city.GetCityInformation(cityName);

                  Switch expressions The list can go on and on. They are trying to make programming much easier and at the same time are making the syntax more and more unreadable:mad::mad:

                  Behzad

                  B Offline
                  B Offline
                  Bruce Greene
                  wrote on last edited by
                  #77

                  Agreed. C# is such an elegant language and well written C# code is understandable at a glance. Much of this new stuff is just WTF??

                  1 Reply Last reply
                  0
                  • Richard DeemingR Richard Deeming

                    Chris Maunder wrote:

                    public readonly double Distance => Math.Sqrt(X * X + Y * Y);

                    That would give you a CS0106 compiler error. :) You would need either:

                    public readonly double Distance = Math.Sqrt(X * X + Y * Y);

                    or:

                    public double Distance => Math.Sqrt(X * X + Y * Y);
                    Wrong: See William's post below.

                    Chris Maunder wrote:

                    c is (>= 'a' and <= 'z') or (>= 'A' and <= 'Z')

                    Well, of course, anyone serious about performance would write that as:

                    (uint)((c | 0x20) - 'a') <= 'z' - 'a'

                    :laugh: Source: Performance Improvements in .NET 7 - .NET Blog[^])


                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                    W Offline
                    W Offline
                    William Rummler
                    wrote on last edited by
                    #78

                    Richard Deeming wrote:

                    That would give you a CS0106 compiler error. :)

                    Not on a struct. :rose: Some C# features are intended mostly or solely to try to help produce more readable code. But some (like the readonly modifier on structs and their members) exist because they allow increased performance and/or compiler-enforced constraints against unintended usages of objects and their members. All that said, I have definitely seen (fine, I'll admit it, sometimes even written :~) code that goes overboard with the more concision-focused syntax that OP mentions. Doesn't help that VS is constantly underlining anything that can be converted to a ternary or expression-bodied member. X| Don't get me wrong, these two things have their place, and the corresponding auto-fixes should be right there in the editor context menu, but their possibility shouldn't trigger the code underlining... :java:

                    1 Reply Last reply
                    0
                    • B Behzad Sedighzadeh

                      Am I the only one who hates recent addings to the language? Some examples: ?? Named/optional arguments () ?[] discards :confused:

                      (_, _, area) = city.GetCityInformation(cityName);

                      Switch expressions The list can go on and on. They are trying to make programming much easier and at the same time are making the syntax more and more unreadable:mad::mad:

                      Behzad

                      A Offline
                      A Offline
                      Andre_Prellwitz
                      wrote on last edited by
                      #79

                      There is often a trade-off between succinctness and clarity. It's not always about "saving a few keystrokes"; sometimes it's about removing nonessential details, or better expressing intent, or allowing a developer to (literally) see the whole picture, or increase speed of understanding. Other times we see features added that have proved valuable in other languages or platforms.

                      1 Reply Last reply
                      0
                      • B Behzad Sedighzadeh

                        Am I the only one who hates recent addings to the language? Some examples: ?? Named/optional arguments () ?[] discards :confused:

                        (_, _, area) = city.GetCityInformation(cityName);

                        Switch expressions The list can go on and on. They are trying to make programming much easier and at the same time are making the syntax more and more unreadable:mad::mad:

                        Behzad

                        J Offline
                        J Offline
                        jochance
                        wrote on last edited by
                        #80

                        First, they came for the types (var) and noone said anything... Haha. I disagree on the named/optional and the discards, mostly on the point of "unreadable". These both make code more legible as opposed to maybe more succinct like your other examples.

                        1 Reply Last reply
                        0
                        • B Behzad Sedighzadeh

                          Am I the only one who hates recent addings to the language? Some examples: ?? Named/optional arguments () ?[] discards :confused:

                          (_, _, area) = city.GetCityInformation(cityName);

                          Switch expressions The list can go on and on. They are trying to make programming much easier and at the same time are making the syntax more and more unreadable:mad::mad:

                          Behzad

                          D Offline
                          D Offline
                          David On Life
                          wrote on last edited by
                          #81

                          Actually, I like and use many of the new enhancements. Some of them are real time savers, for example: ``` int x = foo?.bar?.index?[y] ?? 0; ``` versus: ``` int x = 0; if (foo != null && foo.bar != null && foo.bar.index != null && foo.bar.index[y] != null { x = foo.bar.index[y]; } ``` Not only was the new style code shorter and easier to write, but I would assume that it compiles down to a more efficient form because it doesn't have to keep re-evaluating the entire list of object references / pointers and only assigns a value to `x` once. Of course, I could write the second version to use a bunch of temporary variables and theoretically get close to the same performance, but then it would grow to be many more lines long... and still contain a bunch of redundant assignments to `x` that would be hard to eliminate unless I turned it into a local function (that's a lot of extra code authoring versus the nice simple, efficient, single line). I also think the first version is easier to read. I like the simplicity of a single `?` for a null check and the double `??` for a default value. I've also used the is/as construct as a time saver and really like some of the new switch statement options and scoping, and some of the new code shortcuts (like using `get => x;` instead of `get { return x; }` I particularly like putting them together for properties, for example writing `public int count { get => mylist?.Count ?? 0; }` is a lot easier (and clearer) than having to write out the null check. And while it might take a little bit to get used to, it's fairly intuitive (if I'd never seen it before, I'm pretty sure I'd still be able to figure out what it means just by the context). I can't say I'm a fan of every change, and I tend to lag (if only because I don't like to depend on the latest compiler when I'm working on a team, I try to give everyone time to get on the new compiler). However, the compiler hints do often push me toward the new options when it suggests them as ways to 'improve' my code... (sometimes I like that, and sometimes I don't...)

                          1 Reply Last reply
                          0
                          • B Behzad Sedighzadeh

                            Am I the only one who hates recent addings to the language? Some examples: ?? Named/optional arguments () ?[] discards :confused:

                            (_, _, area) = city.GetCityInformation(cityName);

                            Switch expressions The list can go on and on. They are trying to make programming much easier and at the same time are making the syntax more and more unreadable:mad::mad:

                            Behzad

                            D Offline
                            D Offline
                            Dave B 2022
                            wrote on last edited by
                            #82

                            As you learn more languages, you start to see where the C# developers copy all of their "innovations" from. They do it if it fits or not and if it makes sense or not. Sometimes they adopt the concept, but need to tweak the implementation in such a way, it loses a significant bit of the value it had in the original language. Global usings predefined by project type are my current favorite example. It seems like a matter of pride that C# has practically every feature they have seen in another language that they could make "work". The "magic" they add to the language allows someone to write far more logic in fewer strokes and a reader of the code has to understand exponentially more about the project type, all referenced libraries, and the code in every file if it looks relevant or not. With "global using" statements and extension methods, reading C# code snippets on line or even complete files can be somewhat meaningless without significant additional documentation describing the code.

                            J 1 Reply Last reply
                            0
                            • N NiL

                              And what is a with statement

                              P Offline
                              P Offline
                              PIEBALDconsult
                              wrote on last edited by
                              #83

                              Something C# doesn't have. And which I find useless in lesser languages.

                              1 Reply Last reply
                              0
                              • D Dave B 2022

                                As you learn more languages, you start to see where the C# developers copy all of their "innovations" from. They do it if it fits or not and if it makes sense or not. Sometimes they adopt the concept, but need to tweak the implementation in such a way, it loses a significant bit of the value it had in the original language. Global usings predefined by project type are my current favorite example. It seems like a matter of pride that C# has practically every feature they have seen in another language that they could make "work". The "magic" they add to the language allows someone to write far more logic in fewer strokes and a reader of the code has to understand exponentially more about the project type, all referenced libraries, and the code in every file if it looks relevant or not. With "global using" statements and extension methods, reading C# code snippets on line or even complete files can be somewhat meaningless without significant additional documentation describing the code.

                                J Offline
                                J Offline
                                jochance
                                wrote on last edited by
                                #84

                                Yeah, I think the global usings as well as ditching some of the boilerplate for a shorter "Hello World!" probably falls under the Geneva Convention or some kind of human rights UN charter but I haven't figured out how to bring a case yet. Some noob trying to learn: "args[]? what args[]? wth is args[]? is that a keyword? a variable?! but where...? huh? who thought this was ok?"

                                1 Reply Last reply
                                0
                                • B Behzad Sedighzadeh

                                  Am I the only one who hates recent addings to the language? Some examples: ?? Named/optional arguments () ?[] discards :confused:

                                  (_, _, area) = city.GetCityInformation(cityName);

                                  Switch expressions The list can go on and on. They are trying to make programming much easier and at the same time are making the syntax more and more unreadable:mad::mad:

                                  Behzad

                                  S Offline
                                  S Offline
                                  ShawnVN
                                  wrote on last edited by
                                  #85

                                  I'm ok with some of the simple quality-of-life things like null coalescing, and switch expressions. But a lot of it (compiler and runtime lib) has all gone a bit too far in the direction of high-level / functional programming. The beauty of C# was that it had much of the speed and efficiency of a low-level lang, with high-level niceties like GC, immutable strings and bounds-checked arrays (all of which happen to be important for security). But now code is riddled with anonymous function callbacks and linq expressions which can be 30x slower and consume more heap, than equivalent old school C#. And harder to read, sometimes, yes. I think it's a generational rotation, from devs whose first language was C/C++, to devs whose first programming language was javascript.

                                  1 Reply Last reply
                                  0
                                  • B Behzad Sedighzadeh

                                    Am I the only one who hates recent addings to the language? Some examples: ?? Named/optional arguments () ?[] discards :confused:

                                    (_, _, area) = city.GetCityInformation(cityName);

                                    Switch expressions The list can go on and on. They are trying to make programming much easier and at the same time are making the syntax more and more unreadable:mad::mad:

                                    Behzad

                                    M Offline
                                    M Offline
                                    Member_5893260
                                    wrote on last edited by
                                    #86

                                    I like some of it. It's useful to be able to say something like

                                    string x = Person?.Name ?? "";

                                    and for me, that's quite easy to read. There are other bits which are sort of ghastly, though. Basically, I don't like anything which, to me, isn't totally context-insensitive, or where I can't tell what the compiler's quite going to do with it (which is why I hate Linq). It's all quite interesting, though. As a language, C# is becoming so laden with syntactic sugar that at this point, it's actually idiomatic, which is fascinating for a programming language: until now, I'd always considered idiom as something restricted to spoken, human languages.

                                    1 Reply Last reply
                                    0
                                    • B Behzad Sedighzadeh

                                      Am I the only one who hates recent addings to the language? Some examples: ?? Named/optional arguments () ?[] discards :confused:

                                      (_, _, area) = city.GetCityInformation(cityName);

                                      Switch expressions The list can go on and on. They are trying to make programming much easier and at the same time are making the syntax more and more unreadable:mad::mad:

                                      Behzad

                                      B Offline
                                      B Offline
                                      BotReject
                                      wrote on last edited by
                                      #87

                                      I think C# has always sacrificed clarity of syntax for high-level functionality. Many people seem to like it, as it increasingly enables more to be done with fewer lines of code (though I am not sure why that's important) but personally I find it too high-level now for my liking so I rarely use it. No one language will ever appeal to everyone in my opinion.

                                      1 Reply Last reply
                                      0
                                      • Richard DeemingR Richard Deeming

                                        Chris Maunder wrote:

                                        public readonly double Distance => Math.Sqrt(X * X + Y * Y);

                                        That would give you a CS0106 compiler error. :) You would need either:

                                        public readonly double Distance = Math.Sqrt(X * X + Y * Y);

                                        or:

                                        public double Distance => Math.Sqrt(X * X + Y * Y);
                                        Wrong: See William's post below.

                                        Chris Maunder wrote:

                                        c is (>= 'a' and <= 'z') or (>= 'A' and <= 'Z')

                                        Well, of course, anyone serious about performance would write that as:

                                        (uint)((c | 0x20) - 'a') <= 'z' - 'a'

                                        :laugh: Source: Performance Improvements in .NET 7 - .NET Blog[^])


                                        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                                        P Offline
                                        P Offline
                                        Philippe Verdy
                                        wrote on last edited by
                                        #88

                                        The purpose of the "is" contruct with multiple conditions is to avoid repeating the first operand (imagine it is a function or method, or attribute whose evaluation or dereference would have a side effect). Here it allows writing it, without having to declare and initialize the value of an additional local temporary variable (that temporary variable or register will be allocated/defined intrisicly by the compiler, and only where it needs it). You get a shorter and more readable condition with "is" than with such additional explicit declaration. Then when testing "c is" with constants or ranges, this becomes very clear, and the compiler can choose how to represent the ranges or individual values (notably when they are constant) and generate the best code (yes it means that it can then optimize "c is (>= 'a' and <= 'z') or (>= 'A' and <= 'Z')" itself into "(uint)((c | 0x20) - 'a') <= 'z' - 'a'", but without using ugly expressions with unsafe/unchecked typecasts. So in my opinion this short notation is a good idea, and it remains very readable while being safe (and allowing easier optimization and inlining if needed by the compiler). Doing the same thing in C++ is a real nightmare (you have to play with very ugly "move" semantics that are very hard to debug when there are also multiple overriden candidates for operators/convertors/constructors and so many implicit typecasts to play with). Doing that in C is simpler, but hard to manage as the compiler has no knowledge or control of the semantics, so it cannot make so many optimizations: you need to use explicit local "register" declarations, put multiple statements within braces, possibly surrounded by the "do{}while(0)" trick when using ugly macros... And still the C compiler is unable to perform optimizations you'd expect (and there's no warranty that it works when you also have to play with "volatile" semantics), plus the effect of "restricted" state concurrency used by modern processors using multiple cores and modern architectures using caches over shared buses/networks, where loads/stores may be reordered. Cosidering all this, the "is" construct is very safe and the compiler will generate the correct code while preserving all the needed semantics, and without pollutiong the source code with ugly temp varaibles and extra local scopes.

                                        1 Reply Last reply
                                        0
                                        • S Simon Morrison 2022

                                          Yes! I don't program for PCs any more any just stick to embedded in C largely because all the higher level languages have been similarly afflicted.

                                          M Offline
                                          M Offline
                                          Member_14884492
                                          wrote on last edited by
                                          #89

                                          I do embedded C mostly also. However I had a fairly hairy embedded project that I ended up writing a C# wrapper around to permit faster testing (I was processing recorded data files). The only thing I really missed from C is the preprocessor. Why is this not a thing in C#? #define FOOT_PER_MILE 5280.0 #define INCH_PER_FOOT 12.0 #define INCH_PER_MILE (FOOT_PER_MILE * INCH_PER_FOOT) double miles = 12.54; double inches; ... inches = miles * INCH_PER_MILE; In a lot of embedded applications you don't have GB of ram to use. Sometimes you are stuck with KB of RAM. It is hard to beat inline constants when squeezing a program into a resource limited processor.

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups