C# 13 Programming Essentials - .NET 9 Edition - Neil Smyth - E-Book

C# 13 Programming Essentials - .NET 9 Edition E-Book

Neil Smyth

0,0
26,99 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.
Mehr erfahren.
Beschreibung

This book is a comprehensive guide to mastering the C# 13 programming language and the .NET 9 SDK, providing a thorough learning experience for both beginners and experienced developers. With 31 detailed chapters, 30 online knowledge quizzes, and access to downloadable project code, this guide is structured to support your journey from basic concepts to advanced programming techniques, all within the Visual Studio Code environment.


We will start by introducing the foundational components of the C# ecosystem, providing an overview of the Common Language Infrastructure (CLI), the Common Intermediate Language (CIL), and the Virtual Execution System (VES). These initial sections will offer a deeper understanding of the platform’s architecture. Following that, we will provide step-by-step instructions for installing and using the Visual Studio Code environment.


As you advance, you will learn about the foundational principles of the C# language. This will include exploring topics such as variables, constants, operators, flow control, and loops. These basic concepts will then be expanded to cover object-oriented programming (OOP), with an emphasis on inheritance, abstract classes, interfaces, and structures.


Beyond the basics, this book advances into more specialized areas, including string manipulation, working with arrays and collections, handling exceptions, and implementing asynchronous programming techniques. These sections are designed to deepen your understanding and equip you with the skills necessary to build more sophisticated apps.


Whether you are new to programming or seeking to refine your skills, C# 13 Programming Essentials - .NET 9 Edition offers the knowledge and practical guidance you need to succeed in C# development.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
MOBI

Seitenzahl: 266

Veröffentlichungsjahr: 2024

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



C# 13 Programming

Essentials

.NET 9 Edition

C# 13 Programming Essentials - .NET 9 Edition

ISBN-13: 978-1-965764-01-5

© 2024 Neil Smyth / Payload Media, Inc. All Rights Reserved.

This book is provided for personal use only. Unauthorized use, reproduction and/or distribution strictly prohibited. All rights reserved.

The content of this book is provided for informational purposes only. Neither the publisher nor the author offers any warranties or representation, express or implied, with regard to the accuracy of information contained in this book, nor do they accept any liability for any loss or damage arising from any errors or omissions.

This book contains trademarked terms that are used solely for editorial purposes and to the benefit of the respective trademark owner. The terms used within this book are not intended as infringement of any trademarks.

Rev: 1.0

Copyright

https://www.payloadbooks.com

Table of Contents

1. Start Here

1.1 Source code download

1.2 Feedback

1.3 Errata

1.4 Take the knowledge tests

2. The History of Programming Languages and C#

2.1 The history of programming languages

2.2 What exactly is C#?

2.3 Who is using C#?

2.4 Take the knowledge test

2.5 Summary

3. C# Infrastructure

3.1 The Common Language Infrastructure (CLI)

3.2 Common Intermediate Language (CIL)

3.3 Virtual Execution System (VES)

3.4 Common Type System (CTS) & Common Language Specification (CLS)

3.5 The Framework (Base Class and Framework Class Libraries)

3.6 Implementations of the CLI

3.7 Take the knowledge test

3.8 Summary

4. Installing Visual Studio Code, C# and .NET 9

4.1 Installing the .NET 9 SDK

4.1.1 Installing .NET 9 on Windows

4.1.2 Installing .NET 9 on macOS

4.1.3 Installing .NET 9 on Red Hat Linux-based distributions

4.1.4 Installing the .NET 9 on Debian/Ubuntu distributions

4.1.5 Installing .Net 9 from the Linux binaries

4.2 Downloading Visual Studio Code

4.3 Installing on Windows

4.4 Installing on macOS

4.5 Installing on Linux

4.5.1 Debian/Ubuntu

4.5.2 Red Hat-based distributions

4.6 Customizing Visual Studio Code

4.7 Adding the C# Dev Kit extension

4.8 Take the knowledge test

4.9 Summary

5. A Guided Tour of Visual Studio Code

5.1 Workspaces, projects, and solutions

