C# 7.0 and 7.1 support in ReSharper – Default literal
As part of our blog series about ReSharper support for C# 7.0 and C# 7.1, let’s talk about the
default literal (spec) which was added to C# 7.1. It lets us initialize variables in our code regardless of these variables being value types or reference types. ReSharper adds inspections, quick-fixes and context actions around this new language feature.
In this series:
We have been able to use
default(T) when we did not know whether
T would be a value type or reference type. Using
default(T) would return
null for reference types, or a zero-initialized value for value types.
Visual Studio 2017 15.3 introduced another nice C# 7.1 feature: the
default literal. It can be used instead of the
default(T) expression, inferring the target type based on usage context. It is shorter (
T could be a long class name), and as a bonus we can use it in most places where we would normally use
null – it is essentially the “
null” that works with both value and reference types.
ReSharper recognizes the
default literal syntax and comes with a code inspection that tells us when
default(T) is used and can be replaced by just
default. Of course, a quick-fix can remove the redundant type specification — either for one occurrence or for all occurrences in the current file, project or solution. And if you do prefer using
default(T), the quick-fix allows us to add the type specification again.
Subscribe to Blog updates
Thanks, we've got you!
Eager, Lazy and Explicit Loading with Entity Framework Core
Entity Framework Core (EF Core) supports a number of ways to load related data. There’s eager loading, lazy loading, and explicit loading. Each of these approaches have their own advantages and drawbacks. In this post, let’s have a quick look at each of these ways to load data for navigational prope…
OSS Power-Ups: bUnit – Webinar Recording
The recording of our webinar, OSS Power-Ups: bUnit, with Egil Hansen and Steven Giesel, is available. This was the twelfth episode of our OSS Power-Ups series, where we put a spotlight on open-source .NET projects. Subscribe to our community newsletter to receive notifications about future webi…
Accelerating Your Testing Workflow with Unit Test Creation and Navigation
Unit tests play an important role in our daily development workflow. They help us ensure our codebase's correctness when writing new functionality or performing refactorings to improve readability and maintainability. In the process, we often create new test files that accompany the p…
Introducing Predictive Debugging: A Game-Changing Look into the Future
With the introduction of debugging tools, software developers were empowered to interactively investigate the control flow of software programs to find bugs in live environments. At JetBrains, we've always strived to improve the art of debugging. Besides the more standard things you expect from a de…