Book Image

Learn C# Programming

By : Marius Bancila, Raffaele Rialdi, Ankit Sharma
5 (1)
Book Image

Learn C# Programming

5 (1)
By: Marius Bancila, Raffaele Rialdi, Ankit Sharma

Overview of this book

The C# programming language is often developers’ primary choice for creating a wide range of applications for desktop, cloud, and mobile. In nearly two decades of its existence, C# has evolved from a general-purpose, object-oriented language to a multi-paradigm language with impressive features. This book will take you through C# from the ground up in a step-by-step manner. You'll start with the building blocks of C#, which include basic data types, variables, strings, arrays, operators, control statements, and loops. Once comfortable with the basics, you'll then progress to learning object-oriented programming concepts such as classes and structures, objects, interfaces, and abstraction. Generics, functional programming, dynamic, and asynchronous programming are covered in detail. This book also takes you through regular expressions, reflection, memory management, pattern matching, exceptions, and many other advanced topics. As you advance, you'll explore the .NET Core 3 framework and learn how to use the dotnet command-line interface (CLI), consume NuGet packages, develop for Linux, and migrate apps built with .NET Framework. Finally, you'll understand how to run unit tests with the Microsoft unit testing frameworks available in Visual Studio. By the end of this book, you’ll be well-versed with the essentials of the C# language and be ready to start creating apps with it.
Table of Contents (20 chapters)

Nullable types

Reference types have the default value null, which indicates that a variable is not assigned to the instance of any object. Value types do not have such an option. However, there are cases when no value is a valid value for a value type too. To represent such cases, you can use a nullable type.

A nullable type is an instance of System.Nullable<T>, a generic value type that can represent the values of an underlying T type, which can only be a value type, as well as an additional null value. The following sample shows a few examples:

Nullable<int> a;
Nullable<int> b = null;
Nullable<int> c = 42;

You can use the shorthand syntax, T?, instead of Nullable<T>; these two are interchangeable. The following examples are alternatives for the preceding ones:

int? a;
int? b = null;
int? c = 42;

You can use the HasValue property to check whether a nullable type object has a value, and Value to access the underlying value:

if (c.HasValue)

The following is a list of some of the characteristics of nullable types:

  • You assign values to a nullable type object the same way you would assign to the underlying type.
  • You can use the GetValueOrDefault() method to get either the assigned value or the default value of the underlying type if no value is assigned.
  • Boxing is performed on the underlying type. If the nullable type object has not assigned any value, the result of boxing is a null object.
  • You can use the null-coalescing operator, ??, to access the value of the object of a nullable type (for example, int d = c ?? -1;).

In C# 8, nullable reference types and non-nullable reference types have been introduced. That is a feature that you must opt for in the project properties. It allows you to make sure that only objects of reference types that are declared nullable, using the T? syntax can be assigned the null value. Attempts to do so on non-nullable reference types will result in a compiler warning (not an error, because that has the potential to affect large portions of existing code):

string? s1 = null; // OK, nullable type
string s2 = null;  // error, non-nullable type

You will learn more about nullable reference types in Chapter 15, New Features of C# 8.