techmore.in

C# - Scripts

In C#, scripts are typically associated with the creation of standalone programs or command-line utilities, rather than being embedded into applications as in some scripting languages. C# scripts are often used for quick tasks, prototyping, and automation. They can be written and executed using various tools and environments.

C# Script Basics

C# scripts are usually written in files with the .csx extension and are executed using the .NET Core CLI or through tools like Visual Studio Code with the C# extension.

Example of a Basic C# Script:

Create a file named script.csx:

csharp
// script.csx using System; Console.WriteLine("Hello, C# Script!");

You can execute this script using the .NET Core CLI:

bash
dotnet script script.csx

Key Tools for Running C# Scripts

  1. .NET Core CLI

    • dotnet-script: A tool that allows you to run C# scripts directly from the command line.

    • Installation:

      bash
      dotnet tool install -g dotnet-script
    • Running a Script:

      bash
      dotnet script script.csx
  2. Visual Studio Code

    • With the C# extension and the C# Interactive extension, you can run C# scripts directly within the editor.
    • Open your .csx file in VS Code and use the integrated terminal or commands to execute it.
  3. Visual Studio

    • C# Interactive: This is a REPL (Read-Eval-Print Loop) environment for running C# code snippets. You can find it in Visual Studio under View -> Other Windows -> C# Interactive.

Features of C# Scripts

  • Interactive Execution: C# scripts can be executed line-by-line or as a whole, which is useful for quick experimentation and testing.
  • Dynamic Compilation: Scripts are compiled dynamically at runtime, which simplifies the development process by eliminating the need for a separate compilation step.
  • Reference Libraries: You can add references to assemblies and NuGet packages in your scripts using #r and #load directives.

Adding References:

csharp
// Adding a reference to a NuGet package #r "nuget: Newtonsoft.Json, 13.0.1" using Newtonsoft.Json; var obj = new { Name = "John", Age = 30 }; string json = JsonConvert.SerializeObject(obj); Console.WriteLine(json);

Using Scripts in .NET Interactive

.NET Interactive supports running C# scripts and provides an interactive experience similar to Jupyter Notebooks. It can be used in Visual Studio Code, Jupyter Notebooks, and other environments.

Example in a Jupyter Notebook:

csharp
// Using .NET Interactive in a Jupyter Notebook cell Console.WriteLine("Hello from .NET Interactive!");

Common Use Cases for C# Scripts

  • Automation: Automating repetitive tasks such as file processing, data extraction, or system configuration.
  • Prototyping: Quickly testing new ideas or code snippets without setting up a full project.
  • Educational: Teaching and learning C# programming concepts in an interactive environment.
  • Command-line Tools: Writing simple command-line utilities that don’t require a full-fledged application.

Summary

C# scripts provide a flexible way to execute C# code for various tasks, from simple automation to rapid prototyping. They can be run using the .NET Core CLI with dotnet-script, within Visual Studio Code, or through Visual Studio’s interactive tools. This scripting capability enhances the versatility and productivity of C# in different development scenarios.