Skip to footer content
Iron Academy Logo
C# Scripting and .NET CLI

C# Scripting and File Execution in .NET 10: A Deep Dive with Tim Corey

Tim Corey
14m 10s

C# has traditionally been a compiled language, requiring a project, solution, and Visual Studio setup to execute even the simplest programs. With .NET 10, however, things have changed dramatically. Tim Corey, in his video on C# scripting and file execution, demonstrates how developers can now treat C# as a scripting language, executing script files directly without creating a full project. This new ability allows programmers to write script code in a single cs file, run it from the command prompt, and experiment quickly with new code.

In this article, we will explore these features in detail, following Tim’s guidance to understand the workflow, command line usage, and advanced capabilities of file-based C# execution.

Introduction to C# Scripting in .NET 10

Tim starts the video by highlighting that C# scripting in .NET 10 allows a single cs file to act as a complete application. Unlike traditional Visual Studio projects, there is no need for public class declarations, a Program.cs, or a solution file. You can now write a script, save it as demo.cs, and execute it immediately.

This approach is similar to scripting in Python or JavaScript, where a single file can perform tasks without creating a project. Tim emphasizes that this is more than just convenience—it provides the ability to run tasks, test statements, and experiment with objects or data quickly.

Creating Your First Script File

Tim demonstrates creating a simple cs file called FileDemo.cs in Visual Studio Code. The following code serves as a classic "hello world" example:

Console.WriteLine("Hello World");

Running the file is as simple as opening the command prompt and executing:

dotnet run FileDemo.cs

The output displays Hello World instantly, showing that even runtime execution of a single file is fully supported. Tim notes that this approach is ideal for new code testing, small scripts, or situations where the overhead of a full project is unnecessary.

Handling Command Line Input and Arguments

Tim then demonstrates the ability to accept command line arguments in a script code. By modifying the hello world example, you can pass a value directly to the script file:

Console.WriteLine($"Hello {args[0]}");

When you run dotnet run demo.cs Tim, the output becomes Hello Tim. Tim points out that the args array works exactly like in a traditional console application, showing that file-based scripts are full C# applications with all the capabilities of the language.

Simplified Syntax and Implicit Usings

Tim explains that .NET 10 supports implicit using directives, meaning you can access System namespaces without explicitly writing using System;. This reduces boilerplate syntax and allows the first object or method in your script to be executed immediately.

This simplification extends to csx file extensions and interactive window workflows in Visual Studio Code, where developers can test script code line by line using REPL windows or the interactive window, similar to Python scripting.

Interactive User Input

Next, Tim shows how to make scripts interactive by reading input from the user:

Console.Write("What is your name? ");
string name = Console.ReadLine();
Console.WriteLine($"Hello {name}");

Here, the first object of interaction is the string name, which can be accessed and displayed immediately. Tim notes that nullable warnings may appear but can be handled properly. This ability to read input and display output makes script files versatile and practical for tasks beyond simple demonstration code.

Using NuGet Packages in Scripts

To extend the runtime capabilities of a script file, Tim introduces NuGet packages. For example, using Spectre.Console, you can format the output with color:

#r "nuget: Spectre.Console, 0.54.0"
using Spectre.Console;

AnsiConsole.MarkupLine("[red]Hello Tim[/]");

Tim explains that adding references via NuGet packages in script code allows developers to leverage external assemblies without a full project. He demonstrates running the script and seeing the console display the user’s name in red, showing how file-based scripts can be as powerful as full projects.

Publishing Scripts as Executables

Tim also covers creating executables from cs files. By running:

dotnet publish Filedemo.cs

you can generate a DLL or EXE for Windows, Linux, or Mac, making the script file portable. He highlights that these executables are Native AOT, giving maximum runtime performance. If needed, the AOT property can be turned off using a property directive at the top of the file.

This demonstrates the ability of .NET 10 scripting to produce production-ready files while keeping the syntax simple.

Converting Scripts to Full Projects

For more complex applications requiring multiple classes, methods, or objects, Tim shows how to convert a script file into a full project:

dotnet project convert FileDemo.cs

This command creates a new folder containing a CS project file, preserving NuGet package references, nullable settings, and implicit usings. The following code from the original script is retained, making it easy to integrate into a Visual Studio solution or run as a standalone project.

Key Advantages of File-Based Scripting

Tim highlights several advantages of C# scripting and file execution:

  1. Speed – Run new code immediately from script files.

  2. Portability – Publish scripts as executables for Windows, Linux, or macOS.

  3. Power – Scripts can use NuGet packages, access data, and handle user input.

  4. Flexibility – Promote cs files to full projects with minimal effort.

  5. Reduced Overhead – No need for solution, project, or class declarations for simple tasks.

Tim notes that while file-based scripts are not suitable for every scenario, they are perfect for testing, demos, and quick console-based tasks.

Conclusion

Tim Corey’s video illustrates that C# scripting in .NET 10 is a powerful and flexible tool for developers. Script files allow immediate execution of new code, handling user input, interacting with objects, and using NuGet packages. Scripts can be run from the command prompt, executed in Visual Studio Code, or published as DLLs or EXEs for cross-platform use.

The ability to convert script code into a full project provides the best of both worlds: the simplicity of a script file with the extensibility of a traditional Visual Studio project. Tim encourages developers to write, test, and modify script code freely, making C# scripting a valuable tool for rapid development and experimentation.

As Tim summarizes, this method is ideal for small projects, console applications, and data-processing tasks, providing a modern, efficient workflow that complements traditional C# development.

Hero Worlddot related to C# Scripting and File Execution in .NET 10: A Deep Dive with Tim Corey
Hero Affiliate related to C# Scripting and File Execution in .NET 10: A Deep Dive with Tim Corey

Earn More by Sharing What You Love

Do you create content for developers working with .NET, C#, Java, Python, or Node.js? Turn your expertise into extra income!

Iron Support Team

We're online 24 hours, 5 days a week.
Chat
Email
Call Me