5.2 The VS Code main window

5.3 The Command Palette

5.4 Splitting the editor window

5.5 Zen mode

5.6 Learning with the Editor Playground

5.7 Take the knowledge test

5.8 Summary

6. Creating an Example C# App in VS Code

6.1 Creating the SampleApp project

6.2 Reviewing the SampleApp project

6.3 Writing the app code

6.4 Top-level statements and the Main method

6.5 Take the knowledge test

6.6 Summary

7. C# Variables and Constants

7.1 What is a C# variable?

7.2 Type annotations and implicit typing

7.3 What is a C# constant?

7.4 Integer types

7.5 Floating-point Variables

7.6 Decimal type

7.7 Boolean type

7.8 Character data type

7.9 Special characters

7.10 String data type

7.11 Verbatim string literals

7.12 String interpolation

7.13 Type casting

7.13.1 Implicit casting

7.13.2 Explicit casting

7.13.3 Identifying variable’s type

7.14 Take the knowledge test

7.15 Summary

8. C# Operators and Expressions

8.1 What is an expression?

8.2 The basic assignment operator

8.3 C# arithmetic operators

8.4 C# operator precedence

8.5 Compound assignment operators

8.6 Increment and decrement operators

8.7 Comparison operators

8.8 Boolean logical operators

8.9 Range and index operators

8.10 The ternary operator

8.11 Null-coalescing operators

8.12 Bitwise Operators

8.12.1 Bitwise NOT

8.12.2 Bitwise AND

8.12.3 Bitwise OR

8.12.4 Bitwise XOR

8.12.5 Bitwise Left Shift

8.12.6 Bitwise Right Shift

8.13 Compound Bitwise Operators

8.14 Take the knowledge test

8.15 Summary

9. C# Conditional Control Flow

9.1 Looping vs. conditional control flow

9.2 Using the if statement

9.3 Using if … else … statements

9.4 Using if … else if … statements

9.5 Take the knowledge test

9.6 Summary

10. The C# switch Statement

10.1 Why use the switch statement?

10.2 Using the switch statement

10.3 A switch statement example

10.4 Explaining the example

10.5 Using switch expressions

10.6 Using goto in a C# switch statement

10.7 Using continue in a C# switch statement

10.8 Take the knowledge test

10.9 Summary

11. C# Looping with the for Statement

11.1 Why use loops?

11.2 C# loop variable scope

11.3 Creating an infinite for loop

11.4 Breaking out of a for loop

11.5 Nested for loops

11.6 Breaking from nested loops

11.7 Continuing for loops

11.8 The C# foreach statement

11.9 Take the knowledge test

11.10 Summary

12. C# Looping with do and while Statements

12.1 The C# while loop

12.2 C# do … while loops

12.3 Breaking from loops

12.4 The continue statement

12.5 Take the knowledge test

12.6 Summary

13. An Introduction to C# Object-Oriented Programming

13.1 What is an object?

13.2 What is a class?

13.3 Creating the ObjectDemo project

13.4 Declaring a C# class

13.5 Access modifiers

13.6 Creating C# class members

13.7 Fields vs. properties

13.8 Adding members to the BankAccount class

13.9 Static, read-only, and const data members

13.10 Instantiating an object from a C# class

13.11 Accessing C# object members

13.12 Adding methods to a C# class

13.13 C# constructors

13.14 C# primary constructors

13.15 C# finalizers

13.16 The “this” keyword

13.17 Take the knowledge test

13.18 Summary

14. C# Methods

14.1 What is a method?

14.2 Methods vs. functions

14.3 Parameter or argument?

14.4 How to declare a C# function

14.5 Creating the MethodDemo project

14.6 Declaring a C# class file

14.7 Calling a C# method

14.8 Handling return values

14.9 Passing arguments to a method

14.9.1 Named arguments

14.9.2 Optional arguments

14.10 Handling variable numbers of arguments

14.11 C# in, out, and ref parameter modifiers

14.12 Take the knowledge test

14.13 Summary

15. C# Delegates

15.1 What is a delegate?

15.2 Declaring a delegate

