初めてのC#プログラムを実行する
Coddyの「C++開発者のためのC#」コースのレッスン 1/48。
C++はネイティブコードにコンパイルされ、グローバルなmainが開始されます。このC#コースでは、宣言されたクラスをmcsでコンパイルし、Mono上で静的なMainを開始します。C#はオブジェクトのメモリを管理しますが、リソースの寿命には引き続き明示的な設計が必要です。
C#は.NETファミリーの静的型チェック言語です。このコースでは、互換性のあるMonoコンパイラーと、明示的なクラスおよびメソッドを使用します。最新のレコード、トップレベルステートメント、null許容参照型の注釈は必要ありません。まずは一度正常に実行してみましょう。以降のレッスンで構文とプログラムの構造を説明します。
C++の例
#include <algorithm>
#include <array>
#include <cctype>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <optional>
#include <set>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>
using namespace std;
int main() {
cout << "C# is ready" << "\n";
}
Main内のC#ステートメント
Console.WriteLine("C# is ready");ここに示すのは、学習内容に焦点を当てた抜粋です。課題ファイルには実行可能な完全なラッパーが含まれており、上記のスニペットは関連する宣言とステートメントを示しています。
参照出力:
C# is readyC#の出力:
C# is readyチャレンジ
簡単提供されたプログラムを変更せずに実行してください。入力はありません。C# is readyを出力します。後続の課題では、編集可能なコードは、表示されているロック済みのランチャーとは別のC#ソースファイルに配置します。
ロック解除されたC#ファイルを編集してください。表示されているロック済みのMain.csは、説明されている行を読み込み、Exercise.Solveを呼び出し、入力が変更されていないことを検証して、最後に改行を1つ付けて結果を出力します。宣言済みのAPIを維持してください。2つ目のMainを追加したり、診断メッセージを出力したりしないでください。
自分で試してみよう
using System;
using System.Linq;
using System.Collections.Generic;
using System.Globalization;
public class Program {
static int I(string text) { return int.Parse(text,CultureInfo.InvariantCulture); }
static int[] Ints(string text) { return text.Length==0 ? new int[0] : text.Split(',').Select(I).ToArray(); }
static string[] Words(string text) { return text.Length==0 ? new string[0] : text.Split(','); }
static string Snapshot(object value) { if (value==null) return "<null>"; if (value is string) return (string)value; var sequence=value as System.Collections.IEnumerable; if (sequence!=null) { var items=new List<string>(); foreach (object item in sequence) items.Add(Snapshot(item)); return string.Join("|",items); } return Convert.ToString(value,CultureInfo.InvariantCulture); }
public static void Main() {
System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
var result = Exercise.Solve();
Console.WriteLine(result);
}
}
C++開発者のためのC#のすべてのレッスン
1ここから始めよう
初めてのC#プログラムを実行する自分で練習してみよう: C#オンラインコンパイラ