Menu
CoddyTech

名前空間とファイル

Coddyの「C++開発者のためのC#」コースのレッスン 29/48。

C++の名前空間では::を使い、ソースのリンクではヘッダーと個別の翻訳単位に依存することがよくあります。C#のusingは名前の検索に影響し、mcsは提供された.csファイルを1つのアセンブリにまとめますが、C++のインクルードヘッダーは使いません。タスクを開始する静的なMainはちょうど1つです。

コンパイラーはすべてのソースファイルを1つのプログラムにまとめます。名前空間名は型を整理し、usingは名前をスコープ内で使えるようにします。importスクリプトのようにファイルを実行するわけではありません。publicは、これらの型間のアクセスを制御します。クラスがいくつあっても、Mainはちょうど1つ必要です。

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;
namespace Catalog { int total(int unit,int count) { return unit*count; } }
int main() {
cout << Catalog::total(2,5) << "\n";
}

C#の補助宣言

namespace Display { public static class Text { public static string Bracket(string value) { return "[" + value + "]"; } } }

Main内のC#文

Console.WriteLine(Display.Text.Bracket("pen"));

これらは学習用に要点を抜き出した例です。課題ファイルには実行可能な完全なラッパーが含まれており、上記のスニペットは関連する宣言と文を示しています。

参照出力:

10

C#の出力:

[pen]
challenge icon

チャレンジ

簡単

Pricing.cs に Catalog.Pricing.Total を実装してください。これは、0以上の整数の入力に対して unit * quantity を返します。Solve は名前空間付きのヘルパーを呼び出します。ドライバーもこのヘルパーを直接呼び出します。

ロックされていない C# ファイルを編集してください。表示されているロック済みの Main.cs は、説明されている行を読み込み、Exercise.Solve を呼び出し、入力が変更されていないことを検証して、最後に改行を1つ付けて結果を出力します。宣言済みの API を維持してください。2つ目の Main を追加したり、診断出力を表示したりしないでください。

ロックされたランチャーは、Solve のラッパーだけでなく、エクスポートされたヘルパーまたはクラスの API も個別にテストします。

自分で試してみよう

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 rows = new List<string>(); string line; while ((line=Console.ReadLine()) != null) rows.Add(line);
string[] input=rows.ToArray();
var a0 = I(input[0]); string before0=Snapshot(a0);
var a1 = I(input[1]); string before1=Snapshot(a1);
if (Catalog.Pricing.Total(7,3) != 21 || Catalog.Pricing.Total(4,0) != 0) throw new Exception("Pricing API");
var result = Exercise.Solve(a0,a1);
if (Snapshot(a0) != before0) throw new Exception("Input mutation");
if (Snapshot(a1) != before1) throw new Exception("Input mutation");
Console.WriteLine(result);
}
}
quiz icon腕試し

このレッスンには短いクイズがあります。レッスンを始めて解答し、進捗を記録しましょう。

C++開発者のためのC#のすべてのレッスン

自分で練習してみよう: C#オンラインコンパイラ