15.3 Using delegates

15.4 Creating the DelegateDemo project

15.5 A C# delegate example

15.6 Multicast delegates

15.7 Passing a delegate to a method

15.8 Take the knowledge test

15.9 Summary

16. C# Anonymous Methods, Lambdas, and Local Functions

16.1 Anonymous methods

16.2 Creating the LambdaDemo project

16.3 Lambdas

16.4 Passing multiple parameters

16.5 Passing no parameters

16.6 Local functions

16.7 Creating the LocalFunctions project

16.8 Recursive local functions

16.9 Lambdas vs. local functions

16.10 Take the knowledge test

16.11 Summary

17. C# Inheritance

17.1 What is inheritance?

17.2 A C# inheritance example

17.3 Creating a subclass in C#

17.4 Passing arguments to the base class constructor

17.5 Overriding inherited methods

17.6 Take the knowledge test

17.7 Summary

18. C# Abstract Classes

18.1 What is a C# abstract class?

18.2 Abstract members

18.3 Declaring a C# abstract class

18.4 Deriving from an abstract class

18.5 Creating the AbstractClassDemo project

18.6 Abstract vs. virtual members

18.7 Take the knowledge test

18.8 Summary

19. C# Interfaces

19.1 Understanding C# interfaces

19.2 Declaring an interface

19.3 Creating the InterfaceDemo project

19.4 Adding a C# interface file

19.5 Interface data members

19.6 Take the knowledge test

19.7 Summary

20. C# Structures

20.1 An overview of C# structures

20.2 Creating the StructureDemo project

20.3 Value types vs. reference types

20.4 Adding the Struct declaration

20.5 Read-only structures

20.6 Take the knowledge test

20.7 Summary

21. C# Tuple Type

21.1 Creating the TupleDemo project

21.2 Declaring a Tuple

21.3 Accessing tuple values

21.4 Tuple field names

21.5 Returning tuples from methods

21.6 Tuples as method parameters

21.7 Take the knowledge test

21.8 Summary

22. C# Namespaces

22.1 Understanding scope

22.2 An overview of namespaces

22.3 Creating the NamespaceDemo project

22.4 Declaring namespaces

22.5 Accessing namespace members

22.6 Namespaces and the “using” keyword

22.7 Nested namespaces

22.8 Aliases and the namespace alias operator

22.9 File scoped namespaces

22.10 Take the knowledge test

22.11 Summary

23. C# Exception Handling

23.1 Understanding exception handling

23.2 Creating the ExceptionDemo project

23.3 Throwing exceptions

23.4 Handling exceptions

23.5 Creating exception filters with the “when” keyword

23.6 Using finally blocks

23.7 Using the try-finally Statement

23.8 Re-throwing exceptions

23.9 Creating custom exception types

23.10 Take the knowledge test

23.11 Summary

24. Asynchronous Programming in C#

24.1 An overview of threads

24.2 The main thread

24.3 Asynchronous programming in C#

24.4 Creating the AsyncDemo project

24.5 Synchronous code

24.6 Understanding the C# Task class

24.7 Declaring asynchronous methods

24.8 Introducing the await operator

24.9 Asynchronous calls from synchronous functions

24.10 Canceling asynchronous tasks

24.11 Take the knowledge test

24.12 Summary

25. Creating 2D, 3D, and Jagged Arrays in C#

25.1 Creating arrays in C#

25.2 Declaring multidimensional arrays

25.3 Declaring jagged arrays

25.4 Take the knowledge test

25.5 Summary

26. Accessing and Sorting C# Array Elements

26.1 Creating the ArrayDemo Project

26.2 Getting the number of dimensions of an array

26.3 Accessing array elements

26.4 Array iteration

26.5 Working with ranges

26.6 C# index from end operator

26.7 Sorting C# arrays

26.8 Clearing C# arrays

26.9 Take the knowledge test

26.10 Summary

27. C# List Collections

27.1 Creating the CollectionDemo Project

27.2 Introducing the C# Collection Classes

27.3 Creating C# list collections with List<T>

27.4 Adding items to lists

