4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Visual Studio や 他の IDE を使わずに C# プログラムをコンパイルして実行する

4
Last updated at Posted at 2025-07-28

目的

ちょっとした数行の C#コードを書いて実行するために、わざわざプロジェクトを建ててVisual Studioを使うのは面倒です。本当なら、コマンドラインでインタラクティブに使えるといいのですが、今回は、コードをサクッと書いて、コンパイル&実行する方法をまとめておきます。

ネットで見つけた、次の2つの記事の受け売りです。

Visual Studio Code で「C# Interactive」というREPL環境が使えるとのこと。
記事後半参照

準備

Windows10 や Windows11 をお使いならば、OS標準で.Net Frameworkがインストールされています。

Explorer等でC:\Windows\Microsoft.NET\Framework64を見てください。
下記スクショは自分の環境ですが、V2.0、V3.0、V3.5、V4.0 と複数のバージョンがインストールされていました。

scr_2025-06-23_105655.png

(執筆時点で)最新の .NET Framework のは V4.8.1 のようです。もし、インストールされていないバージョンの .NET Framework を使用する場合は、ダウンロードしてインストールしてください。

使用する .NET Framework が決まったら、パスを PATH環境変数に設定します。
今回は、V4.0 を使用するため、C:\Windows\Microsoft.NET\Framework64\v4.0.30319をPATHに追加しました。

> path
PATH=C:\Windows\Microsoft.NET\Framework64\v4.0.30319;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\dotnet\;C:\Users\nak435\AppData\Local\Microsoft\WindowsApps;C:\Users\nak435\AppData\Local\Programs\Microsoft VS Code\bin;

コマンドラインを再起動し、csc と打って Not Foundにならなければ、準備完了です。

> csc
Microsoft (R) Visual C# Compiler version 4.8.9232.0
for C# 5
Copyright (C) Microsoft Corporation. All rights reserved.

This compiler is provided as part of the Microsoft (R) .NET Framework, but only supports language versions up to C# 5, which is no longer the latest version. For compilers that support newer versions of the C# programming language, see http://go.microsoft.com/fwlink/?LinkID=533240

This compiler is provided as part of the Microsoft (R) .NET Frameworkと警告が出ますが、問題ありません。もし、フルインストールするなら、.NET SDKをインストールしてください。

Hello World!

次のコードを、ファイル名「A.cs」として適当なフォルダに保存します。

A.cs
// C# program to print Hello World!
using System;

// namespace declaration
namespace HelloWorldApp {

// Class declaration
class A {

    // Main Method
    static void Main(string[] args)
    {
        // printing Hello World!
        Console.WriteLine("Hello World!");
        // To prevents the screen from
        // running and closing quickly
        Console.ReadKey();
    }
  }
}
  • コンパイル

コマンドプロンプトを開き、csc A.csを実行します(A.exeが生成されます)。

> csc A.cs
  • 実行

Aコマンド(A.exe)を実行します。

> .\A
Hello World!
(任意のキーを押して終了)

コンパイルして実行できました。
 

クラスCを使用するクラスBをコンパイルして実行する

次のB.csC.csをメモ帳か適当なテキストエディタで作成します。

B.cs
using System;

// namespace declaration
namespace HelloWorldApp {

// Class declaration
class B {
    // Main Method
    static void Main(string[] args)
    {
        // printing Hello World!
        Console.WriteLine("Hello World!");

        var b = new C();
        b.startMusic();

        // To prevents the screen from
        // running and closing quickly
        Console.ReadKey();
    }
  }
}
C.cs
using System;

// namespace declaration
namespace HelloWorldApp
{
    public class C {
        public void startMusic() {
            Console.Write("I am in C");
        }   
    }
}
  • csc /target:library C.cs とタイプして C.dll ファイルを生成します
> csc /target:library C.cs

> dir ?.*
   426 b.cs
   204 c.cs
 3,072 c.dll
  • 次に、csc /r:C.dll B.cs とタイプして B.cs をコンパイルします(B.exe ファイルが生成されます)
> csc /r:C.dll B.cs

> dir ?.*
   426 b.cs
 3,584 b.exe
   204 c.cs
 3,072 c.dll
  • B とタイプして exe ファイルを実行します
> .\B
Hello World!
I am in C
(任意のキーを押して終了)

DLL化しない場合は、次のコマンドでも可

> csc B.cs C.cs

> .\B


 

Windows Formsアプリ

Windows Formsアプリも作れます。

main.cs
using System;
using System.Drawing;
using System.Windows.Forms;

public class app : Form
{
    static public void Main ()
    {
        Application.Run (new app ());
    }
}
  • コンパイル
> csc main.cs -r:System.Windows.Forms.dll -r:System.Drawing.dll
  • 実行
> .\main

scr_2025-06-23_155812.png

空っぽなウィンドウが開くだけです。
テキスト ボックス、ボタン、ドロップダウン ボックス等の生成コードを頑張って書くことで、リッチなアプリも 作れます。。。

using System;
using System.Drawing;
using System.Windows.Forms;

public class app : Form
{
    private Label label1 = new Label();// ラベルコントロール
    public app()
    {
        this.label1.Location = new Point(50, 100);
        this.label1.Size = new Size(200, 100);
        this.label1.Font = new Font("Arial", 24);
        this.label1.Text = "Hello, World!";
        this.Controls.Add(this.label1);
    }

    static public void Main()
    {
        Application.Run(new app());
    }
}

すべてコードで書くのは しんどいので、その場合は、素直に Visual Studioを使いましょう。

プロジェクトをビルドする

Project.csprojファイルを作成することで、複数の*.csファイルから構成されるプログラムをビルドすることができます。

(例)Project.csproj
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <Compile Include="Program.cs" />
    <Compile Include="Solver.cs" />
  </ItemGroup>
  <Target Name="Build">
    <Csc Sources="@(Compile)"/>
  </Target>

</Project>

.NETのバージョンによって、書き方に多少の違いがあるようです。

次のコマンドでビルドします。

> msbuild .\Project.csproj

C# Interactive

古い記事で紹介されている「C# Interactive」は、今の Visual Studio Codeでは使えませんでした。
今は、Visual Studio Code に 拡張機能Polyglot Notebooks をインストールして、notebook スタイルで使用できる ということで、試してみます。

インストール手順

1. .NET SDK 9.0 を インストール
PS> winget install Microsoft.DotNet.SDK.9
2. .Net Interactive をインストール
PS> dotnet tool install --global Microsoft.dotnet-interactive
3. 拡張機能 Polyglot Notebooks をインストール
Visual Studio Code を起動し、拡張機能 Polyglot Notebooks をインストール

使ってみる

Visual Studio Codeの コマンドパレット(Ctrl+Shift+P)に、Polyglot Notebooks: Create new blank notebookをタイプ(検索)

dibを選択、C#を選択

scr_2025-06-23_165239.png

一応 動きましたが、「ターミナル上でコマンドでサクッと使う」とはいきません。

dotnet interactive stdioは、まったく意味が違う?!



以上

4
2
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?