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. Ever use this syntax in C# 7?

Ever use this syntax in C# 7?

Scheduled Pinned Locked Moved The Lounge
csharppythonrubycsscom
20 Posts 15 Posters 0 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.
  • M Offline
    M Offline
    Marc Clifton
    wrote on last edited by
    #1

    return _(); IEnumerable _() {...}

    Found this on [MoreLinq](https://github.com/morelinq/MoreLINQ/blob/master/MoreLinq/DistinctBy.cs), it actually took an [SO](https://stackoverflow.com/questions/45911116/meaning-of-the-syntax-return-ienumerabletsource) lookup for me to realize they were use a local method with an "anonymous" name. > In this context, the underscore is just an arbitrary name for a local function (which is a new feature introduced in C# 7.0). If you prefer, you could replace the underscore by a more descriptive name. :^) C# is starting to look more like Ruby/Python. The explanation is useful though: > By the way, the reason for having two methods here is so that, in case any argument is null, ArgumentNullException will be thrown immediately when DistinctBy is called instead of when the result is enumerated (due to the presence of the yield return statement). Though, putting this on the same line...I agree with what others wrote. X|

    Latest Article - Class-less Coding - Minimalist C# and Why F# and Function Programming Has Some Advantages Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802

    OriginalGriffO D R J M 5 Replies Last reply
    0
    • M Marc Clifton

      return _(); IEnumerable _() {...}

      Found this on [MoreLinq](https://github.com/morelinq/MoreLINQ/blob/master/MoreLinq/DistinctBy.cs), it actually took an [SO](https://stackoverflow.com/questions/45911116/meaning-of-the-syntax-return-ienumerabletsource) lookup for me to realize they were use a local method with an "anonymous" name. > In this context, the underscore is just an arbitrary name for a local function (which is a new feature introduced in C# 7.0). If you prefer, you could replace the underscore by a more descriptive name. :^) C# is starting to look more like Ruby/Python. The explanation is useful though: > By the way, the reason for having two methods here is so that, in case any argument is null, ArgumentNullException will be thrown immediately when DistinctBy is called instead of when the result is enumerated (due to the presence of the yield return statement). Though, putting this on the same line...I agree with what others wrote. X|

      Latest Article - Class-less Coding - Minimalist C# and Why F# and Function Programming Has Some Advantages Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      The more they add to the language, the more I look at it and think it's time to rip it apart and create D# or whatever. Some of this seems to have been added to please one person who might use it, instead of being a general solution to a widespread problem. Can't see how this makes maintenance easier at all...

      Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      C Richard DeemingR S W 4 Replies Last reply
      0
      • M Marc Clifton

        return _(); IEnumerable _() {...}

        Found this on [MoreLinq](https://github.com/morelinq/MoreLINQ/blob/master/MoreLinq/DistinctBy.cs), it actually took an [SO](https://stackoverflow.com/questions/45911116/meaning-of-the-syntax-return-ienumerabletsource) lookup for me to realize they were use a local method with an "anonymous" name. > In this context, the underscore is just an arbitrary name for a local function (which is a new feature introduced in C# 7.0). If you prefer, you could replace the underscore by a more descriptive name. :^) C# is starting to look more like Ruby/Python. The explanation is useful though: > By the way, the reason for having two methods here is so that, in case any argument is null, ArgumentNullException will be thrown immediately when DistinctBy is called instead of when the result is enumerated (due to the presence of the yield return statement). Though, putting this on the same line...I agree with what others wrote. X|

        Latest Article - Class-less Coding - Minimalist C# and Why F# and Function Programming Has Some Advantages Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        I can see the value in local functions but, I agree, that code could have been written for better readability. Another one that doesn't quite sit well with me is the new Expression-bodied Members[^] and Throw Expressions. That just makes the code harder to understand if you're new to C#. This one depends more on understanding the convention being used instead of on the intent of the code.

        T J 2 Replies Last reply
        0
        • OriginalGriffO OriginalGriff

          The more they add to the language, the more I look at it and think it's time to rip it apart and create D# or whatever. Some of this seems to have been added to please one person who might use it, instead of being a general solution to a widespread problem. Can't see how this makes maintenance easier at all...

          Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

          C Offline
          C Offline
          CodeWraith
          wrote on last edited by
          #4

          Maintenance? Are you joking? These 'features' only exist to lengthen the feature list of upcoming new versions, to give some guys something to pontificate over and, most importantly, keep the fanbois drooling in anticipation. Who cares what it's actually good for?

          The user can't update the up: we update it for them (Choice in the CP poll)

          1 Reply Last reply
          0
          • M Marc Clifton

            return _(); IEnumerable _() {...}

            Found this on [MoreLinq](https://github.com/morelinq/MoreLINQ/blob/master/MoreLinq/DistinctBy.cs), it actually took an [SO](https://stackoverflow.com/questions/45911116/meaning-of-the-syntax-return-ienumerabletsource) lookup for me to realize they were use a local method with an "anonymous" name. > In this context, the underscore is just an arbitrary name for a local function (which is a new feature introduced in C# 7.0). If you prefer, you could replace the underscore by a more descriptive name. :^) C# is starting to look more like Ruby/Python. The explanation is useful though: > By the way, the reason for having two methods here is so that, in case any argument is null, ArgumentNullException will be thrown immediately when DistinctBy is called instead of when the result is enumerated (due to the presence of the yield return statement). Though, putting this on the same line...I agree with what others wrote. X|

            Latest Article - Class-less Coding - Minimalist C# and Why F# and Function Programming Has Some Advantages Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802

            R Offline
            R Offline
            R Giskard Reventlov
            wrote on last edited by
            #5

            I use what I need to get the job done - if it is that syntax and is readable and easy to maintain...

            L 1 Reply Last reply
            0
            • D Dave Kreskowiak

              I can see the value in local functions but, I agree, that code could have been written for better readability. Another one that doesn't quite sit well with me is the new Expression-bodied Members[^] and Throw Expressions. That just makes the code harder to understand if you're new to C#. This one depends more on understanding the convention being used instead of on the intent of the code.

              T Offline
              T Offline
              TheGreatAndPowerfulOz
              wrote on last edited by
              #6

              I'm kinda meh about Expression-bodied Members since they seem limited to single statements. However, throw expressions are a bit of a wack-on-the-head. You don't expect to see them when encountered. Why not just have the called method on the left side throw an exception?

              #SupportHeForShe Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun

              Richard DeemingR 1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                The more they add to the language, the more I look at it and think it's time to rip it apart and create D# or whatever. Some of this seems to have been added to please one person who might use it, instead of being a general solution to a widespread problem. Can't see how this makes maintenance easier at all...

                Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

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

                OriginalGriff wrote:

                Can't see how this makes maintenance easier at all...

                You can probably find people making the same argument for every feature ever introduced since v1.0! :-D I assume you don't use anonymous methods[^] either? Because manually writing a private class to represent a closure makes maintenance so much easier. :laugh: In this specific instance, they should have given the local function a more meaningful name than "_". But ignoring that, there's no real benefit to sticking with the previous version[^], which had a private static "Impl" method that was only called from the main method.


                "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

                OriginalGriffO M J 3 Replies Last reply
                0
                • T TheGreatAndPowerfulOz

                  I'm kinda meh about Expression-bodied Members since they seem limited to single statements. However, throw expressions are a bit of a wack-on-the-head. You don't expect to see them when encountered. Why not just have the called method on the left side throw an exception?

                  #SupportHeForShe Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun

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

                  As far as I can tell, throw expressions only exist to make expression-bodied members work. Without them, you'd have to check for null and throw an ArgumentNullException, which would mean multiple lines, and therefore prevent you from using an expression body. I'm sure some people will find a use for them. But I still much prefer using multi-statement bodies when there are multiple statements to execute. And I'm not really a fan of using expression-bodied methods for properties with more than one accessor either.

                  public string Name
                  {
                  get => name;
                  set => name = value ?? throw new ArgumentNullException(nameof(value));
                  }

                  // vs:

                  public string Name
                  {
                  get => name;
                  set
                  {
                  if (value == null) throw new ArgumentNullException(nameof(value));
                  name = value;
                  }
                  }

                  // vs:

                  public string Name
                  {
                  get
                  {
                  return name;
                  }
                  set
                  {
                  if (value == null) throw new ArgumentNullException(nameof(value));
                  name = value;
                  }
                  }


                  "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

                  1 Reply Last reply
                  0
                  • Richard DeemingR Richard Deeming

                    OriginalGriff wrote:

                    Can't see how this makes maintenance easier at all...

                    You can probably find people making the same argument for every feature ever introduced since v1.0! :-D I assume you don't use anonymous methods[^] either? Because manually writing a private class to represent a closure makes maintenance so much easier. :laugh: In this specific instance, they should have given the local function a more meaningful name than "_". But ignoring that, there's no real benefit to sticking with the previous version[^], which had a private static "Impl" method that was only called from the main method.


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

                    OriginalGriffO Offline
                    OriginalGriffO Offline
                    OriginalGriff
                    wrote on last edited by
                    #9

                    I use anonymous methods - when they are appropriate - but that abortion is just bad code. It's lazy and doesn't make anything easier on the poor sod who has to work on it. And I have noticed that the people who throw this kind of thing together are always far too busy to fix it six months later when the bug reports come in ...

                    Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

                    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                    1 Reply Last reply
                    0
                    • OriginalGriffO OriginalGriff

                      The more they add to the language, the more I look at it and think it's time to rip it apart and create D# or whatever. Some of this seems to have been added to please one person who might use it, instead of being a general solution to a widespread problem. Can't see how this makes maintenance easier at all...

                      Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

                      S Offline
                      S Offline
                      Slacker007
                      wrote on last edited by
                      #10

                      I think the reason is so that there is less code on the page. Do more with less. Eventually in C# v45.6 we will be able to accomplish an entire line of code in the span of one character - a super character of sorts. Here is a sample of what a simple "Hello World" console app might look like with C# v45.6.

                      a

                      F 1 Reply Last reply
                      0
                      • Richard DeemingR Richard Deeming

                        OriginalGriff wrote:

                        Can't see how this makes maintenance easier at all...

                        You can probably find people making the same argument for every feature ever introduced since v1.0! :-D I assume you don't use anonymous methods[^] either? Because manually writing a private class to represent a closure makes maintenance so much easier. :laugh: In this specific instance, they should have given the local function a more meaningful name than "_". But ignoring that, there's no real benefit to sticking with the previous version[^], which had a private static "Impl" method that was only called from the main method.


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

                        M Offline
                        M Offline
                        Mladen Jankovic
                        wrote on last edited by
                        #11

                        Richard Deeming wrote:

                        class

                        Classes? Are you insane? These little buggers are nightmare for maintenance. You don't need anything more than assembler, you're just trying to be hip.

                        1 Reply Last reply
                        0
                        • R R Giskard Reventlov

                          I use what I need to get the job done - if it is that syntax and is readable and easy to maintain...

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #12

                          It's becoming like every other ms product: 90% of the features must be [at best] barely useful. (feeling kind today - avoiding negatives.)

                          Signature not found error error: Unable to throw signature not found error.

                          1 Reply Last reply
                          0
                          • OriginalGriffO OriginalGriff

                            The more they add to the language, the more I look at it and think it's time to rip it apart and create D# or whatever. Some of this seems to have been added to please one person who might use it, instead of being a general solution to a widespread problem. Can't see how this makes maintenance easier at all...

                            Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

                            W Offline
                            W Offline
                            W Balboos GHB
                            wrote on last edited by
                            #13

                            With people, we note them as "Jackass of all trades - Master of None". Where's my damn QuickC ?

                            Ravings en masse^

                            "The difference between genius and stupidity is that genius has its limits." - Albert Einstein

                            "If you are searching for perfection in others, then you seek disappointment. If you are seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010

                            1 Reply Last reply
                            0
                            • M Marc Clifton

                              return _(); IEnumerable _() {...}

                              Found this on [MoreLinq](https://github.com/morelinq/MoreLINQ/blob/master/MoreLinq/DistinctBy.cs), it actually took an [SO](https://stackoverflow.com/questions/45911116/meaning-of-the-syntax-return-ienumerabletsource) lookup for me to realize they were use a local method with an "anonymous" name. > In this context, the underscore is just an arbitrary name for a local function (which is a new feature introduced in C# 7.0). If you prefer, you could replace the underscore by a more descriptive name. :^) C# is starting to look more like Ruby/Python. The explanation is useful though: > By the way, the reason for having two methods here is so that, in case any argument is null, ArgumentNullException will be thrown immediately when DistinctBy is called instead of when the result is enumerated (due to the presence of the yield return statement). Though, putting this on the same line...I agree with what others wrote. X|

                              Latest Article - Class-less Coding - Minimalist C# and Why F# and Function Programming Has Some Advantages Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802

                              J Offline
                              J Offline
                              Jon McKee
                              wrote on last edited by
                              #14

                              Never used it exactly like this, no. But I've used it when using a lambda to satisfy a delegate/action/func parameter with at least one parameter of its own where I literally do not care about the lambda parameter. Ex. _ => { /*....*/ }

                              1 Reply Last reply
                              0
                              • D Dave Kreskowiak

                                I can see the value in local functions but, I agree, that code could have been written for better readability. Another one that doesn't quite sit well with me is the new Expression-bodied Members[^] and Throw Expressions. That just makes the code harder to understand if you're new to C#. This one depends more on understanding the convention being used instead of on the intent of the code.

                                J Offline
                                J Offline
                                Jon McKee
                                wrote on last edited by
                                #15

                                Lambdas have become so important for the language that I don't see the issue with expression-bodied members. It's pretty easy to figure out what they mean even if you've never seen them before. But I can see the argument for "it wasn't needed so why add it?" The throw expressions with a null-coalescing operator, however, is a very logical addition in my opinion. It just makes sense. That operator is great for two things: defaults on null and errors on null. Until now you couldn't do the second instead having to fall back on if(X == null) throw /*error*/. Granted that isn't much boilerplate compared to the handler code that was simplified with ?., but it just makes sense that ?? should make it unnecessary.

                                D 1 Reply Last reply
                                0
                                • J Jon McKee

                                  Lambdas have become so important for the language that I don't see the issue with expression-bodied members. It's pretty easy to figure out what they mean even if you've never seen them before. But I can see the argument for "it wasn't needed so why add it?" The throw expressions with a null-coalescing operator, however, is a very logical addition in my opinion. It just makes sense. That operator is great for two things: defaults on null and errors on null. Until now you couldn't do the second instead having to fall back on if(X == null) throw /*error*/. Granted that isn't much boilerplate compared to the handler code that was simplified with ?., but it just makes sense that ?? should make it unnecessary.

                                  D Offline
                                  D Offline
                                  Dave Kreskowiak
                                  wrote on last edited by
                                  #16

                                  Oh, I can see the value in it, but like I said, someone new to this looks at that and goes "what the :elephant: is THAT?!". I know this because I had three people who have some curly language experience come up, see this, and said that very thing.

                                  System.ItDidntWorkException: Something didn't work as expected. A guide to posting questions on CodeProject

                                  Click this: Asking questions is a skill. Seriously, do it.
                                  Dave Kreskowiak

                                  1 Reply Last reply
                                  0
                                  • S Slacker007

                                    I think the reason is so that there is less code on the page. Do more with less. Eventually in C# v45.6 we will be able to accomplish an entire line of code in the span of one character - a super character of sorts. Here is a sample of what a simple "Hello World" console app might look like with C# v45.6.

                                    a

                                    F Offline
                                    F Offline
                                    Forogar
                                    wrote on last edited by
                                    #17

                                    Don't you mean:

                                    public void a

                                    - I would love to change the world, but they won’t give me the source code.

                                    S 1 Reply Last reply
                                    0
                                    • F Forogar

                                      Don't you mean:

                                      public void a

                                      - I would love to change the world, but they won’t give me the source code.

                                      S Offline
                                      S Offline
                                      Slacker007
                                      wrote on last edited by
                                      #18

                                      I think you missed the point of my joke. Either that, or just wasn't funny to begin with, which is totally possible. No worries. :sigh:

                                      1 Reply Last reply
                                      0
                                      • M Marc Clifton

                                        return _(); IEnumerable _() {...}

                                        Found this on [MoreLinq](https://github.com/morelinq/MoreLINQ/blob/master/MoreLinq/DistinctBy.cs), it actually took an [SO](https://stackoverflow.com/questions/45911116/meaning-of-the-syntax-return-ienumerabletsource) lookup for me to realize they were use a local method with an "anonymous" name. > In this context, the underscore is just an arbitrary name for a local function (which is a new feature introduced in C# 7.0). If you prefer, you could replace the underscore by a more descriptive name. :^) C# is starting to look more like Ruby/Python. The explanation is useful though: > By the way, the reason for having two methods here is so that, in case any argument is null, ArgumentNullException will be thrown immediately when DistinctBy is called instead of when the result is enumerated (due to the presence of the yield return statement). Though, putting this on the same line...I agree with what others wrote. X|

                                        Latest Article - Class-less Coding - Minimalist C# and Why F# and Function Programming Has Some Advantages Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802

                                        M Offline
                                        M Offline
                                        Munchies_Matt
                                        wrote on last edited by
                                        #19

                                        Dont know, pretty sure there is a C# forum for this question though. :)

                                        1 Reply Last reply
                                        0
                                        • Richard DeemingR Richard Deeming

                                          OriginalGriff wrote:

                                          Can't see how this makes maintenance easier at all...

                                          You can probably find people making the same argument for every feature ever introduced since v1.0! :-D I assume you don't use anonymous methods[^] either? Because manually writing a private class to represent a closure makes maintenance so much easier. :laugh: In this specific instance, they should have given the local function a more meaningful name than "_". But ignoring that, there's no real benefit to sticking with the previous version[^], which had a private static "Impl" method that was only called from the main method.


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

                                          J Offline
                                          J Offline
                                          jschell
                                          wrote on last edited by
                                          #20

                                          Richard Deeming wrote:

                                          You can probably find people making the same argument for every feature ever introduced since v1.0!

                                          Version 1.0 of C you mean. Or perhaps version 1.0 of Fortran/Algol/Cobol.

                                          Richard Deeming wrote:

                                          Because manually writing a private class to represent a closure makes maintenance so much easier

                                          And easier to read also. And those one liners make debugging a snap as well.

                                          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