Skip to content

Commit

Permalink
初回公開用のサンプルコードを追加
Browse files Browse the repository at this point in the history
  • Loading branch information
tanaka-takayoshi committed Apr 12, 2016
1 parent 8d8288d commit 32dc523
Show file tree
Hide file tree
Showing 49 changed files with 1,529 additions and 0 deletions.
29 changes: 29 additions & 0 deletions CSharp CheatSeet/10-1 Indexer.linq
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Query Kind="Program" />

void Main()
{
var my = new MyClass();
my[1] = 100;
my[1].Dump();
my["test"].Dump();
}

public class MyClass
{
private int[] array = new int[100];
private Dictionary<string, int> dict = new Dictionary<string, int>();

public int this[int i]
{
get
{
return array[i];
}
set
{
array[i] = value;
}
}

public int this[string s] => dict.ContainsKey(s) ? dict[s] : 0;
}
32 changes: 32 additions & 0 deletions CSharp CheatSeet/11-1 UserDefinedOperator.linq
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<Query Kind="Program" />

void Main()
{
var c1 = new Complex(1, 3) + new Complex(2, 4); // 3 + 7i
c1.Dump();

var c2 = -new Complex(2, -2); //-2+2i
c2.Dump();
}

public class Complex
{
public double Real { get; }
public double Imaginary { get; }

public Complex(double real, double imaginary)
{
Real = real;
Imaginary = imaginary;
}

public static Complex operator -(Complex c1)
{
return new Complex(-c1.Real, -c1.Imaginary);
}

public static Complex operator +(Complex c1, Complex c2)
{
return new Complex(c1.Real + c2.Real, c1.Imaginary + c2.Imaginary);
}
}
35 changes: 35 additions & 0 deletions CSharp CheatSeet/12-1 InstanceConstructor.linq
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<Query Kind="Program" />

void Main()
{
var my1 = new MyClass();
//0
my1.Value.Dump();

var my2 = new MyClass(2);
//2
my2.Value.Dump();
}

public class MyClass
{
public int Value { get; set; }

public MyClass() : this(0)
{}

public MyClass(int v)
{
Value = v;
}

}

//singletonパターンなど、コンストラクタ呼び出しを公開したくない場合はprivateにできる
public class MyClass2
{
public static MyClass2 Instance = new MyClass2();

private MyClass2()
{}
}
21 changes: 21 additions & 0 deletions CSharp CheatSeet/12-2 StaticConstructor.linq
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Query Kind="Program" />

void Main()
{
var my = new MyClass();
//5
my.Value.Dump();
}

public class MyClass
{
static int DefaultValue;
static MyClass()
{
//代入するだけであればフィールドの初期化子として書けるが
//より複雑な初期化も可能
DefaultValue = 5;
}

public int Value { get; set; } = DefaultValue;
}
18 changes: 18 additions & 0 deletions CSharp CheatSeet/12-3 Destructor.linq
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Query Kind="Program" />

void Main()
{
var my = new MyClass();
my = null;
GC.Collect();
GC.WaitForPendingFinalizers(); // Destruct
}

public class MyClass
{
// デストラクター
~MyClass()
{
Console.WriteLine("Destruct");
}
}
31 changes: 31 additions & 0 deletions CSharp CheatSeet/13-1 Struct.linq
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<Query Kind="Program" />

void Main()
{
var p1 = new Point(10);
//参照ではなく、コピーした値がp2に割り当てられる
var p2 = p1;
//p1のプロパティを更新してもp2には影響がない
p1.X = 1;
p1.Dump(); //p1.X=1
p2.Dump(); //p2.X=10

//構造体をデフォルト値はnullではない
var points = new Point[10];
//point[0].X=0
points[0].Dump();
}

struct Point
{
private int x;
public int X
{
get { return x; }
set { x = value; }
}
public Point(int x)
{
this.x = x;
}
}
43 changes: 43 additions & 0 deletions CSharp CheatSeet/14-1 Inheritance.linq
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<Query Kind="Program" />

