{"id":2066,"date":"2025-05-28T16:46:01","date_gmt":"2025-05-28T16:46:01","guid":{"rendered":"https:\/\/rssfeedtelegrambot.bnaya.co.il\/index.php\/2025\/05\/28\/announcing-dotnet-run-app-cs-a-simpler-way-to-start-with-c-and-net-10\/"},"modified":"2025-05-28T16:46:01","modified_gmt":"2025-05-28T16:46:01","slug":"announcing-dotnet-run-app-cs-a-simpler-way-to-start-with-c-and-net-10","status":"publish","type":"post","link":"https:\/\/rssfeedtelegrambot.bnaya.co.il\/index.php\/2025\/05\/28\/announcing-dotnet-run-app-cs-a-simpler-way-to-start-with-c-and-net-10\/","title":{"rendered":"Announcing dotnet run app.cs \u2013 A simpler way to start with C# and .NET 10"},"content":{"rendered":"<p>We are super excited to introduce a new feature that was released as part of .NET 10 Preview 4 that makes getting started with C# easier than ever. You can now run a C# file directly using dotnet run app.cs. This means you no longer need to create a project file or scaffold a whole application to run a quick script, test a snippet, or experiment with an idea. It\u2019s simple, intuitive, and designed to streamline the C# development experience, especially for those just getting started.<\/p>\n<h2>What is dotnet run app.cs?<\/h2>\n<p>Until now, executing C# code using the dotnet CLI required a project structure that included a .csproj file. With this new capability, which we call <em>file-based apps<\/em>, you can run a standalone .cs file directly, much like you would with scripting languages such as Python or JavaScript.<\/p>\n<p>This lowers the entry barrier to trying out C# and makes the language a much more attractive choice for learning, prototyping, or automation scenarios.<\/p>\n<p><strong>Quick Start, No Project File Required<\/strong> \u2013 Great for learning, experimentation, and small scripts.<br \/>\n<strong>First-Class CLI Integration<\/strong> \u2013 No extra tools, no dependencies, just dotnet and your .cs file.<br \/>\n<strong>Scales to Real Applications<\/strong> \u2013 This isn\u2019t a separate dialect or runtime. When your script grows up, it can evolve into a full-fledged project using the same language, syntax, and tooling.<\/p>\n<h2>New file-level directives for file-based C# apps<\/h2>\n<p>With .NET 10 Preview 4, file-based apps also support a set of powerful <strong>file-level directives<\/strong> that allow to declare a small number of important things that are stored in project files for project-based apps, all without leaving your single .cs file. These directives make file-based apps more flexible and expressive while maintaining compatibility with MSBuild concepts.<\/p>\n<h3>Referencing NuGet packages with #:package<\/h3>\n<p>You can add NuGet package references directly in your .cs file using the #:package directive:<\/p>\n<p>#:package Humanizer@2.14.1<\/p>\n<p>using Humanizer;<\/p>\n<p>var dotNet9Released = DateTimeOffset.Parse(&#8220;2024-12-03&#8221;);<br \/>\nvar since = DateTimeOffset.Now &#8211; dotNet9Released;<\/p>\n<p>Console.WriteLine($&#8221;It has been {since.Humanize()} since .NET 9 was released.&#8221;);<\/p>\n<h3>Specifying an SDK with #:sdk<\/h3>\n<p>By default, file-based apps use the Microsoft.NET.Sdk SDK. If you\u2019re building something like a web API, you can change the SDK using the #:sdk directive:<\/p>\n<p>#:sdk Microsoft.NET.Sdk.Web<\/p>\n<p>This tells the tooling to treat the file as if it were part of a web project, enabling features of ASP.NET Core like Minimal APIs and MVC.<\/p>\n<h3>Setting MSBuild properties with #:property<\/h3>\n<p>You can configure additional build properties using #:property. For example:<\/p>\n<p>#:property LangVersion preview<\/p>\n<p>This allows your file-based app to opt into advanced language features and platform targeting, without needing a full project file.<\/p>\n<h3>Using shebang lines for shell scripts<\/h3>\n<p>File-based apps also support <a href=\"https:\/\/en.wikipedia.org\/wiki\/Shebang_%28Unix%29\">shebang<\/a> lines (#!), allowing you to write cross-platform C# shell scripts that are executable directly on Unix-like systems. For example:<\/p>\n<p>#!\/usr\/bin\/dotnet run<br \/>\nConsole.WriteLine(&#8220;Hello from a C# script!&#8221;);<\/p>\n<p>You can make the file executable and run it directly:<\/p>\n<p>chmod +x app.cs<br \/>\n.\/app.cs<\/p>\n<p>This makes C# a convenient option for CLI utilities, automation scripts, and tooling, no project setup required.<\/p>\n<h2>Converting to a project-based app<\/h2>\n<p>When your file-based app grows in complexity, or you simply want the extra capabilities afforded in project-based apps, you can convert it to a standard project with:<\/p>\n<p>dotnet project convert app.cs<\/p>\n<p>This command creates a new directory named for your file, scaffolds a .csproj file, moves your code into a Program.cs file, and translates any #: directives into MSBuild properties and references.<\/p>\n<h3>Example<\/h3>\n<p>Given this file:<\/p>\n<p>#:sdk Microsoft.NET.Sdk.Web<br \/>\n#:package Microsoft.AspNetCore.OpenApi@10.*-*<\/p>\n<p>var builder = WebApplication.CreateBuilder();<\/p>\n<p>builder.AddOpenApi();<\/p>\n<p>var app = builder.Build();<\/p>\n<p>app.MapGet(&#8220;\/&#8221;, () =&gt; &#8220;Hello, world!&#8221;);<br \/>\napp.Run();<\/p>\n<p>The generated .csproj would be:<\/p>\n<p>&lt;Project Sdk=&#8221;Microsoft.NET.Sdk.Web&#8221;&gt;<\/p>\n<p>  &lt;PropertyGroup&gt;<br \/>\n    &lt;TargetFramework&gt;net10.0&lt;\/TargetFramework&gt;<br \/>\n    &lt;ImplicitUsings&gt;enable&lt;\/ImplicitUsings&gt;<br \/>\n    &lt;Nullable&gt;enable&lt;\/Nullable&gt;<br \/>\n  &lt;\/PropertyGroup&gt;<\/p>\n<p>  &lt;ItemGroup&gt;<br \/>\n    &lt;PackageReference Include=&#8221;Microsoft.AspNetCore.OpenApi&#8221; Version=&#8221;10.*-*&#8221; \/&gt;<br \/>\n  &lt;\/ItemGroup&gt;<\/p>\n<p>&lt;\/Project&gt;<\/p>\n<p>This makes the transition seamless, from a single file to a fully functional, buildable, and extensible project.<\/p>\n<h2>Existing ways to run C# without projects<\/h2>\n<p>This is far from the first time developers have wanted to run C# without a project. Community projects like <a href=\"https:\/\/github.com\/oleg-shilo\/cs-script\">CS-Script<\/a>, <a href=\"https:\/\/github.com\/dotnet-script\/dotnet-script\">dotnet-script<\/a>, <a href=\"https:\/\/cakebuild.net\/\">Cake<\/a>, and others have long filled this role, enabling scripting workflows, REPL experiences, and other experiences with C#. Here\u2019s a <a href=\"https:\/\/www.hanselman.com\/blog\/c-and-net-core-scripting-with-the-dotnetscript-global-tool\">blog post by Scott Hanselman from 2018 detailing the dotnet-script global tool<\/a>.<\/p>\n<p>These tools remain valuable and are worth checking out, especially for more advanced scripting scenarios. However, with this new built-in support, developers can get started immediately: no additional installation, configuration, or discovery steps required.<\/p>\n<p>Equally important: this isn\u2019t a separate dialect or mode of C#. We\u2019re being intentional about making this feature a natural earlier \u201cclick-stop\u201d from a regular C# project-based app. You\u2019re writing the same C#, using the same compiler, and when your code grows up, it transitions naturally into a project-based app, if and when you want.<\/p>\n<h2>Getting Started<\/h2>\n<p><strong>Install .NET 10 Preview 4<\/strong><br \/>\nDownload and install it from <a href=\"https:\/\/dotnet.microsoft.com\/download\/dotnet\/10.0\">dotnet.microsoft.com<\/a>.<br \/>\n<strong>Install Visual Studio Code (recommended)<\/strong><br \/>\nIf you\u2019re using Visual Studio Code, install the <a href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=ms-dotnettools.csdevkit\">C# Dev Kit<\/a> and then follow these instructions to update the C# extension for file-based apps support:<\/p>\n<p>To enable support for file-based apps and directives, you\u2019ll need the latest <strong>pre-release version<\/strong> of the C# extension:<\/p>\n<p>Open the Extensions sidebar (Ctrl+Shift+X)<br \/>\nSearch for \u201cC#\u201d<br \/>\nIn the extension page, click the <strong>Switch to Pre-Release Version<\/strong> button<br \/>\nEnsure the version installed is at least 2.79.8<\/p>\n<p><strong>Write your code<\/strong><br \/>\nCreate a file called hello.cs:<br \/>\nConsole.WriteLine(&#8220;Hello, world!&#8221;);<\/p>\n<p><strong>Run it!<\/strong><br \/>\nOpen a terminal in the same folder and run:<br \/>\ndotnet run hello.cs<\/p>\n<p><strong>Convert to a project<\/strong><br \/>\nTo convert the file to a project, run:<br \/>\ndotnet project convert hello.cs<\/p>\n<h2>Learn more<\/h2>\n<p>Watch this feature in action in this <a href=\"https:\/\/build.microsoft.com\/sessions\/DEM518?source=sessions\">demo session from Microsoft Build<\/a>:<br \/>\n<a href=\"https:\/\/www.youtube.com\/watch?v=98MizuB7i-w\">No projects, just C# with dotnet run app.cs<\/a><\/p>\n<p>You\u2019ll see how easy it is to get started, explore directives, and convert to a full project when ready.\n<\/p>\n<p>You\u2019ll see how easy it is to get started, explore directives, and convert to a full project when ready.<\/p>\n<h2>The road ahead<\/h2>\n<p>With dotnet run app.cs, we\u2019re making C# more approachable, while preserving the full power and depth of the .NET ecosystem. Whether you\u2019re prototyping, teaching, or building production systems, this new capability helps you move faster from idea to execution.<\/p>\n<p>In upcoming .NET 10 previews we\u2019re aiming to improve the experience of working with file-based apps in VS Code, with enhnanced IntelliSense for the new file-based directives, improved performance, and support for debugging. At the command line we\u2019re exploring support for file-based apps with <a href=\"https:\/\/github.com\/dotnet\/sdk\/blob\/main\/documentation\/general\/dotnet-run-file.md#multiple-c-files\">multiple files<\/a>, and ways to make running file-based apps faster.<\/p>\n<p>Try it out today and send your <a href=\"https:\/\/github.com\/dotnet\/sdk\/issues\/new\">feedback to GitHub<\/a> as we continue to shape this experience during .NET 10 and beyond.<\/p>\n<p>The post <a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/announcing-dotnet-run-app\/\">Announcing dotnet run app.cs \u2013 A simpler way to start with C# and .NET 10<\/a> appeared first on <a href=\"https:\/\/devblogs.microsoft.com\/dotnet\">.NET Blog<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>We are super excited to introduce a new feature that was released as part of .NET 10 Preview 4 that [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[7],"tags":[],"class_list":["post-2066","post","type-post","status-publish","format-standard","hentry","category-dotnet"],"_links":{"self":[{"href":"https:\/\/rssfeedtelegrambot.bnaya.co.il\/index.php\/wp-json\/wp\/v2\/posts\/2066","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rssfeedtelegrambot.bnaya.co.il\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rssfeedtelegrambot.bnaya.co.il\/index.php\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/rssfeedtelegrambot.bnaya.co.il\/index.php\/wp-json\/wp\/v2\/comments?post=2066"}],"version-history":[{"count":0,"href":"https:\/\/rssfeedtelegrambot.bnaya.co.il\/index.php\/wp-json\/wp\/v2\/posts\/2066\/revisions"}],"wp:attachment":[{"href":"https:\/\/rssfeedtelegrambot.bnaya.co.il\/index.php\/wp-json\/wp\/v2\/media?parent=2066"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rssfeedtelegrambot.bnaya.co.il\/index.php\/wp-json\/wp\/v2\/categories?post=2066"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rssfeedtelegrambot.bnaya.co.il\/index.php\/wp-json\/wp\/v2\/tags?post=2066"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}