IT練習ノート

IT関連で調べたこと(実際は嵌ったこと)を書いています。

C#のはじめの一歩

コマンドラインからC#でHelloWorldを練習してみました。

もちろん、VisualStudioですと諸々よろしくやってくれて便利なので、コマンドラインから試す必要は全くありません。ただ、便利すぎて、諸々隠蔽化されているので、仕組みが理解できません。なので、あえてコマンドラインから実行してみて、いろいろ嵌った上で、理解を確かめたいと思いました。

HelloWorldでたっぷり2時間嵌りました。

PS C:\Users\mytest01\Documents\myapp01> dir .\MyAppMain.*


    Directory: C:\Users\mytest01\Documents\myapp01


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---        2014/04/05     12:33        140 MyAppMain.cs
-a---        2014/04/05     12:29        141 MyAppMain.cs~

PS C:\Users\mytest01\Documents\myapp01> type .\MyAppMain.cs
using System;

namespace MyApp01

{
        class MyAppMain
        {
                static void Main()
                {
                        Console.WriteLine("Hello World");
                }
        }
}

PS C:\Users\mytest01\Documents\myapp01> C:\windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe .\*.cs
Microsoft (R) Visual C# Compiler version 4.0.30319.33440
for Microsoft (R) .NET Framework 4.5
Copyright (C) Microsoft Corporation. All rights reserved.

PS C:\Users\mytest01\Documents\myapp01> dir .\MyAppMain.*


    Directory: C:\Users\mytest01\Documents\myapp01


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---        2014/04/05     12:33        140 MyAppMain.cs
-a---        2014/04/05     12:29        141 MyAppMain.cs~
-a---        2014/04/05     12:37       3584 MyAppMain.exe


PS C:\Users\mytest01\Documents\myapp01> .\MyAppMain.exe
Hello World
PS C:\Users\mytest01\Documents\myapp01>

コンパイル対象のファイル指定は一番最後ではないといけないようです。(outオプションが後に来ているとだめ)

PS C:\Users\mytest01\Documents\myapp01> C:\windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe .\*.cs /out:.\Hello.exe
Microsoft (R) Visual C# Compiler version 4.0.30319.33440
for Microsoft (R) .NET Framework 4.5
Copyright (C) Microsoft Corporation. All rights reserved.

fatal error CS2022: Options '/out' and '/target' must appear before source file names

Mainクラスのファイル名と異なるexeファイル名でも大丈夫です。

PS C:\Users\mytest01\Documents\myapp01> C:\windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe /out:.\Hello.exe .\*.cs
Microsoft (R) Visual C# Compiler version 4.0.30319.33440
for Microsoft (R) .NET Framework 4.5
Copyright (C) Microsoft Corporation. All rights reserved.

PS C:\Users\mytest01\Documents\myapp01> .\Hello.exe
Hello World

拡張子をつけないと、起動するアプリを聞かれます。

PS C:\Users\mytest01\Documents\myapp01> C:\windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe /out:.\Hello2 .\*.cs
Microsoft (R) Visual C# Compiler version 4.0.30319.33440
for Microsoft (R) .NET Framework 4.5
Copyright (C) Microsoft Corporation. All rights reserved.

PS C:\Users\mytest01\Documents\myapp01> .\Hello2

f:id:naotoogawa:20140405215952p:plain

コンパイルするファイルの中でMainの重複はできません。outをつけないと、Mainのあるファイル名がexeのファイル名になるみたいですね。

PS C:\Users\mytest01\Documents\myapp01> type *.cs
namespace MyApp01

{
        class Foo
        {
                static void Main(){}
                public int val {get; set;}

                public Foo()
                {
                        val = 2;
                }
        }
}

using System;

namespace MyApp01

{
        class MyAppMain
        {
                static void Main()
                {
                        Console.WriteLine("Hello World");
                }
        }
}
PS C:\Users\mytest01\Documents\myapp01> C:\windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe .\*.cs
Microsoft (R) Visual C# Compiler version 4.0.30319.33440
for Microsoft (R) .NET Framework 4.5
Copyright (C) Microsoft Corporation. All rights reserved.

Foo.cs(6,15): error CS0017: Program 'c:\Users\mytest01\Documents\myapp01\Foo.exe' has more than one entry point defined: 'MyApp01.Foo.Main()'.  Compile with /main to specify the type that contains the
        entry point.
MyAppMain.cs(8,15): error CS0017: Program 'c:\Users\mytest01\Documents\myapp01\Foo.exe' has more than one entry point defined: 'MyApp01.MyAppMain.Main()'.  Compile with /main to specify the type that
        contains the entry point.
PS C:\Users\mytest01\Documents\myapp01>