Reader small image

You're reading from  C# 12 and .NET 8 – Modern Cross-Platform Development Fundamentals - Eighth Edition

Product typeBook
Published inNov 2023
PublisherPackt
ISBN-139781837635870
Edition8th Edition
Right arrow
Author (1)
Mark J. Price
Mark J. Price
author image
Mark J. Price

Mark J. Price is a Microsoft Specialist: Programming in C# and Architecting Microsoft Azure Solutions, with over 20 years' experience. Since 1993, he has passed more than 80 Microsoft programming exams and specializes in preparing others to pass them. Between 2001 and 2003, Mark was employed to write official courseware for Microsoft in Redmond, USA. His team wrote the first training courses for C# while it was still an early alpha version. While with Microsoft, he taught "train-the-trainer" classes to get other MCTs up-to-speed on C# and .NET. Mark holds a Computer Science BSc. Hons. Degree.
Read more about Mark J. Price

Right arrow

Publishing your code for deployment

If you write a novel and you want other people to read it, you must publish it.Most developers write code for other developers to use in their own projects, or for users to run as an app. To do so, you must publish your code as packaged class libraries or executable applications.There are three ways to publish and deploy a .NET application. They are:

  • Framework-dependent deployment (FDD)
  • Framework-dependent executable (FDE)
  • Self-contained

If you choose to deploy your application and its package dependencies, but not .NET itself, then you rely on .NET already being on the target computer. This works well for web applications deployed to a server because .NET and lots of other web applications are likely already on the server.FDD means you deploy a DLL that must be executed by the dotnet command-line tool. FDE means you deploy an EXE that can be run directly from the command line. Both require the appropriate version of the .NET runtime to be installed...

Native ahead-of-time compilation

Native AOT produces assemblies that are:

  • Self-contained, meaning they can run on systems that do not have the .NET runtime installed.
  • Ahead-of-time (AOT) compiled to native code, meaning a faster startup time and a potentially smaller memory footprint.

Native AOT compiles IL code to native code at the time of publishing, rather than at runtime using the Just In Time (JIT) compiler. But native AOT assemblies must target a specific runtime environment like Windows x64 or Linux Arm.Since native AOT happens at publish time, you should remember that while you are debugging and live working on a project in your code editor, it is still using the runtime JIT compiler, not native AOT, even if you have AOT enabled in the project! However, some features that are incompatible with native AOT will be disabled or throw exceptions, and a source analyzer is enabled to show warnings about potential code incompatibilities.

Limitations of native AOT

Native AOT has limitations...

7 Packaging and Distributing .NET Types

Join our book community on Discord

https://packt.link/EarlyAccess

Qr code Description automatically generated

This chapter is about how C# keywords are related to .NET types, and about the relationship between namespaces and assemblies. You'll become familiar with how to package and publish your .NET apps and libraries for cross-platform use. We also cover how to decompile .NET assemblies for learning purposes and why you cannot prevent others from decompiling your code.In an online-only section, Exercise 7.3 – Porting from .NET Framework to modern .NET, you can learn how to use legacy .NET Framework libraries in .NET libraries and the possibility of porting legacy .NET Framework code bases to modern .NET. In another online-only section, Exercise 7.4 – Creating source generators, you will learn how to create source generators that can dynamically add source code to your projects, a very powerful feature.This chapter covers the following topics:

  • The road to .NET 8
  • Understanding...

Packaging your libraries for NuGet distribution

Before we learn how to create and package our own libraries, we will review how a project can use an existing package.

Referencing a NuGet package

Let's say that you want to add a package created by a third-party developer, for example, Newtonsoft.Json, a popular package for working with the JavaScript Object Notation (JSON) serialization format:

  1. In the AssembliesAndNamespaces project, add a reference to the Newtonsoft.Json NuGet package, either using the GUI for Visual Studio 2022 or the dotnet add package command for Visual Studio Code.
  2. Open the AssembliesAndNamespaces.csproj file and note that a package reference has been added, as shown in the following markup:
<ItemGroup>
  <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

If you have a more recent version of the Newtonsoft.Json package, then it has been updated since this chapter was written.

Fixing dependencies...

Working with preview features

It is a challenge for Microsoft to deliver some new features that have cross-cutting effects across many parts of .NET like the runtime, language compilers, and API libraries. It is the classic chicken and egg problem. What do you do first?From a practical perspective, it means that although Microsoft might have completed most of the work needed for a feature, the whole thing might not be ready until very late in their now annual cycle of .NET releases, too late for proper testing in "the wild."So, from .NET 6 onward, Microsoft will include preview features in general availability (GA) releases. Developers can opt into these preview features and provide Microsoft with feedback. In a later GA release, they can be enabled for everyone.

It is important to note that this topic is about preview features. This is different from a preview version of .NET or a preview version of Visual Studio 2022. Microsoft releases preview versions of Visual Studio and...

Practicing and exploring

Test your knowledge and understanding by answering some questions, getting some hands-on practice, and exploring with deeper research into the topics of this chapter.

Exercise 7.1 – Test your knowledge

Answer the following questions:

  1. What is the difference between a namespace and an assembly?
  2. How do you reference another project in a .csproj file?
  3. What is the benefit of a tool like ILSpy?
  4. Which .NET type does the C# float alias represent?
  5. When porting an application from .NET Framework to .NET 6, what tool should you run before porting, and what tool could you run to perform much of the porting work?
  6. What is the difference between framework-dependent and self-contained deployments of .NET applications?
  7. What is a RID?
  8. What is the difference between the dotnet pack and dotnet publish commands?
  9. What types of applications written for the .NET Framework can be ported to modern .NET?
  10. Can you use packages written for .NET Framework with modern .NET?

Exercise...

Summary

In this chapter, we:

  • Reviewed the journey to .NET 8 for BCL functionality.
  • Explored the relationship between assemblies and namespaces.
  • Saw options for publishing an app for distribution to multiple operating systems.
  • Learned how to publish to native AOT for faster startup and smaller memory footprint.
  • Learned how to decompile .NET assemblies for educational purposes.
  • Packaged and distributed a class library.
  • Learned how to activate preview features.

In the next chapter, you will learn about some common BCL types that are included with modern .NET.

Summary

In this chapter, you explored:

  • Choices for types to store and manipulate numbers.
  • Handling text, including using regular expressions for validating input.
  • Collections to use for storing multiple items.
  • Working with indexes, ranges, and spans.

In the next chapter, we will manage files and streams, encode and decode text, and perform serialization.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
C# 12 and .NET 8 – Modern Cross-Platform Development Fundamentals - Eighth Edition
Published in: Nov 2023Publisher: PacktISBN-13: 9781837635870
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at ₹800/month. Cancel anytime

Author (1)

author image
Mark J. Price

Mark J. Price is a Microsoft Specialist: Programming in C# and Architecting Microsoft Azure Solutions, with over 20 years' experience. Since 1993, he has passed more than 80 Microsoft programming exams and specializes in preparing others to pass them. Between 2001 and 2003, Mark was employed to write official courseware for Microsoft in Redmond, USA. His team wrote the first training courses for C# while it was still an early alpha version. While with Microsoft, he taught "train-the-trainer" classes to get other MCTs up-to-speed on C# and .NET. Mark holds a Computer Science BSc. Hons. Degree.
Read more about Mark J. Price