27.5 Initializing a list with multiple values

27.6 Accessing list items

27.7 Removing items from lists

27.8 Inserting items into a list

27.9 Sorting lists in C#

27.10 Finding items in a C# list

27.11 Obtaining information about a list

27.12 Clearing C# lists

27.13 Take the knowledge test

27.14 Summary

28. C# Dictionary Collections

28.1 Creating the DictionaryDemo Project

28.2 Dictionary initialization

28.3 Dictionary item count

28.4 Dictionary iteration

28.5 Adding and removing dictionary entries

28.6 Accessing and updating dictionary items

28.7 Checking if a key or value exists

28.8 Take the knowledge test

28.9 Summary

29. C# File and Directory Handling

29.1 Introducing the System.IO classes

29.2 Understanding paths in C#

29.3 Creating the FileDemo project

29.4 Current working directory

29.5 Checking if a directory exists

29.6 Creating a Directory

29.7 Deleting a Directory

29.8 Changing the current working directory

29.9 Handling File and Directory Exceptions

29.10 File handling using the File class

29.10.1 Checking if a file exists

29.10.2 Creating and opening files

29.10.3 Reading and writing using the File class

29.11 Deleting, copying, and moving files

29.12 File I/O with streams

29.13 Asynchronous file handling

29.14 Working with binary streams

29.15 Take the knowledge test

29.16 Summary

30. C# Strings

30.1 Creating the StringsDemo Project

30.2 Creating strings in C#

30.3 Obtaining the length of a C# string

30.4 Treating strings as arrays

30.5 String character iteration

30.6 Concatenating strings

30.7 Comparing strings

30.8 Changing string case

30.9 Splitting a string into multiple parts

30.10 Trimming and padding strings

30.11 String replacement

30.12 Take the knowledge test

30.13 Summary

31. C# String Formatting

31.1 The Syntax of the string Format() method

31.2 Creating the StringFormatDemo project

31.3 A simple string formatting example

31.4 Using format controls

31.5 A simple format control example

31.6 C# string Format() format controls

31.7 Take the knowledge test

31.8 Summary

32. C# Dates and Times

32.1 Creating the DateTimeDemo project

32.2 Creating a C# DateTime object

32.3 Getting the current system time and date

32.4 Adding to or subtracting from dates and times

32.5 Retrieving parts of a date and time

32.6 Take the knowledge test

32.7 Summary

Index

1. Start Here

This book is a comprehensive guide to mastering the C# 13 programming language and the .NET 9 SDK, providing a thorough learning experience for both beginners and experienced developers. With 31 detailed chapters, 30 online knowledge quizzes, and access to downloadable project code, this guide is structured to support your journey from basic concepts to advanced programming techniques, all within the Visual Studio Code environment.

We will start by introducing the foundational components of the C# ecosystem, providing an overview of the Common Language Infrastructure (CLI), the Common Intermediate Language (CIL), and the Virtual Execution System (VES). These initial sections will offer a deeper understanding of the platform’s architecture. Following that, we will provide step-by-step instructions for installing and using the Visual Studio Code environment.

As you advance, you will learn about the foundational principles of the C# language. This will include exploring topics such as variables, constants, operators, flow control, and loops. These basic concepts will then be expanded to cover object-oriented programming (OOP), with an emphasis on inheritance, abstract classes, interfaces, and structures.

Beyond the basics, this book advances into more specialized areas, including string manipulation, working with arrays and collections, handling exceptions, and implementing asynchronous programming techniques. These sections are designed to deepen your understanding and equip you with the skills necessary to build more sophisticated apps.

Whether you are new to programming or seeking to refine your skills, C# 13 Programming Essentials - .NET 9 Edition offers the knowledge and practical guidance you need to succeed in C# development.

1.1 Source code download

The source code and Visual Studio Code project files for the examples contained in this book can be downloaded using the following link:

https://www.payloadbooks.com/product/csharp13/

After downloading and unzipping the file, you will find the projects in the “C# Essentials” workspace folder. In Visual Studio Code, you can open the workspace by selecting File -> Open Folder... from the menu bar, navigating to the workspace folder, and clicking Open to load the projects.

