Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying C# 14 and .NET 10 – Modern Cross-Platform Development Fundamentals
  • Table Of Contents Toc
  • Feedback & Rating feedback
C# 14 and .NET 10 – Modern Cross-Platform Development Fundamentals

C# 14 and .NET 10 – Modern Cross-Platform Development Fundamentals - Tenth Edition

By : Mark J. Price
5 (1)
close
close
C# 14 and .NET 10 – Modern Cross-Platform Development Fundamentals

C# 14 and .NET 10 – Modern Cross-Platform Development Fundamentals

5 (1)
By: Mark J. Price

Overview of this book

C# and .NET remain the backbone of modern enterprise and cross-platform development. Whether you're building high-performance websites and services on Windows or Linux or desktop apps that run on Windows and macOS, the .NET ecosystem provides a mature yet constantly evolving foundation for software development. With the release of C# 14 and .NET 10, developers now have access to a more powerful toolset. This tenth edition has been thoroughly updated to reflect the latest features. It begins with a deep grounding in object-oriented programming and then walks you through building, testing, and debugging functions, as well as managing resources with .NET libraries. You'll work with data using LINQ and serialization, handle filesystem operations, and integrate new features like numerical string comparisons and OrderedDictionary improvements. This book explains C# 14 updates, such as field modifiers, partial events, and instance constructors, in a real-world context. It also guides you in building practical projects with ASP.NET Core 10, exploring Blazor and Minimal API web services that support AOT compilation—ideal for microservices and containers. By the end of the book, you’ll be ready to ship professional websites, services, and tools across platforms, confident in your ability to build software that's maintainable, efficient, and aligned with modern .NET development.
Table of Contents (19 chapters)
close
close
18
Index

Running a C# code file without a project file

As you have seen in this chapter, as well as .cs code files, C# projects usually need a project file. The project file configures which version of the .NET Runtime you want to target, language features like nullability and global namespace imports, and references to other projects and packages.

However, when you are first learning a programming language, you probably won’t change many of those options, so it’s extra complexity without any benefit.

Rival languages like Python allow you to execute a code file without a project file, as shown in the following code:

python app.py

Introducing file-based apps

C# 14 introduces a similar feature named file-based apps that allows developers to execute single .cs files directly, as shown in the following command:

dotnet run app.cs

Microsoft’s official terms are “file-based app” versus “project-based app.” This new feature is also sometimes referred to as “dotnet run app.cs,” which is misleading because you can actually miss out the “run” part of the command!

This enhancement supports top-level statements, allowing for concise code without boilerplate, and simplifies the development process by eliminating the need for a full project structure, making C# more accessible for quick scripting and prototyping.

The benefits of being able to execute a single .cs file include easier prototyping, ideal for quick tests and learning, where you can quickly experiment with ideas without overhead, and a lower barrier to entry for newcomers to C#.

The filename of the C# file does not have to be Program.cs.

Creating a file-based app

File-based apps are not supported in Visual Studio because it’s a command-line feature.

Let’s see an example:

  1. In the Chapter01 directory, create a new directory named NoProject.
  2. In the NoProject directory, use a plain text editor to create a file named hello.cs, with content as shown in the following code:
    Console.WriteLine("Hello World with no project file!");
    
  3. At the command prompt or terminal, in the NoProject folder, use the dotnet CLI to run the hello.cs file, as shown in the following code:
    dotnet run hello.cs
    
  4. Note the result, as shown in the following output:
    Hello World with no project file!
    

Currently, you can only have one C# file in a file-based app. Multi-file support is planned for .NET 11.

Configuring no-project C# code files

To enhance functionality, you can use special #: directives at the top of the .cs file:

  • You can add NuGet package references like Humanizer, as shown in the following code:
    #:package [email protected]
    using Humanizer;
    Console.WriteLine(TimeSpan.FromDays(1).Humanize());
    
  • You can add project references, as shown in the following code:
    #:project ../MyClassLib/MyClassLib.csproj
    
  • You can specify the SDK, like switching to Microsoft.NET.Sdk.Web for ASP.NET Core projects, as shown in the following code:
    #:sdk Microsoft.NET.Sdk.Web
    

To separate the SDK ID and an explicit version number, use an @, for example, #:sdk [email protected].

  • You can set MS Build properties, like setting the C# language version to preview, as shown in the following code:
    #:property LangVersion=preview
    
  • On Linux, you can specify #! aka “shebang” commands, as shown in the following code:
    #!/usr/bin/dotnet run
    

This allows you to execute the .cs file without prefixing it with the dotnet run command. You must also mark the .cs file as executable in the filesystem. For example, at the command prompt or terminal: ./hello.cs

These #: directives allow for greater flexibility and control directly within your script.

Converting a file-based app to a project-based app

You can later convert it into a full project, as shown in the following command:

dotnet project convert app.cs

Publishing a file-based app

You can publish a file-based app, as shown in the following command:

dotnet publish app.cs

By default, it is published as a native-compiled AOT app. You can disable that by setting a property at the top of the file, as shown in the following code:

#:property PublishAot=false

To learn more about the file-based app feature, you can read the article found at the following link: https://devblogs.microsoft.com/dotnet/announcing-dotnet-run-app/ and for a visual demonstration, you can watch the following video: https://www.youtube.com/watch?v=98MizuB7i-w.

Visually different images
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
C# 14 and .NET 10 – Modern Cross-Platform Development Fundamentals
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist download Download options font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon