Skip to content

Instantly share code, notes, and snippets.

@ytabuchi
Last active July 2, 2016 10:16
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ytabuchi/de7c8d7ba0f9368cf3d805299e233c4a to your computer and use it in GitHub Desktop.
uti platform
com.xamarin.workbook
iOS

Xamarin Workbooks

C# での playground と同じようなツールで、Xamarin 製で現在のバージョンは 0.9.0.14 です。無料で こちら からダウンロードできます。C# の世界に足を踏み入れてみましょう。

早速 C# コードを書いてみます。その前に、文章を書ける。Markdown で。文章を追加して保存すると、Markdown が更新されます。

var str = "Hello Xamarin Workbooks";
double Square(double x)
{
	return x * x;
}

関数を定義して使うこともできます。

for(int i=0; i<20; ++i)
{
	double x = i * 0.1;
	double y = Square(x); // 関数呼び出し

	Console.WriteLine($"{x:N2}の2乗={y:N4}");
}

なにができるの?

C# ならではの LINQ を使ってみましょう。
Person クラスのリストを用意します。

using System.Collections.Generic;
class Person
{
	public string Name;
	public int Age;
	public string Country;
	
	public Person(string name, int age, string country) 
	{
		Name = name;
		Age = age;
		Country = country;
	}
}

var peaple = new List<Person> {
    new Person("ytabuchi", 41, "Japan"),
	new Person("Bill Gates", 60, "USA"),
	new Person("Nadella Satyanarayana", 49, "USA"),
	new Person("Miguel de Icaza", 43, "USA")
};

foreach (var item in peaple)
{
	System.Console.WriteLine($"Name: {item.Name}, Age: {item.Age}, Country: {item.Country}");
}

アメリカ出身の50歳未満の人の名前を表示します。

var linq = peaple.Where(p => p.Country.Contains("USA")).Where(p => p.Age < 50).Select(p => p.Name);

現在の最新版、C# 6.0 の機能もサポートされています。例えば Null 条件演算子。Null 合体演算子と組み合わせると便利ですね。

var names = new string[] { "Foo", null };
var lengths = names.Select(names => names?.Length ?? 0);

つまり C# はいいぞ。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment