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. Other Discussions
  3. The Insider News
  4. Essential C# 6 Features You Need to Know!

Essential C# 6 Features You Need to Know!

Scheduled Pinned Locked Moved The Insider News
csharpcomquestionannouncement
27 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.
  • D Duncan Edwards Jones

    nameOf very good Filtered exceptions - good but can encourage bad habits Not sure about the rest...time will tell?

    F Offline
    F Offline
    FIorian Schneidereit
    wrote on last edited by
    #4

    I also like string interpolation a lot...

    $"The sum of {a} and {b} is {a + b}."

    P 1 Reply Last reply
    0
    • F FIorian Schneidereit

      I also like string interpolation a lot...

      $"The sum of {a} and {b} is {a + b}."

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

      That's dreadful. X|

      A F 2 Replies Last reply
      0
      • P PIEBALDconsult

        That's dreadful. X|

        A Offline
        A Offline
        Agent__007
        wrote on last edited by
        #6

        I agree. :thumbsup:

        You have just been Sharapova'd.

        1 Reply Last reply
        0
        • D Duncan Edwards Jones

          nameOf very good Filtered exceptions - good but can encourage bad habits Not sure about the rest...time will tell?

          A Offline
          A Offline
          Agent__007
          wrote on last edited by
          #7

          Duncan Edwards Jones wrote:

          time will tell?

          I don't see myself to be using any of those in near future except "nameOf" and "await in catch and finally".

          You have just been Sharapova'd.

          D 1 Reply Last reply
          0
          • A Agent__007

            Duncan Edwards Jones wrote:

            time will tell?

            I don't see myself to be using any of those in near future except "nameOf" and "await in catch and finally".

            You have just been Sharapova'd.

            D Offline
            D Offline
            Duncan Edwards Jones
            wrote on last edited by
            #8

            Agent__007 wrote:

            "await in catch and finally".

            I don't feel the article linked articulated that one well - I shall look it up..certainly error handling is something that needs fixing in async code.

            1 Reply Last reply
            0
            • K Kevin Priddle

              Telerik[^]:

              With the final version of C# 6 having been released, we can seen that a lot of syntax changes have occurred since its inception. Because of this, I’ve noticed that most of the blog posts currently on the internet don’t work anymore or are too vague about how they implemented a feature. I thought it would be useful to make a list of the most essential C# 6 features with simple code examples that would make it both easy to understand and simple to copy/paste a sample into a new console app to try it. Let’s jump in.

              Don't you feel smarter now?

              P Offline
              P Offline
              PANKAJMAURYA
              wrote on last edited by
              #9

              What is New in C# 6.0[^]

              Pankaj Maurya Sr. Software Engineer Gurgaon, India

              1 Reply Last reply
              0
              • K Kevin Priddle

                Telerik[^]:

                With the final version of C# 6 having been released, we can seen that a lot of syntax changes have occurred since its inception. Because of this, I’ve noticed that most of the blog posts currently on the internet don’t work anymore or are too vague about how they implemented a feature. I thought it would be useful to make a list of the most essential C# 6 features with simple code examples that would make it both easy to understand and simple to copy/paste a sample into a new console app to try it. Let’s jump in.

                Don't you feel smarter now?

                M Offline
                M Offline
                Mario Z
                wrote on last edited by
                #10

                Does anyone find "using static" to be helpful? If yes then in what circumstances? I just can't find any reason to use it... even in that very common example ("using static System.Console", which everyone that has posted a C# 6 feature used, I find it's much worse to read it in this manner. There are some nice features, like Auto-Property initializers it could be helpful for creating a readonly properties, but the only extremely helpful thing (at least to me) is the introduction of nameOf...

                D 1 Reply Last reply
                0
                • K Kevin Priddle

                  Telerik[^]:

                  With the final version of C# 6 having been released, we can seen that a lot of syntax changes have occurred since its inception. Because of this, I’ve noticed that most of the blog posts currently on the internet don’t work anymore or are too vague about how they implemented a feature. I thought it would be useful to make a list of the most essential C# 6 features with simple code examples that would make it both easy to understand and simple to copy/paste a sample into a new console app to try it. Let’s jump in.

                  Don't you feel smarter now?

                  R Offline
                  R Offline
                  Rob Grainger
                  wrote on last edited by
                  #11

                  While a lot of these new features do have a valid use, I have reservations about the language being extended with a whole set of new terse operators. If that continues, we may end up with something as unreadable as Perl.

                  "If you don't fail at least 90 percent of the time, you're not aiming high enough." Alan Kay.

                  K 1 Reply Last reply
                  0
                  • R Rob Grainger

                    While a lot of these new features do have a valid use, I have reservations about the language being extended with a whole set of new terse operators. If that continues, we may end up with something as unreadable as Perl.

                    "If you don't fail at least 90 percent of the time, you're not aiming high enough." Alan Kay.

                    K Offline
                    K Offline
                    Kevin McFarlane
                    wrote on last edited by
                    #12

                    Rob Grainger wrote:

                    If that continues, we may end up with something as unreadable as Perl.

                    :laugh: I hate Perl with a vengeance!

                    Kevin

                    P 1 Reply Last reply
                    0
                    • K Kevin Priddle

                      Telerik[^]:

                      With the final version of C# 6 having been released, we can seen that a lot of syntax changes have occurred since its inception. Because of this, I’ve noticed that most of the blog posts currently on the internet don’t work anymore or are too vague about how they implemented a feature. I thought it would be useful to make a list of the most essential C# 6 features with simple code examples that would make it both easy to understand and simple to copy/paste a sample into a new console app to try it. Let’s jump in.

                      Don't you feel smarter now?

                      D Offline
                      D Offline
                      Dominic Burford
                      wrote on last edited by
                      #13

                      I think the Exception Filters example is a little contrived. You can achieve the same behaviour by simple catching specific exceptions in your catch block. Here is some code that I have used in my WCF client for handling WCF exceptions.

                      try
                      {
                      //some WCF service calls here
                      }
                      catch (FaultException<UnexpectedServiceFault> ex)
                      {
                      Console.WriteLine("Error occurred: {0}", ex.Message);
                      Console.WriteLine("service message: {0}", ex.Detail.ErrorMessage);
                      Console.WriteLine("source: {0}", ex.Detail.Source);
                      Console.WriteLine("target: {0}", ex.Detail.Target);
                      Console.WriteLine("stack trace: {0}", ex.Detail.StackTrace);
                      }
                      catch (CommunicationException ex)
                      {
                      Console.WriteLine("Communications error occurred: {0}", ex.Message);
                      }
                      catch (TimeoutException ex)
                      {
                      Console.WriteLine("Service has timed out");
                      Console.WriteLine(ex.Message);
                      }
                      catch (Exception ex)
                      {
                      Console.WriteLine("Error has occurred");
                      DumpExceptionDetails(ex);
                      }

                      Surely this is cleaner and easier to read than using C#6 Exception Filters.

                      "There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult." - C.A.R. Hoare Home | LinkedIn | Google+ | Twitter

                      1 Reply Last reply
                      0
                      • K Kevin Priddle

                        Telerik[^]:

                        With the final version of C# 6 having been released, we can seen that a lot of syntax changes have occurred since its inception. Because of this, I’ve noticed that most of the blog posts currently on the internet don’t work anymore or are too vague about how they implemented a feature. I thought it would be useful to make a list of the most essential C# 6 features with simple code examples that would make it both easy to understand and simple to copy/paste a sample into a new console app to try it. Let’s jump in.

                        Don't you feel smarter now?

                        D Offline
                        D Offline
                        Dan Neely
                        wrote on last edited by
                        #14

                        Quote:

                        Static Using Syntax

                        I'm also struggling to see a good use for this. Maybe if you had a huge number of `Console.WriteLine()` statements in a block?

                        Quote:

                        Auto-Property Initializers

                        I like this. Having to move the initialization to the constructor is one of the things I find mildly annoying about properties.

                        Quote:

                        Dictionary Initializers

                        Cleaner looking perhaps, but I don't see how it's supposed to be less error prone.

                        Quote:

                        String Interpolation

                        Meh. Combined with `nameof()` this might make writing a custom `ToString()` for debug purposes a bit better ( `return "{nameof(var1)} = {var1}, {nameof(var2)} = {var2}, ...";` ); but you're supposed to use a different syntax for that instead of abusing `ToString()` anyway.

                        Quote:

                        nameOf Expression

                        You could do this with reflection before but IIRC it was rather fugly. It'd be nice to see if this could make some reflection heavy code I wrote years ago easier to read and less brittle.

                        Quote:

                        Expression Bodied Function & Property

                        Yuck! I'm only not doing my giant X|[^] here for space reasons.

                        Quote:

                        Exception Filters

                        Not sure. His example sucks, but I could see this potentially being useful in other cases.

                        Quote:

                        Await in a Catch and Finally Block

                        Sounds useful but I haven't written any async code yet so not sure.

                        Quote:

                        Null Conditional Operator

                        I've wanted this for years. The example isn't that bad with the old syntax, but try drilling down through 3 or 4 levels of nullable properties/members and checking at each point. Saying elephant it and slapping a try/catch for null reference exceptions was the least unreadable version if not something you'd want to do in hot loops. BEST NEW FEATURE EVER!!!!!!!!!!!!!!!!!!!!!!!!!111oneoneone111elventyone11!11

                        Did

                        1 Reply Last reply
                        0
                        • K Kevin Priddle

                          Telerik[^]:

                          With the final version of C# 6 having been released, we can seen that a lot of syntax changes have occurred since its inception. Because of this, I’ve noticed that most of the blog posts currently on the internet don’t work anymore or are too vague about how they implemented a feature. I thought it would be useful to make a list of the most essential C# 6 features with simple code examples that would make it both easy to understand and simple to copy/paste a sample into a new console app to try it. Let’s jump in.

                          Don't you feel smarter now?

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

                          Michael Crump wrote:

                          throw new Exception(...

                          No, no, no, no NO!!! :mad: I wish the authors of the BCL had made the base Exception class abstract, to stop people from doing this. If you want to throw an exception, throw a specific exception type. In this case, it's a problem with an argument being null, so an ArgumentNullException would be appropriate. Throwing the base System.Exception class is just lazy, and makes your code harder to call - to the extent that Microsoft had to mess with the exception handling[^] to make catch (Exception) do the right thing.


                          "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
                          • M Mario Z

                            Does anyone find "using static" to be helpful? If yes then in what circumstances? I just can't find any reason to use it... even in that very common example ("using static System.Console", which everyone that has posted a C# 6 feature used, I find it's much worse to read it in this manner. There are some nice features, like Auto-Property initializers it could be helpful for creating a readonly properties, but the only extremely helpful thing (at least to me) is the introduction of nameOf...

                            D Offline
                            D Offline
                            Dennis_E
                            wrote on last edited by
                            #16

                            I can´t imagine using it for Console.WriteLine or something, but I think importing extension methods from a class instead of a namespace makes much more sense. Like: using static System.Linq.Enumerable;

                            M P 2 Replies Last reply
                            0
                            • D Dennis_E

                              I can´t imagine using it for Console.WriteLine or something, but I think importing extension methods from a class instead of a namespace makes much more sense. Like: using static System.Linq.Enumerable;

                              M Offline
                              M Offline
                              Mario Z
                              wrote on last edited by
                              #17

                              I see what you mean, so for example if we have one large Extensions namespace with this we could clarify or emphasise exactly which extensions we are using. Well that could be helpful. Thanks for the reply.

                              P 1 Reply Last reply
                              0
                              • P PIEBALDconsult

                                That's dreadful. X|

                                F Offline
                                F Offline
                                FIorian Schneidereit
                                wrote on last edited by
                                #18

                                PIEBALDconsult wrote:

                                That's dreadful. X|

                                The feature, or the sample? :D

                                P 1 Reply Last reply
                                0
                                • F FIorian Schneidereit

                                  PIEBALDconsult wrote:

                                  That's dreadful. X|

                                  The feature, or the sample? :D

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

                                  Yes.

                                  1 Reply Last reply
                                  0
                                  • D Dennis_E

                                    I can´t imagine using it for Console.WriteLine or something, but I think importing extension methods from a class instead of a namespace makes much more sense. Like: using static System.Linq.Enumerable;

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

                                    That's why I separate each of my Extension Methods (with overloads) into its own namespace -- I really can't stand having a whole load of crap brought in when I want only one small piece of crap. But then I also don't like the implementation of Extension Methods requiring the using directive, and the C# implementation not directly using the Attribute (unlike VB.net). X|

                                    M 1 Reply Last reply
                                    0
                                    • M Mario Z

                                      I see what you mean, so for example if we have one large Extensions namespace with this we could clarify or emphasise exactly which extensions we are using. Well that could be helpful. Thanks for the reply.

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

                                      Only as far as the developer allows.

                                      1 Reply Last reply
                                      0
                                      • P PIEBALDconsult

                                        That's why I separate each of my Extension Methods (with overloads) into its own namespace -- I really can't stand having a whole load of crap brought in when I want only one small piece of crap. But then I also don't like the implementation of Extension Methods requiring the using directive, and the C# implementation not directly using the Attribute (unlike VB.net). X|

                                        M Offline
                                        M Offline
                                        Mario Z
                                        wrote on last edited by
                                        #22

                                        I also keep them in separate namespaces, it's much cleaner. But nevertheless I do recall seeing some huge extension containers and I presume this could arrange a bit those big piles of mess...

                                        1 Reply Last reply
                                        0
                                        • P PIEBALDconsult

                                          Telerik wrote:

                                          Let’s jump in.

                                          No, let's not.

                                          P Offline
                                          P Offline
                                          Patrice T
                                          wrote on last edited by
                                          #23

                                          :thumbsup::thumbsup: ;P ;P

                                          Patrice “Everything should be made as simple as possible, but no simpler.” Albert Einstein

                                          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