4th Edition Early Access 9
Early Access #9 is available. Here are the release notes:
This is the 9th early access release of the C# Player’s Guide, 4th Edition.
There were three major efforts that were completed in this update, and it might be the most substantial update we’ve had.
First, the book now accounts for all C# 9 features. Most of the actual C# 9 feature changes were in advanced, dark, corner cases and didn’t require any big changes. But there were several that are worth pointing out:
- Partial methods just got more powerful. The Partial Methods sub-section in Level 47 got revised.
- “Native sized” integers (ints that match the target hardware, for situations where you are interoperating with low-level code) were added. A section for this was added to Level 46.
- You can use
static
on a local method or lambda to prevent closures/capturing local variables. The Closures section in Level 38 changed to show this. - If a lambda expression does not need a variable, it can now use a discard for the variable (Level 38).
- When you override a method, you can now specify a more specific return type (Level 26).
- When the compiler can infer the type, you no longer need to list the class name when using
new
to call a constructor:Point p = new (x, y);
. See the new subsection called “Leaving Off the Class Name” in Level 18. - Lots of new improvements to patterns, including the
and
,or
, andnot
patterns, as well as the>
,<
,<=
, and>=
patterns. See Level 40. init
accessors for properties, in addition toget
andset
. These cannot be set whenever, but can be set in the constructor or in object-initializer syntax. See the revised section called “Object Initializer Syntax and Init Properties” in Level 20.- Records are a new special type of class that is data-centric, with extremely compact definitions. A whole new level exists for records: Level 29.
- Top-level statements. More on that later.
Second, I made some rather significant changes to the internal visual design of the book, changing a number of fonts. I was able to increase the font size and reduce the page count (weird, right?), and every graphic designer I have talked to about it has suggested a serif font is better for long reading than a sans-serif font, which is what drove the change more than anything else. I suspect there are a few places here and there that didn’t get the appropriate font change. If something looks out of place to you visually, I’d love to hear about it. I’m using styles through and through, but there are a lot to manage, and it is easy to miss them here and there. I will still be going through everything with a fine-toothed comb at least one more time, but I could definitely use your eyes to help.
Third, I spent time getting an early version sent to the printer to make a physical copy. It takes time to get that going and to get a copy shipped, so I have yet to see the physical edition, but it should be available in the next few days. I’m generally expecting to get an updated physical copy every week starting now, to help track down those issues that only reveal themselves in print.
Now let’s talk about that top-level statement thing in more depth. C# 9 added a feature called top-level statements.
It allows you to ditch the whole namespace blah
, class Program
, and public static void Main(string[] args)
and
just write something like System.Console.WriteLine("Hello World!");
as a complete program. The compiler generates
a Program
class and a Main
method of sorts, so it is still technically doing the same thing. Just with less typing.
It is helpful for new programmers, because you don’t have to know what classes, namespaces, and methods are, nor static
,
void
, and string[]
. You can just start really basic. It is also helpful for everybody else too. I’m not sure
I’ll ever write another public static void Main
in my life. For large programs, this change has only the tiniest impact
on your overall code. But for small programs, it greatly reduces the amount of ceremony and pomp around creating a basic
program.
As you may know from my mid-week email asking for feedback on how to approach top-level statements, I was very
conflicted on how to address this. I could just leave things more or less as they are and put a small blurb late in the
book that said, “By the way, there’s this other syntax,” or I could change the entire beginning of the book to allow
people to ease into C# better by using top-level statements to get going. It was a hard decision (and even now, I continue
to wonder) but I decided that top-level statements are probably the way to go. It definitely eases the learning curve
for new programmers, and while 99.99% of current C# code uses the traditional Main
entry point, future code will
likely slant towards top-level statements. So lots of the book was reworked to account for this.
There is now a note right at the beginning of the book that has you turn on a feature in Visual Studio (16.8) to make
using top-level statements easy to begin with. Level 3 (Hello World) got a pretty substantial rewrite, including telling
you to delete the contents generated from the template and replace it with a top-level statement. The Methods level
got some pretty significant changes as well. So if you’re past that, you might want to skim it. And the Enumerations and
Classes levels got some adjustments, mostly around where to place new type definitions. Finally, Level 33, which used to
be about namespaces, is not about namespaces, splitting your code into multiple files, and the traditional Main
entry
points. Because methods created as a part of top-level statements are local functions, the section on local functions
was also moved up one level.
There is one important drawback: as the book currently stands, it assumes that you will have Visual Studio 16.9 in your hands. That is not out yet, and I was told by somebody who knows that it will not likely be released in December. Until it is out, you will have to enable the preview feature described in the note at the beginning. My initial thought is to delay the release of the book until Visual Studio 16.9 (16.x is Visual Studio 2019, if that helps avoid confusion with their double-versioning scheme) is out, probably in January or maybe February. I do still have the option of putting into the book something to the effect of, “If you are using a version before 16.9, go turn this thing on. Otherwise, you don’t have to do anything.” That would allow me to not have to wait for the final 16.9 release.
My decision about top-level statements is made, but it is tentative. I got a lot of comments (split fairly evenly) on what approach to take, and for many people, feelings were pretty strong. I’d be interested in hearing from you again now that you have a version to review in your hands. (Whether it changed your mind or not.)
The next big efforts are creating solutions to the challenges, updating the cover, and beginning the final, fine-grained revision pass (copyediting, indexing, checking that the sample code all compiles, etc.). We’re getting to the end!