1.2 Feedback

We want you to be satisfied with your purchase of this book. Therefore, if you find any errors in the book or have any comments, questions, or concerns, please contact us at [email protected].

1.3 Errata

While we make every effort to ensure the accuracy of the content of this book, inevitably, a book covering a subject area of this size and complexity may include some errors and oversights. Any known issues with the book will be outlined, together with solutions, at the following URL:

https://www.payloadbooks.com/csharp13-errata/

If you find an error not listed in the errata, please email our technical support team at [email protected].

1.4 Take the knowledge tests

Look for this section at the end of most chapters and use the link or scan the QR code to take a knowledge quiz to test and reinforce your understanding of the covered topic. Use the following link to review the full list of tests available for this book:

https://www.answertopia.com/4my3

2. The History of Programming Languages and C#

C# is part of the never-ending quest to make programming computers as easy and efficient as possible for humans. While it is easy to describe C# as another object-oriented programming language, the fact is that C# is an integral part of an entire development and execution infrastructure. The primary object of these initial chapters is to provide an overview of both the C# language and the infrastructure on which it relies.

2.1 The history of programming languages

The problem with programming is that computers think exclusively in numbers (0 and 1, to be precise), known as machine code, while humans communicate using words. In the early days, programmers entered machine code directly into computers to program them. This, as you can imagine, was a laborious and error-prone process. The next evolution was associating brief human-readable commands with the corresponding machine code. For example, a programmer could enter the command MOV to transfer a value from one microprocessor register to another. These commands would then be translated into machine code by a piece of software called an assembler, thereby giving this command syntax the name Assembly Language.

Next came a series of high-level languages designed to make it easier for humans to write programs. These programs are written using a human-readable syntax and then either compiled to machine code by a compiler or interpreted by an interpreter on the processor’s behalf. Such languages include BASIC, COBOL, Pascal, and Fortran. Another language is C, which was created at AT&T Bell Labs in the late 1960s and early 1970s. Then, in the late 1970s and early 1980s, work started on an object-oriented approach to C programming, culminating in a new, object-oriented variant of C known as C++.

The story, however, continues. The problem with C++ was that it was too easy to make programming mistakes. For example, C++ would quite happily allow a programmer to make coding mistakes that would cause buffers to overflow, memory locations to be arbitrarily overwritten, and introduce memory leaks that would cause applications to bloat to the point of using up the entire physical memory and swap space on a system. Another problem encountered with C, C++, and all other compiled languages is that the source code must be recompiled for each processor type, making it difficult to port an application from one hardware platform to another.

To address the shortcomings of C and C++, Sun Microsystems started work on a new programming language and execution environment in the 1990s. The result was called Java. Java consists of a programming language with many of the pitfalls of C++ removed, a portable intermediate byte code format, a runtime environment (called the virtual machine) that executes the byte code and handles issues such as memory management, and a vast suite of libraries providing all the functionality required to develop enterprise-class applications (such as networking, file handling, database access, graphics, etc.).

Java gained rapid acceptance, and for a time, Microsoft began its Java “embrace and extend” campaign. Sun was happy for Microsoft to embrace Java but reached for their lawyers when they realized that the “extend” part was a plan for Microsoft to introduce their own proprietary version of the language. Politics ensued, and Microsoft eventually walked away from Java. Soon after, Microsoft started talking about something called .NET, followed by a programming language called C#.

2.2 What exactly is C#?

“What does all this history have to do with C#?” I hear you ask. The origins of the C# programming syntax can be traced back to C and C++. If you are already familiar with C or C++ (or even Kotlin or Swift, for that matter), then you have a big head start in learning C#. The same can be said of syntax similarities between Java, C, C++, and C# syntax. In addition, C# also inherits many of the benefits of Java in terms of memory handling (better known as garbage collection) and an intermediate byte code that negates the need to recompile an application for each target hardware platform. C# is also accompanied by a vast framework of libraries designed to provide the programmer with ready-made solutions to almost every imaginable scenario.

