5 Essential C# features

 

This chapter covers

  • Using C# language features for ASP.NET Core development
  • Dealing with null values and the null state analysis feature
  • Creating objects concisely
  • Adding features to classes without directly modifying them
  • Expressing functions concisely
  • Modifying interfaces without breaking implementation classes
  • Defining asynchronous methods

In this chapter, I describe C# features used in web application development that are not widely understood or that often cause confusion. This is not a book about C#, however, so I provide only a brief example for each feature so that you can follow the examples in the rest of the book and take advantage of these features in your projects. Table 5.1 provides a guide to this chapter.

5.1 Preparing for this chapter

5.1.1 Opening the project

5.1.2 Enabling the MVC Framework

5.1.3 Creating the application components

5.1.4 Selecting the HTTP port

5.1.5 Running the example application

5.2 Understanding top-level statements

5.3 Understanding global using statements

5.3.1 Understanding implicit using statements

5.4 Understanding null state analysis

5.4.1 Ensuring fields and properties are assigned values

5.4.2 Providing a default value for non-nullable types

5.4.3 Using nullable types

5.4.4 Checking for null values

5.4.5 Overriding null state analysis

5.4.6 Disabling null state analysis warnings

5.5 Using string interpolation

5.6 Using object and collection initializers

5.6.1 Using an index initializer

5.7 Using target-typed new expressions

5.8 Pattern Matching

5.8.1 Pattern matching in switch statements

5.9 Using extension methods