void Main()
{
var my = new MyClass1();
my.Test2(); //MyClass1.Test2

var my2 = new MyClass2();
my2.Test2(); //1
}

public class MyBase
{
public int Value { get; set; } = 0;

public void Test1()
{
Console.WriteLine("MyBase.Test1");
}

public virtual void Test2()
{
Console.WriteLine("MyBase.Test2");
}
}

public class MyClass1 : MyBase
{
public new int Value { get; set; } = 1;

public override void Test2()
{
Console.WriteLine("MyClass1.Test2");
}
}

public class MyClass2 : MyClass1
{
public override void Test2()
{
Console.WriteLine(Value);
}
}
47 changes: 47 additions & 0 deletions CSharp CheatSeet/14-2 Abstract.linq
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<Query Kind="Program" />

void Main()
{
var my = new MyClass();
my.Test1(); // MyAbstractClass.Test1
my.Test2(); // MyClass.Test2
my.Test3(); // MyClass.Test3

var my2 = new MyClass2();
my2.Test1(); // MyAbstractClass.Test1
my2.Test2(); // MyClass2.Test2
my2.Test3(); // MyClass.Test3
}

public abstract class MyAbstractClass
{
public void Test1()
{
Console.WriteLine("MyAbstractClass.Test1");
}

public abstract void Test2();

public abstract void Test3();
}

public class MyClass : MyAbstractClass
{
public override void Test2()
{
Console.WriteLine("MyClass.Test2");
}

public sealed override void Test3()
{
Console.WriteLine("MyClass.Test3");
}
}

public sealed class MyClass2 : MyClass
{
public override void Test2()
{
Console.WriteLine("MyClass2.Test2");
}
}
49 changes: 49 additions & 0 deletions CSharp CheatSeet/14-3 Interface.linq
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<Query Kind="Program" />

void Main()
{
var s1 = new Surface();
s1.Name = "a";
s1.Paint(); // Paint
((ISurface)s1).Paint(); // Paint
((IPaintable)s1).Paint(); // Paint

var s2 = new Surface2();
s2.Paint(); // Paint
((ISurface)s2).Paint(); // Paint
((IPaintable)s2).Paint(); // IPaintable.Paint
}

interface IPaintable
{
void Paint();
}

interface ISurface
{
string Name { get; set; }
void Paint();
}

public class Surface : ISurface, IPaintable
{
public string Name { get; set; }
public void Paint()
{
Console.WriteLine("Paint");
}
}

public class Surface2 : ISurface, IPaintable
{
public string Name { get; set; }
public void Paint()
{
Console.WriteLine("Paint");
}

void IPaintable.Paint()
{
Console.WriteLine("IPaintable.Paint");
}
}
28 changes: 28 additions & 0 deletions CSharp CheatSeet/15-1 Enum.linq
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Query Kind="Program" />

void Main()
{
var t1 = Color.Red;
t1.Dump();

var t2 = Color.Blue;
t2.Dump();

//定義されていない値をキャストすることが可能だが、非推奨
var t3 = (LongType)23;
t3.Dump();
}

public enum Color
{
Red,
Blue,
Orange
}

public enum LongType : long
{
Solid = int.MaxValue + 1L,
Soft = 1,
Hard = Solid
}
17 changes: 17 additions & 0 deletions CSharp CheatSeet/15-2 Flags.linq
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Query Kind="Program" />

void Main()
{
var colors = Color.Red | Color.Blue;
colors.HasFlag(Color.Red).Dump(); //True
(colors == Color.Red).Dump(); // False
colors.HasFlag(Color.Yellow).Dump(); // False
}

[Flags]
public enum Color
{
Red,
Blue,
Yellow
}
Loading

0 comments on commit 32dc523

Please sign in to comment.