Despite these similarities, there are differences between the Java and C# infrastructures. Therefore, the next chapter will provide an overview of the C# infrastructure.

2.3 Who is using C#?

Although some significant new programming languages have been released since the introduction of C# (such as Swift and Kotlin), C# is still evolving as a language and has a vast and growing user base.

At the time of writing, for example, C# was ranked on the TIOBE Index in 5th place among the top 50 most widely used programming languages (eclipsed only by C, Java, Python, and C++).

C# is used to develop desktop, mobile, and server applications, as well as web-based applications and services. It is the programming language of choice on all Microsoft platforms and is used to program within the Unity Real-Time Development Platform.

2.4 Take the knowledge test

Click the link below or scan the QR code to test your knowledge and understanding of the history of C#:

https://www.answertopia.com/qoe2

2.5 Summary

C# is a programming language created by Microsoft that combines elements from other popular languages like C, C++, and Java. It’s designed to make programming simpler and more efficient with features like memory management to reduce errors and intermediate byte code for compatibility across platforms. C# is used for desktop, mobile, server, web, and game development and is among the top 10 most widely used programming languages.

3. C# Infrastructure

This chapter will introduce the infrastructure on which the C# language is built. By the end of the chapter, you will have a clear understanding of what acronyms such as CLI, CLR, VES, JIT, and .NET mean.

3.1 The Common Language Infrastructure (CLI)

C# is an object-oriented programming language. It is essentially a standard defining what constitutes valid syntax. On its own, C# is of little use because it depends upon the Common Language Infrastructure (CLI) for the compilation and execution of applications. The CLI, in turn, is a standard that defines specifications for the following components:

•Virtual Execution System (VES)

•Common Intermediate Language (CIL)

•Common Type System (CTS)

•Common Language Specification (CLS)

•Framework

In the remainder of this chapter, we will examine each CLI component to understand how the CLI environment fits together.

3.2 Common Intermediate Language (CIL)

Unlike the C and C++ compilers, which compile the source code to machine code understood by the target microprocessor, the C# compiler compiles to an intermediate byte code format known as the Common Intermediate Language (CIL). This code can be executed on any system with a CLI-compliant Virtual Execution System (VES). Therefore, there is no need to compile an application for every target platform.

The word “Common” in Common Intermediate Language is used because this format is common to more than just the C# programming language. In fact, any programming language may target the CIL, allowing libraries and code modules from different languages to execute together in the same application. Typical languages for which CIL compilation is available include Visual Basic, COBOL, PowerShell, Python, and C++.

3.3 Virtual Execution System (VES)

The VES (referred to as the runtime) is the environment in which the CIL byte code is executed. The VES reads the byte code generated by the C# compiler and uses a Just in Time (JIT) compiler to compile the byte code down to the native machine code of the processor on which it is running. While executing, this code does so in conjunction with a runtime agent, which manages the execution process. As a result, this executing code is known as managed code, and the process handles issues such as garbage collection (to handle memory allocation and de-allocation), memory access, and type safety to ensure that the code does not do anything it is not supposed to do.

A term often used in connection with the VES is the Common Language Runtime (CLR). The CLR is officially the name given to Microsoft’s implementation of the VES component of the CLI specification.

It is worth noting that the JIT process can introduce a startup delay in the execution of an application. One option available with .NET to avoid this problem is pre-compiling CLI byte code to native machine code using the NGen compiler. However, because the NGen compilation must occur on the target processor architecture, this step is often performed when the user installs the application.

3.4 Common Type System (CTS) & Common Language Specification (CLS)

As mentioned previously, several different programming languages target the CLI, allowing, for example, code from C# sources to interact with code from Visual Basic. However, to achieve this feat, each language must have the same concept of how data types are stored in memory. The CTS, therefore, defines how a CLI-compatible language must view the bit patterns of values and the layout and behavior of objects to ensure interoperability.

The CLS is a subset of the CTS aimed at creating interoperable libraries.

3.5 The Framework (Base Class and Framework Class Libraries)

