【C#, CLI】クラスライブラリを参照、monoでコンパイル実行する
環境
macOS Sierra 10.12.6 mono version 5.8.0.108 NVIM v0.2.2
準備と実行
[1]. コンソールアプリを作る
dotnet new -n Console
[2]. クラスライブラリを作る
dotnet new classlib -n Clazz
dotnet new コマンド - .NET Core CLI | Microsoft Docs
[3]. dllを作成する
csc /target:library /out:Clazz.DLL ../Clazz/Clazz.cs
方法: コマンド ラインを使用してアセンブリを作成および使用する (C# および Visual Basic)
[4]. Console.cs のディレクティブに Clazz を追加する
[5]. mono を使ってコンパイル/実行する
dll を元にコンソールアプリをコンパイル
mcs -r:Clazz.DLL Console.cs
.exeが作成されるので実行
mono Console.exe
dll の作成は dotnet build でもいけるのかな?
dotnet build コマンド - .NET Core CLI | Microsoft Docs
配列とList<T>
配列は「インスタンス生成時に格納できる要素数が決まり、後から変更ができない」、
List<T>
は「インタスタンス生成後に、要素を追加、挿入、削除を行える」。
配列、List<T>
ともに、IEnumerable<T>
インターフェースを持つ。
IEnumerable<T>
型の連続したデータをシーケンスという。
オブジェクトの総称は コレクション
と呼び、扱う。
入力
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 5 namespace Program 6 { 13 class Program 14 { 15 static void Main(string[] args) 16 { 25 var numbers = Enumerable.Repeat(-1, 20).ToList(); // List<T>を同じ値で埋める 26 var strings = Enumerable.Repeat("(unknown)", 12).ToArray(); // 配列を同じ値で埋める 27 var array = Enumerable.Range(1, 20).ToArray(); // 配列に連続した値を設定する 28 29 Console.WriteLine(numbers); 30 Console.WriteLine(strings); 31 Console.WriteLine(array); 32 } 33 } 34 }
出力
System.Collections.Generic.List`1[System.Int32] System.String[] System.Int32[]
【Mac】ターミナル, bash コマンド, シンボリックリンク
頻繁に使うコマンドを列挙。 シンボリックリンクの変更に関しては参考URLより。
Command + t … 新しいタブを開く Command + w … 現在のタブを閉じる Shift + Command + → … 右のタブに移動 Shift + Command + ← … 左のタブに移動
ショートカットキー 説明 Ctrl + A カーソルを行頭へ Ctrl + E カーソルを行末へ Ctrl + K カーソル位置から行末までを削除 Ctrl + U カーソル位置から行頭までを削除 Ctrl + W カーソル位置から単語の先頭までを削除 Ctrl + D カーソル位置の文字を削除
参考:
コマンドラインの操作(カーソル移動・文字削除・補完) - Linux入門 - Webkaru
シンボリックリンクを上書きしてリンク先を変更する | 俺的備忘録 〜なんかいろいろ〜
Bashのコンソールで単語移動するショートカット - Qiita
Macでターミナルを使うときはショートカットキーを覚えよう!! - よんちゅBlog
LINQ基礎
クエリ演算子
LINQが用意しているメソッドのこと。
クエリ演算子は全て IEnumerable<T>
に対する拡張メソッドとして定義されている。
シーケンス
標準クエリ演算子の操作対象のデータをシーケンスと呼ぶ。配列や List<T>
等。
クエリ遅延実行/即時実行
遅延実行
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 5 namespace Program 6 { 7 class Program 8 { 9 static void Main(string[] args) 10 { 11 string[] names = { 12 "Tokyo", "New Delhi", "Bangkok", "Paris", 13 }; 14 var query = names.Where(x => x.Length <= 5); 15 16 foreach (var item in query) 17 Console.WriteLine(item); 18 19 Console.WriteLine("----------------"); 20 21 names[0] = "Osaka"; 22 foreach (var item in query) 23 Console.WriteLine(item); 24 } 25 } 26 }
Tokyo Paris ---------------- Osaka Paris
Whereメソッドが呼び出されても、検索はその時には行われずに、
実際に値が必要になったとき(foreachで要素を取り出したとき)に、クエリが実行される。
即時実行
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 5 namespace Program 6 { 7 class Program 8 { 9 static void Main(string[] args) 10 { 11 string[] names = { 12 "Tokyo", "New Delhi", "Bangkok", "Paris", 13 }; 14 var query = names.Where(x => x.Length <= 5).ToArray(); // 配列に変換 15 16 foreach (var item in query) 17 Console.WriteLine(item); 18 19 Console.WriteLine("----------------"); 20 21 names[0] = "Osaka"; 22 foreach (var item in query) 23 Console.WriteLine(item); 24 } 25 } 26 }
Tokyo Paris ---------------- Tokyo Paris
ToArrayメソッドが呼び出されたときにクエリが実行され、結果が配列に格納される。
ToList メソッドに変えても同じ。Countメソッドとかも即時実行。
参考 :
実戦で役立つ C#プログラミングのイディオム/定石&パターン:書籍案内|技術評論社
LINQ クエリの概要 (C#) | Microsoft Docs
【C#】Lake Counting
大きさがNxMの庭があります。そこに雨が降り、水溜まりができました。 水溜りは8近傍で隣接している場合につながっているとみなします。全部でいくつかの水たまりがあるでしょうか?(8近傍とは、次のWに対する*の部分を指します。) *** *W* *** 制約 N,M ≦ 100
using System; namespace Program { class Program { public static int N = 10; // 庭の広さ(縦) public static int M = 12; // 庭の広さ(横) public static int MAX_N = 0; public static int MAX_M = 0; public static char[][] field = { new []{'W', '.', '.', '.', '.', '.', '.', '.', '.', 'W', 'W', '.'}, new []{'.', 'W', 'W', 'W', '.', '.', '.', '.', '.', 'W', 'W', 'W'}, new []{'.', '.', '.', '.', 'W', 'W', '.', '.', '.', 'W', 'W', '.'}, new []{'.', '.', '.', '.', '.', '.', '.', '.', '.', 'W', 'W', '.'}, new []{'.', '.', '.', '.', '.', '.', '.', '.', '.', 'W', '.', '.'}, new []{'.', '.', 'W', '.', '.', '.', '.', '.', '.', 'W', '.', '.'}, new []{'.', 'W', '.', 'W', '.', '.', '.', '.', '.', 'W', 'W', '.'}, new []{'W', '.', 'W', '.', 'W', '.', '.', '.', '.', '.', 'W', '.'}, new []{'.', 'W', '.', 'W', '.', '.', '.', '.', '.', '.', 'W', '.'}, new []{'.', '.', 'W', '.', '.', '.', '.', '.', '.', '.', 'W', '.'} }; public static void dfs(int x = 0, int y = 0) { field[x][y] = '.'; for (int dx = -1; dx <= 1; dx++) { for (int dy = -1; dy <= 1; dy++) { int nx = x + dx; int ny = y + dy; if (0 <= nx && nx < N && 0 <= ny && ny < M && field[nx][ny] == 'W') { dfs(nx, ny); } } } } public static void solve() { int res = 0; for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { if (field[i][j] == 'W') { dfs(i, j); res++; } } } Console.WriteLine(res); // 3 } public static void Main(string[] args) { solve(); } } }
競技プログラミング
競技プログラミングを始めるにあたって参考にさせてもらった記事
TopCorder登録
Adventer
「競プロ!!」 競技プログラミング Advent Calendar 2017 - Adventar
C# で競プロをする話 - うにゅーん、って感じだ
競技プログラミングのための C# (4.0 以降) の Tips 詰め合わせ - C♯の勉強
環境構築
MacでC#のコードをコンパイルして実行する - Qiita
MacのAtomでOmniSharpを使う | C# / note.nkmk.me
Tips
[Sy] Topcoder SRM に初参加してハマったこと&感想 | Syntax Error.
競技プログラミングのための C# (4.0 以降) の Tips 詰め合わせ - C♯の勉強
TopCoder SRM 648 Div1 | tatsyblog
Configuring the TopCoder Arena for C# developers - Productive C#