News

What's New for C#, F# and Visual Basic in .NET 6

So, with the arrival of the unifying .NET 6 and 64-bit Visual Studio 2022 releases last week, what's new in C#, F# and Visual Basic?

Lots, says Microsoft's Kathleen Dollard, principal program manager, .NET, who explained everything in a series of blog posts. For the time-challenged, here's a summary, with links for those wanting to dive deeper:

C# 10

In detailing new features and functionality in Microsoft's flagship programming language, Dollard started out with global and implicit using directives, which let developers use types defined in a namespace without specifying the fully qualified namespace of that type. C# 10 features a new global using directive and implicit usings to reduce the number of usings needed, which are specified at the top of each file. Implicit usings, meanwhile, automatically adds common global using directives for specific types of projects.

File-scoped namespaces are another new feature, allowing for a simpler format in files containing only one namespace. This format is namespace X.Y.Z; (note the semicolon and lack of braces):

namespace X.Y.Z;

using System;

class X
{
}

Other highlights include:

  • Improvements for lambda expressions and method groups: "We've made several improvements to both the types and the syntax surrounding lambdas," Dollard said. "We expect these to be widely useful, and one of the driving scenarios has been to make ASP.NET Minimal APIs even more straightforward."
  • Improvements to structs: "C# 10 introduces features for structs that provide better parity between structs and classes. These new features include parameterless constructors, field initializers, record structs and with expressions." See more about natural types for lambdas and method groups, return types for lambdas, attributes on lambdas and more here.
  • Interpolated string improvements: "When we added interpolated strings to C#, we always felt that there was more that could be done with that syntax down the line, both for performance and expressiveness. With C# 10, that time has come!" For more on interpolated string handlers ("a library pattern that allows an API to 'take over' the handling of an interpolated string argument expression") and constant interpolated strings ("If all the holes of an interpolated string are constant strings, then the resulting string is now also constant. This lets you use string interpolation syntax in more places, like attributes") go here.

Smaller improvements listed by Dollard include:

More on new C# goodies is available in last month's Visual Studio Magazine article, "Q&A with Jim Wooley: C# Past, Present, and Beyond."

"People think [C# is] just a different syntax from JavaScript, Java, C, etc. and don't take the time to understand differences in memory models, object pointers. Oftentimes, they produce systems which have performance issues, or flat-out don't work right because they didn't understand what their code was doing."

Jim Wooley, Solution Architect, Slalom Consulting

"Some features improve performance of the framework APIs, like Span<T>, range operators and native ints," Wooley said "And then there are features to make getting started with simple projects easier, including top-level statements, static and global usings, and file-scoped namespace declarations. There are even some features that some might consider controversial, including tuples, nullable reference types and default interface implementations."

Jason Bock also weighed in with some C# 10 insights in the recent article, "Q&A with Jason Bock: What's New in C# 10."

"One aspect to watch is C#'s evolution over time and weighing the ability to add new features with potentially breaking the language to support a desirable concept. It's not an easy problem to solve."

Jason Bock, Microsoft MVP

When asked what C# 10 capability he is most excited about -- that he couldn't do without -- he replied: "Static abstract members in interfaces is a preview feature, so it technically doesn't count, but developers will be able to experiment with it. I think this feature brings another level of expressivity and design capabilities.

The caller argument expression feature isn't getting as much visibility as global using directives and lambda improvements, but it'll make scenarios like logging easier to determine what was done at the call site. I have the feeling this may be used in ways that go beyond capturing the expression as a string."

F# 6

The latest edition of Microsoft's functional programming language progresses on the goal of making it easier for developers to write robust, succinct and performant code, Dollard said.

"F# 6 is about making F# simpler and more performant," she explained in an earlier post. "This applies to the language design, library, and tooling. A major goal of long-term language evolution is to remove corner-cases in the language that surprise users or are unnecessary hurdles on the path to adoption. We are very pleased to have worked with the F# community in this release to make the F# language simpler, more performant, and easier to learn."

Here are the highlights of her new post about F# 6 in .NET 6:

  • Template simplification: "We've simplified the Console template for F#. The new template is:
    // For more information see https://aka.ms/fsharp-console-apps
    printfn "Hello from F#"
  • Updated Hello World tutorial with Next Steps video: "If you're new to F#, a great place to start is the F# Tutorial – Hello World in 5 minutes. This tutorial has been updated for F# 6 and now includes a link to Luis Quintanilla's great F# 101 Series on the Next Steps page."
  • Documentation updates:
  • Fantomas: "Fantomas, the F# code formatting tool for F#, is now available for Visual Studio 2022. Search for "fantomas" in the Visual Studio Marketplace or in Extensions > Manage Extensions > Online > Search."
  • Ionide (cross platform developer tools for F#): "Ionide now supports F# 6 features and syntax. You can update your project to .NET 6 right now and everything should work great. If you encounter any issues, report them in the Ionide repo."

Other highlights in the article "What's new in F# 6" include:

"With F# 6, the language continues to deliver on its goals since its inception --succinct, robust, performant code that you can trust for practical coding, whether for personal use, start-ups or the enterprise," Dollard concluded. "We love working together with the F# community to advance the language and its tooling, and encourage you to take F# 6 for a spin, and to join us in advancing it further!"

Visual Basic

Even though Microsoft long ago announced "We Do Not Plan to Evolve Visual Basic as a Language," Dollard explained "What's New for Visual Basic in Visual Studio 2022," starting out with fonts.

"Visual Studio 2022 has a new look, with the new Cascadia font and updated icons," she said. "If you have customized your font, you may need to explicitly set your font to Cascadia. You have several Cascadia choices with different weights and two styles: Mono and Code. Cascadia Mono is the default. Choose Cascadia Code if you would like ligatures. Ligatures express two or more characters as a single unit. For Visual Basic, this mainly affects the appearance of >=, <= and <>."

Blazor Hot Reload in Animated Action
[Click on image for larger, animated GIF view.] Blazor Hot Reload in Animated Action (circa May 2021) (source: Microsoft).

Dollard also discussed Hot Reload, a much-requested and long-awaited feature that allows for a developer to make a change in source code and instantly see the change reflected in a running application, with no need for a reload or restart. Hot Reload in .NET 6 became controversial when Microsoft recently decided to limit Hot Reload functionality to only be available in VS 2022 and not via the command line, where it worked in earlier previews. After receiving negative developer feedback on that decision, it was quickly reversed.

'We Listen and Learn'
[Click on image for larger view.] 'We Listen and Learn' (source: Twitter).

Hot Reload has been infused into much .NET tooling with .NET 6, but the process is ongoing.

"Hot Reload is a core technology that covers many scenarios, however there are some edits that are not supported at this time," Dollard said. "For example, if you move a WinForms control or change something in Properties, that change won't currently appear in Hot Reload for C# or Visual Basic. Also, if you add a control or other object that uses WithEvents to a Visual Basic application, you will receive a message that that you've made an unsupported change.

"Hot Reload will cover more scenarios as it evolves in upcoming releases. Try it out with your applications to simplify the way you interact with Edit and Continue."

Other Visual Basic highlights include:

  • Debugging: In Visual Studio 2022, as developers move a cursor within the left margin, a dot will appear that can be clicked to find available types of breakpoints to insert, including: conditional, tracepoint, temporary and dependent breakpoints. See more about debugging here.
    Different Types of Breakpoints
    [Click on image for larger view.] Different Types of Breakpoints (source: Microsoft).
  • Editor: Dollard said a number of new features in the editor make everyday coding smoother and more efficient, including:
    • Subword navigation: "You probably use Ctrl-Left and Ctrl-Right to move one word to the left and right. In Visual Studio 2022, you can use Ctrl-Alt-Left and Ctrl-Alt-Right to move left and right by parts of words (supports the Pascal style of symbol naming)."
    • Inheritance Margin: "The inheritance margin adds icons to the left margin representing where code is derived from other code, and where other code derives from this code."
    • Underline reassigned: "The new underline reassigned feature is for folks that want to know whether variables are reassigned, or remain set to their initial value."
  • IntelliSense for preprocessor symbols: Preprocessor symbols now have IntelliSense, with available symbols appearing after a coder types "#If." Also, when turned on, "parameter name hints" display a small indicator for the parameter name, and Visual Studio 2022 will add required namespace imports when a developer pastes in a code block containing a code element requires a namespace import.
  • Refactoring: New functionality here include:
  • WinForms startup: "The Visual Basic Application Model provides the familiar Visual Basic experience for application startup, particularly for WinForms and WPF applications. This model has been updated in .NET 6.0 to support a new event: ApplyApplicationDefaults. This event lets you set application wide values that must be set before any forms or controls are created." Also explained are support for C# init properties and Roslyn Source Generators.

"As you can see here, the experience of Visual Basic just keeps getting better," Dollard said in conclusion. " We've enabled many new features in Visual Studio 2022 and we're excited for you to download the new release and give it a try on your Visual Basic solutions."

As far as TypeScript, another prominent programming language from Microsoft, v4.4 shipped in August. Here are links to learn about some changes there.

Of course, Microsoft is continually evolving its programming languages, so stay tuned for more coverage on developments in C#, F#, TypeScript and Visual Basic.

comments powered by Disqus

Featured

Subscribe on YouTube