The CLI specifies a set of base classes that must be available to execute CLI code, otherwise known as the Base Class Library (BCL). The BCL contains Application Programming Interfaces (APIs) that enable executing CIL code to interact with the runtime environment and the underlying operating system.

Beyond the basics, there is also the .NET Framework Class Library (FCL). This Microsoft library contains APIs for creating graphical user interfaces, database applications, web access, and more.

3.6 Implementations of the CLI

Microsoft’s implementation of the CLI stack is called .NET. Initially only available on Windows systems, Microsoft repackaged .NET in 2014 as an open-source, cross-platform development platform available on Windows, Linux, and macOS.

Another implementation Microsoft provides is Shared Source Common Language Infrastructure (SSCLI). SSCLI, however, is primarily a learning tool and, as such, is licensed under terms that prohibit use as the basis of commercial applications.

Finally, if you would like to develop apps for iOS and Android using C#, there is also the .NET MAUI implementation.

3.7 Take the knowledge test

Click the link below or scan the QR code to test your knowledge and understanding of C# Infrastructure:

https://www.answertopia.com/kbxm

3.8 Summary

The Common Language Infrastructure (CLI) provides a standardized environment for executing C# code. It includes the Common Intermediate Language (CIL), Virtual Execution System (VES), Common Type System (CTS), and Common Language Specification (CLS). The CLI enables interoperability between different programming languages and provides a base class library for executing CIL code.

In addition, the .NET Framework Class Library offers a vast collection of pre-built functions and classes for creating diverse applications and includes APIs for file I/O, networking, database access, and user interface design.

4. Installing Visual Studio Code, C# and .NET 9

To begin programming in C#, we will need an environment to write, edit, compile, and run our code. There are many options, but for this book, we need a solution that is free to use and available to all readers, regardless of whether they are learning C# on macOS, Windows, or Linux systems. A popular tool, and the one we will be installing in this chapter, is Visual Studio Code. Developed by Microsoft, Visual Studio Code is a feature-rich, intuitive code editing environment, which, when combined with the C# Dev Kit extension, provides everything we need to learn C#.

4.1 Installing the .NET 9 SDK

In addition to installing Visual Studio Code, we will also need to install the .NET 9 SDK prior to compiling C# programs. While there are several ways to install .NET, we will use a technique that involves downloading and install the SDK independently from Visual Studio Code.

The .NET 9 SDK installation steps are specific to the operating system you are using as outlined below.

4.1.1 Installing .NET 9 on Windows

The .NET 9 SDK is available for Windows systems running on x86, x64, and Arm64 processor architectures. Using the following link, download the latest .NET 9 Installer package that matches your system:

https://dotnet.microsoft.com/en-us/download/dotnet/9.0

Once you have downloaded the installer, launch it and follow the setup wizard to complete the installation:

Figure 4-1

4.1.2 Installing .NET 9 on macOS

The macOS edition of the .NET 9 SDK is available or Intel and Apple Silicon systems. To begin the installation, open a browser window, navigate to the following URL, and download the Installer that matches your processor architecture:

https://dotnet.microsoft.com/en-us/download/dotnet/9.0

After downloading the Installer package, launch it and follow the steps to complete the installation:

Figure 4-2

4.1.3 Installing .NET 9 on Red Hat Linux-based distributions

The best way to install the .NET 9 SDK on Linux distributions derived from Red Hat Enterprise Linux will depend on when you are reading this. At the time of writing, .Net 9 was still at the release candidate stage. It is possible that by the time you read this, .NET 9 will have been officially released. To find out, open a Terminal window and run the following command:

$ sudo dnf install dotnet-sdk-9.0

If the installation request reports that there is no match for dotnet-sdk-9.0, skip to the “Installing .Net 9 from the Linux binaries” section below to perform the installation. Otherwise, press ‘y’ to complete the installation.

4.1.4 Installing the .NET 9 on Debian/Ubuntu distributions

The installation steps for Debian and Ubuntu systems will, once again, depend on the release status of the .NET 9 SDK. Begin by running the following common in a Terminal window:

$ sudo apt-get install dotnet-sdk-9.0

