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 Weird and The Wonderful
  4. C# 6, you dirty dog!

C# 6, you dirty dog!

Scheduled Pinned Locked Moved The Weird and The Wonderful
csharpvisual-studiohelpannouncementxml
7 Posts 5 Posters 41 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.
  • P Online
    P Online
    PIEBALDconsult
    wrote on last edited by
    #1

    OK, so after my earlier post, I found that I was compiling with C# 6 turned on without realizing it. I prefer to use new features of C# only if they offer me something useful. I use only a few features of C# 3, and no newer features than that, so I want my code to compile with C# 3 selected, but I hadn't been bothering to specify it, I had left it set to the default (my fault of course). So yesterday I spent some time ensuring that my libraries and utilities will compile with C# 3. And BOOM! one of my recent utilities complained: error CS0840: '[REDACTED].CommonBase.ScriptXML.get' must declare a body because it is not marked abstract or extern. Automatically implemented properties must define both get and set accessors. and error CS8026: Feature 'readonly automatically implemented properties' is not available in C# 3. Please use language version 6 or greater. (Messages from two versions of CSC.EXE . I'm not sure which is the more helpful.) It turns out that a few months back, I had made a copy/paste error. I had intended to write: public System.Xml.XmlNode ScriptXML { get ; private set ; } but, because I had copied: System.Xml.XmlNode ScriptXML { get ; } from an Interface definition, I wound up with: public System.Xml.XmlNode ScriptXML { get ; } I had forgotten to add the private set ; (or do I want it protected?) and the compiler (set to C# 6) was fine with it! :elephant: ! Yeah, OK, so it's not actually a bug, but it's a coding error and I wish I had been alerted to it when I first wrote it. Going forward, I am specifying /langversion:3 until I find a useful feature of some future version. Bonus: My work laptop has these two versions of CSC.EXE: C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe Supports up to C# 5 C:\Program Files (x86)\MSBuild\14.0\Bin\csc.exe Supports up to C# 6 and, of course, I had been using the newer version (as you do). I expect the newer version was installed in the past month. But I found that utilities compiled with the newer version won't run on the servers, so I had to revert to using the older version when I compile on the command line, which is the norm for my utilities. Yes, I do use Visual Studio (2015) on occasion, but not for making release builds of command-line utilities to deploy to the servers.

    Richard DeemingR J E 3 Replies Last reply
    0
    • P PIEBALDconsult

      OK, so after my earlier post, I found that I was compiling with C# 6 turned on without realizing it. I prefer to use new features of C# only if they offer me something useful. I use only a few features of C# 3, and no newer features than that, so I want my code to compile with C# 3 selected, but I hadn't been bothering to specify it, I had left it set to the default (my fault of course). So yesterday I spent some time ensuring that my libraries and utilities will compile with C# 3. And BOOM! one of my recent utilities complained: error CS0840: '[REDACTED].CommonBase.ScriptXML.get' must declare a body because it is not marked abstract or extern. Automatically implemented properties must define both get and set accessors. and error CS8026: Feature 'readonly automatically implemented properties' is not available in C# 3. Please use language version 6 or greater. (Messages from two versions of CSC.EXE . I'm not sure which is the more helpful.) It turns out that a few months back, I had made a copy/paste error. I had intended to write: public System.Xml.XmlNode ScriptXML { get ; private set ; } but, because I had copied: System.Xml.XmlNode ScriptXML { get ; } from an Interface definition, I wound up with: public System.Xml.XmlNode ScriptXML { get ; } I had forgotten to add the private set ; (or do I want it protected?) and the compiler (set to C# 6) was fine with it! :elephant: ! Yeah, OK, so it's not actually a bug, but it's a coding error and I wish I had been alerted to it when I first wrote it. Going forward, I am specifying /langversion:3 until I find a useful feature of some future version. Bonus: My work laptop has these two versions of CSC.EXE: C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe Supports up to C# 5 C:\Program Files (x86)\MSBuild\14.0\Bin\csc.exe Supports up to C# 6 and, of course, I had been using the newer version (as you do). I expect the newer version was installed in the past month. But I found that utilities compiled with the newer version won't run on the servers, so I had to revert to using the older version when I compile on the command line, which is the norm for my utilities. Yes, I do use Visual Studio (2015) on occasion, but not for making release builds of command-line utilities to deploy to the servers.

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

      PIEBALDconsult wrote:

      it's a coding error

      That depends on your intention. If you wanted a property which could be modified by any code within your class, then yes. If you wanted a true "read-only" property, which can only be initialized in the constructor or an initializer, then omitting the private set; is perfectly correct.

      public class Foo
      {
      public int Bar { get; private set; }
      public int Baz { get; }

      public Foo(int bar, int baz)
      {
          Bar = bar; // Fine
          Baz = baz; // Also fine
      }
      
      public void Groot()
      {
          Bar = 42; // Compiles and runs
          Baz = 42; // Compiler error CS0200 - Property or indexer 'Foo.Baz' cannot be assigned to -- it is read-only
      }
      

      }


      "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

      P 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        PIEBALDconsult wrote:

        it's a coding error

        That depends on your intention. If you wanted a property which could be modified by any code within your class, then yes. If you wanted a true "read-only" property, which can only be initialized in the constructor or an initializer, then omitting the private set; is perfectly correct.

        public class Foo
        {
        public int Bar { get; private set; }
        public int Baz { get; }

        public Foo(int bar, int baz)
        {
            Bar = bar; // Fine
            Baz = baz; // Also fine
        }
        
        public void Groot()
        {
            Bar = 42; // Compiles and runs
            Baz = 42; // Compiler error CS0200 - Property or indexer 'Foo.Baz' cannot be assigned to -- it is read-only
        }
        

        }


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

        P Online
        P Online
        PIEBALDconsult
        wrote on last edited by
        #3

        Richard Deeming wrote:

        could be modified by any code within your class

        Yes. Even though I don't intend to change it. I prefer to leave that option open, possibly even making it protected instead.

        Richard Deeming wrote:

        can only be initialized in the constructor

        No. I prefer not to tie my hands that tightly. And I didn't realize that was the implication. I do occasionally make a readonly field though. That "feature" may be good, but the implementation is not -- adding a limitation by not writing something. X|

        J 1 Reply Last reply
        0
        • P PIEBALDconsult

          Richard Deeming wrote:

          could be modified by any code within your class

          Yes. Even though I don't intend to change it. I prefer to leave that option open, possibly even making it protected instead.

          Richard Deeming wrote:

          can only be initialized in the constructor

          No. I prefer not to tie my hands that tightly. And I didn't realize that was the implication. I do occasionally make a readonly field though. That "feature" may be good, but the implementation is not -- adding a limitation by not writing something. X|

          J Offline
          J Offline
          James Curran
          wrote on last edited by
          #4

          >> That "feature" may be good, but the implementation is not -- adding a limitation by not writing something. Say what? You are not "adding a limitation". You are not adding a feature (by not writing something). Or, written in the positive, You need to write something to add the capability. That's perfectly natural and correct.

          Truth, James

          1 Reply Last reply
          0
          • P PIEBALDconsult

            OK, so after my earlier post, I found that I was compiling with C# 6 turned on without realizing it. I prefer to use new features of C# only if they offer me something useful. I use only a few features of C# 3, and no newer features than that, so I want my code to compile with C# 3 selected, but I hadn't been bothering to specify it, I had left it set to the default (my fault of course). So yesterday I spent some time ensuring that my libraries and utilities will compile with C# 3. And BOOM! one of my recent utilities complained: error CS0840: '[REDACTED].CommonBase.ScriptXML.get' must declare a body because it is not marked abstract or extern. Automatically implemented properties must define both get and set accessors. and error CS8026: Feature 'readonly automatically implemented properties' is not available in C# 3. Please use language version 6 or greater. (Messages from two versions of CSC.EXE . I'm not sure which is the more helpful.) It turns out that a few months back, I had made a copy/paste error. I had intended to write: public System.Xml.XmlNode ScriptXML { get ; private set ; } but, because I had copied: System.Xml.XmlNode ScriptXML { get ; } from an Interface definition, I wound up with: public System.Xml.XmlNode ScriptXML { get ; } I had forgotten to add the private set ; (or do I want it protected?) and the compiler (set to C# 6) was fine with it! :elephant: ! Yeah, OK, so it's not actually a bug, but it's a coding error and I wish I had been alerted to it when I first wrote it. Going forward, I am specifying /langversion:3 until I find a useful feature of some future version. Bonus: My work laptop has these two versions of CSC.EXE: C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe Supports up to C# 5 C:\Program Files (x86)\MSBuild\14.0\Bin\csc.exe Supports up to C# 6 and, of course, I had been using the newer version (as you do). I expect the newer version was installed in the past month. But I found that utilities compiled with the newer version won't run on the servers, so I had to revert to using the older version when I compile on the command line, which is the norm for my utilities. Yes, I do use Visual Studio (2015) on occasion, but not for making release builds of command-line utilities to deploy to the servers.

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

            I was just sure this was going to be about how sometime readonly isn't really readonly. :)

            1 Reply Last reply
            0
            • P PIEBALDconsult

              OK, so after my earlier post, I found that I was compiling with C# 6 turned on without realizing it. I prefer to use new features of C# only if they offer me something useful. I use only a few features of C# 3, and no newer features than that, so I want my code to compile with C# 3 selected, but I hadn't been bothering to specify it, I had left it set to the default (my fault of course). So yesterday I spent some time ensuring that my libraries and utilities will compile with C# 3. And BOOM! one of my recent utilities complained: error CS0840: '[REDACTED].CommonBase.ScriptXML.get' must declare a body because it is not marked abstract or extern. Automatically implemented properties must define both get and set accessors. and error CS8026: Feature 'readonly automatically implemented properties' is not available in C# 3. Please use language version 6 or greater. (Messages from two versions of CSC.EXE . I'm not sure which is the more helpful.) It turns out that a few months back, I had made a copy/paste error. I had intended to write: public System.Xml.XmlNode ScriptXML { get ; private set ; } but, because I had copied: System.Xml.XmlNode ScriptXML { get ; } from an Interface definition, I wound up with: public System.Xml.XmlNode ScriptXML { get ; } I had forgotten to add the private set ; (or do I want it protected?) and the compiler (set to C# 6) was fine with it! :elephant: ! Yeah, OK, so it's not actually a bug, but it's a coding error and I wish I had been alerted to it when I first wrote it. Going forward, I am specifying /langversion:3 until I find a useful feature of some future version. Bonus: My work laptop has these two versions of CSC.EXE: C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe Supports up to C# 5 C:\Program Files (x86)\MSBuild\14.0\Bin\csc.exe Supports up to C# 6 and, of course, I had been using the newer version (as you do). I expect the newer version was installed in the past month. But I found that utilities compiled with the newer version won't run on the servers, so I had to revert to using the older version when I compile on the command line, which is the norm for my utilities. Yes, I do use Visual Studio (2015) on occasion, but not for making release builds of command-line utilities to deploy to the servers.

              E Offline
              E Offline
              englebart
              wrote on last edited by
              #6

              Command line utilities should be written in notepad and compiled with csc.exe.

              P 1 Reply Last reply
              0
              • E englebart

                Command line utilities should be written in notepad and compiled with csc.exe.

                P Online
                P Online
                PIEBALDconsult
                wrote on last edited by
                #7

                Yes, which is what I do... as far as you know ;) . (I actually wrote my own simple IDE.) I don't like that notepad can't be flexible on TABs. I do use it for XML though.

                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