C# 6, you dirty dog!
-
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.
anderror 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 theprivate 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# 5C:\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. -
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.
anderror 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 theprivate 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# 5C:\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.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
-
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
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| -
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|>> 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
-
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.
anderror 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 theprivate 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# 5C:\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. -
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.
anderror 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 theprivate 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# 5C:\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. -
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.