If the installation request reports that the dotnet-sdk-9.0 package could not be found, skip to the next section to perform the installation. Otherwise, press ‘y’ to complete the installation.

4.1.5 Installing .Net 9 from the Linux binaries

If .NET 9 has not yet been officially released, the installation will need to be performed using the release candidate SDK binaries. Return to the .NET 9 download page and locate and download the binary that matches your CPU as highlighted in Figure 4-3:

Figure 4-3

Within a Terminal window, create a directory into which you would like to install .NET 9. Next, run the following commands (which assume you chose $HOME/dotnet9 as your installation directory):

$ cd $HOME/dotnet9

$ tar xvfz <path to downloaded binary archive>

After unpacking the .NET package, modify your $HOME/.bashrc file to export the DOTNET_ROOT environment variable and to add the .NET 9 SDK to your path:

export DOTNET_ROOT=$HOME/dotnet9

export PATH=$PATH:$HOME/dotnet9

Open a new Terminal window and run the following command to verify the installation:

$ dotnet --version

9.0.100-rc.1.24452.12

4.2 Downloading Visual Studio Code

The first step in installing Visual Studio Code (often abbreviated to VS Code) is downloading it. Downloads for Windows, macOS, and Linux are available from the following web page:

https://code.visualstudio.com/Download

4.3 Installing on Windows

Visual Studio Code for Windows is available for Arm and Intel-based systems. Once you have downloaded the installer, launch it and follow the setup wizard to complete the installation. You can use the default settings unless you have specific installation requirements.

4.4 Installing on macOS

Visual Studio Code is available for Apple Silicon and Intel-based systems and is provided as a ZIP archive file containing versions for both processor types. Once you have downloaded the file, double-click on it to extract the Visual Studio Code package, then open a second Finder window and drag the package to your Applications folder. For convenience, take this opportunity to drag Visual Studio Code to the Dock as shown in Figure 4-4 below:

Figure 4-4

4.5 Installing on Linux

VS Code installation packages are available for several Linux distributions including Debian, Ubuntu, SUSE, and most Red Hat Linux-based platforms with support for x64, Arm32, and Arm64 processor architectures.

The steps to install VS Code will depend on which distribution you are running as outlined below:

4.5.1 Debian/Ubuntu

VS Code for Debian and Ubuntu is provided as a .deb package. To complete the installation, open a Terminal window, change directory to the location of the downloaded package, and run the following command (where <filename>.deb is the name of the package file):

$ sudo dpkg -i <filename>.deb

4.5.2 Red Hat-based distributions

VS Code is installed on Red Hat-based systems including RHEL, CentOS, Fedora, AlmaLinux, and Rocky Linux using a Red Hat Package Manager (.rpm) file. To install VS Code, open a terminal window, change directory to the location of the downloaded package, and run the following command (where <filename>.rpm is the name of the package file):

$ sudo rpm -ihv <filename>.rpm

After the installation completes, VS Code may be launched either by searching for the app in the desktop environment or via the command line by running the code executable.

4.6 Customizing Visual Studio Code

When VS Code runs for the first time, the screen shown in Figure 4-5 will appear. By default, the user interface will use the Dark Modern theme. If you prefer a different theme, make a selection from the option marked A below, or click the Browse Color Themes button (B) to browse the entire list:

Figure 4-5

4.7 Adding the C# Dev Kit extension

The extension for C# development is the C# Dev Kit, and we will need to install it before we start writing code. To install the extension, click on the Extensions button located in the sidebar (marked A in Figure 4-6 below), then enter “C# Dev Kit” into the search field (B). Locate the C# Dev Kit entry in the extensions list and click the Install button (C) to add the extension:

Figure 4-6

Another helpful extension when programming in C# is IntelliCode for C# Dev Kit. This extension uses AI to offer code completion suggestions as you type into the editor and is a valuable tool for increasing productivity and identifying ways to improve your code. Click the Install button marked D to add IntelliCode support to your VS Code configuration.

With the above steps completed, Visual Studio Code is installed and configured, and you are ready to start programming in C#.

4.8 Take the knowledge test