C#でHelloWorld【VisualStudioを使わない方法】

Microsoft Windows コンピュータ
Microsoft Windows

以前VisualStudio2019をインストールしてC#を試してみましたがVisualStudioをインストールしなくてもc#のコンパイルが出来るようです。

私のWindows7の環境ではC:\Windows\Microsoft.NET\Framework64の下にいくつかのバージョンのディレクトリがあり、その中にあるcsc.exeというファイルがC#のコンパイラの実行ファイルでコマンドベースのアプリになっています。

コンパイル作業をpowershellから行うことになりますが、C#のコンパイルコマンドがある場所にパスを通しておきます。
Windowsの環境変数を編集しても良いですが、私の環境ではPathが大変長くなっているので今回はpowershellの$profileでパスを設定するようにし、powershell内でのみPathが通るようにしました。

powershell_ise等で$profileに以下のコードを追加
私の環境では$profileの場所は”C:\Users\ユーザー名\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1″でした。
存在しない場合作成します。

$env:Path = $env:Path + ";C:\Windows\Microsoft.NET\Framework64\v4.0.30319"

これでパスが通りましたのでpowershellを立ち上げてコンパイルコマンドを実行してみます。

PS> csc
Microsoft (R) Visual C# Compiler version 4.8.3761.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. Fo
r compilers that support newer versions of the C# programming language, see http
://go.microsoft.com/fwlink/?LinkID=533240

warning CS2008: ソース ファイルが指定されていません。
error CS1562: ソースのない出力には、/out オプションを指定しなければなりません。

cscと入力しエンターを押したところソースファイルが無いと叱られてしまいましたが、パスはきちんと通っているようです。

それでは適当なディレクトリにC#のソースファイルを作成します。ソースファイルの編集はメモ帳などのテキストエディタで作成しました。

using System;
namespace test
{
    class Foo
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!!");
        }
    }
}

ファイル名をtest.csで保存しました。
それではpowershellを立ち上げてコンパイルをしてみます。

PS > h:
PS H:\> cd \csharp\first_test
PS H:\csharp\first_test> csc .\test.cs
Microsoft (R) Visual C# Compiler version 4.8.3761.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. Fo
r compilers that support newer versions of the C# programming language, see http
://go.microsoft.com/fwlink/?LinkID=533240

先程はソースファイルが無いと叱られましたが今回はそのメッセージが出ていません。
ソースファイルのディレクトリを見てみると、test.exeが出来上がっていました。

PS H:\csharp\first_test> dir


    Directory: H:\csharp\first_test


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       2019/12/18     13:13            176 test.cs
-a----       2019/12/18     13:38           3584 test.exe

test.exeを実行してみます。

PS H:\csharp\first_test> .\test.exe
Hello World!!
PS H:\csharp\first_test>

どうやら成功したようです。

学習用に短いサンプルを実行するするのであればVisualStudioをインストールしなくても良いので、WindowsがインストールされたPCとやる気さえあれば、お手軽に学習を始められます。

最近はpowershellでコードを書くことが多く、スクリプトで用が足りてしまえば他の言語をさわる機会があまりありません。特に比較的新しいコンピュータ言語を習得したいとは思うのですが、基本的に自分が使うソフト以外を作る気がおきないので、楽に作れる言語に流れてしまいます。

私はC#に関してはほぼ初心者ですが、今後学習がてら最近書いたpowershellのスクリプトをC#で書き直してみたいと思います。

文字列の書式

数値左0埋め3桁
String.Format("{0:000}",変数)"
数値カンマあり
String.Format("{0:#,##0}",変数)"

Excelの書式指定っぽい。さすがMS。
{0:の0は0番目の変数を参照するという意味

Powersehllでコンパイルと実行

'./ソースファイル名' | % { csc "${_}.cs"; . $_ }
csc.exeへのパスが通っていること。
ソースファイルの保存されているディレクトリで実行。

追記:20250205
Windows10にもcsc.exeがありました。

C#のバージョンは5で.NetFrameworkで4.6あたりだと思われます。

コメント

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy