Skip to content

Commit 32dc523

Browse files
初回公開用のサンプルコードを追加
1 parent 8d8288d commit 32dc523

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1529
-0
lines changed

CSharp CheatSeet/10-1 Indexer.linq

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<Query Kind="Program" />
2+
3+
void Main()
4+
{
5+
var my = new MyClass();
6+
my[1] = 100;
7+
my[1].Dump();
8+
my["test"].Dump();
9+
}
10+
11+
public class MyClass
12+
{
13+
private int[] array = new int[100];
14+
private Dictionary<string, int> dict = new Dictionary<string, int>();
15+
16+
public int this[int i]
17+
{
18+
get
19+
{
20+
return array[i];
21+
}
22+
set
23+
{
24+
array[i] = value;
25+
}
26+
}
27+
28+
public int this[string s] => dict.ContainsKey(s) ? dict[s] : 0;
29+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<Query Kind="Program" />
2+
3+
void Main()
4+
{
5+
var c1 = new Complex(1, 3) + new Complex(2, 4); // 3 + 7i
6+
c1.Dump();
7+
8+
var c2 = -new Complex(2, -2); //-2+2i
9+
c2.Dump();
10+
}
11+
12+
public class Complex
13+
{
14+
public double Real { get; }
15+
public double Imaginary { get; }
16+
17+
public Complex(double real, double imaginary)
18+
{
19+
Real = real;
20+
Imaginary = imaginary;
21+
}
22+
23+
public static Complex operator -(Complex c1)
24+
{
25+
return new Complex(-c1.Real, -c1.Imaginary);
26+
}
27+
28+
public static Complex operator +(Complex c1, Complex c2)
29+
{
30+
return new Complex(c1.Real + c2.Real, c1.Imaginary + c2.Imaginary);
31+
}
32+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<Query Kind="Program" />
2+
3+
void Main()
4+
{
5+
var my1 = new MyClass();
6+
//0
7+
my1.Value.Dump();
8+
9+
var my2 = new MyClass(2);
10+
//2
11+
my2.Value.Dump();
12+
}
13+
14+
public class MyClass
15+
{
16+
public int Value { get; set; }
17+
18+
public MyClass() : this(0)
19+
{}
20+
21+
public MyClass(int v)
22+
{
23+
Value = v;
24+
}
25+
26+
}
27+
28+
//singletonパターンなど、コンストラクタ呼び出しを公開したくない場合はprivateにできる
29+
public class MyClass2
30+
{
31+
public static MyClass2 Instance = new MyClass2();
32+
33+
private MyClass2()
34+
{}
35+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Query Kind="Program" />
2+
3+
void Main()
4+
{
5+
var my = new MyClass();
6+
//5
7+
my.Value.Dump();
8+
}
9+
10+
public class MyClass
11+
{
12+
static int DefaultValue;
13+
static MyClass()
14+
{
15+
//代入するだけであればフィールドの初期化子として書けるが
16+
//より複雑な初期化も可能
17+
DefaultValue = 5;
18+
}
19+
20+
public int Value { get; set; } = DefaultValue;
21+
}

CSharp CheatSeet/12-3 Destructor.linq

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Query Kind="Program" />
2+
3+
void Main()
4+
{
5+
var my = new MyClass();
6+
my = null;
7+
GC.Collect();
8+
GC.WaitForPendingFinalizers(); // Destruct
9+
}
10+
11+
public class MyClass
12+
{
13+
// デストラクター
14+
~MyClass()
15+
{
16+
Console.WriteLine("Destruct");
17+
}
18+
}

CSharp CheatSeet/13-1 Struct.linq

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<Query Kind="Program" />
2+
3+
void Main()
4+
{
5+
var p1 = new Point(10);
6+
//参照ではなく、コピーした値がp2に割り当てられる
7+
var p2 = p1;
8+
//p1のプロパティを更新してもp2には影響がない
9+
p1.X = 1;
10+
p1.Dump(); //p1.X=1
11+
p2.Dump(); //p2.X=10
12+
13+
//構造体をデフォルト値はnullではない
14+
var points = new Point[10];
15+
//point[0].X=0
16+
points[0].Dump();
17+
}
18+
19+
struct Point
20+
{
21+
private int x;
22+
public int X
23+
{
24+
get { return x; }
25+
set { x = value; }
26+
}
27+
public Point(int x)
28+
{
29+
this.x = x;
30+
}
31+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<Query Kind="Program" />
2+
3+
void Main()
4+
{
5+
var my = new MyClass1();
6+
my.Test2(); //MyClass1.Test2
7+
8+
var my2 = new MyClass2();
9+
my2.Test2(); //1
10+
}
11+
12+
public class MyBase
13+
{
14+
public int Value { get; set; } = 0;
15+
16+
public void Test1()
17+
{
18+
Console.WriteLine("MyBase.Test1");
19+
}
20+
21+
public virtual void Test2()
22+
{
23+
Console.WriteLine("MyBase.Test2");
24+
}
25+
}
26+
27+
public class MyClass1 : MyBase
28+
{
29+
public new int Value { get; set; } = 1;
30+
31+
public override void Test2()
32+
{
33+
Console.WriteLine("MyClass1.Test2");
34+
}
35+
}
36+
37+
public class MyClass2 : MyClass1
38+
{
39+
public override void Test2()
40+
{
41+
Console.WriteLine(Value);
42+
}
43+
}

CSharp CheatSeet/14-2 Abstract.linq

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<Query Kind="Program" />
2+
3+
void Main()
4+
{
5+
var my = new MyClass();
6+
my.Test1(); // MyAbstractClass.Test1
7+
my.Test2(); // MyClass.Test2
8+
my.Test3(); // MyClass.Test3
9+
10+
var my2 = new MyClass2();
11+
my2.Test1(); // MyAbstractClass.Test1
12+
my2.Test2(); // MyClass2.Test2
13+
my2.Test3(); // MyClass.Test3
14+
}
15+
16+
public abstract class MyAbstractClass
17+
{
18+
public void Test1()
19+
{
20+
Console.WriteLine("MyAbstractClass.Test1");
21+
}
22+
23+
public abstract void Test2();
24+
25+
public abstract void Test3();
26+
}
27+
28+
public class MyClass : MyAbstractClass
29+
{
30+
public override void Test2()
31+
{
32+
Console.WriteLine("MyClass.Test2");
33+
}
34+
35+
public sealed override void Test3()
36+
{
37+
Console.WriteLine("MyClass.Test3");
38+
}
39+
}
40+
41+
public sealed class MyClass2 : MyClass
42+
{
43+
public override void Test2()
44+
{
45+
Console.WriteLine("MyClass2.Test2");
46+
}
47+
}

CSharp CheatSeet/14-3 Interface.linq

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<Query Kind="Program" />
2+
3+
void Main()
4+
{
5+
var s1 = new Surface();
6+
s1.Name = "a";
7+
s1.Paint(); // Paint
8+
((ISurface)s1).Paint(); // Paint
9+
((IPaintable)s1).Paint(); // Paint
10+
11+
var s2 = new Surface2();
12+
s2.Paint(); // Paint
13+
((ISurface)s2).Paint(); // Paint
14+
((IPaintable)s2).Paint(); // IPaintable.Paint
15+
}
16+
17+
interface IPaintable
18+
{
19+
void Paint();
20+
}
21+
22+
interface ISurface
23+
{
24+
string Name { get; set; }
25+
void Paint();
26+
}
27+
28+
public class Surface : ISurface, IPaintable
29+
{
30+
public string Name { get; set; }
31+
public void Paint()
32+
{
33+
Console.WriteLine("Paint");
34+
}
35+
}
36+
37+
public class Surface2 : ISurface, IPaintable
38+
{
39+
public string Name { get; set; }
40+
public void Paint()
41+
{
42+
Console.WriteLine("Paint");
43+
}
44+
45+
void IPaintable.Paint()
46+
{
47+
Console.WriteLine("IPaintable.Paint");
48+
}
49+
}

CSharp CheatSeet/15-1 Enum.linq

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<Query Kind="Program" />
2+
3+
void Main()
4+
{
5+
var t1 = Color.Red;
6+
t1.Dump();
7+
8+
var t2 = Color.Blue;
9+
t2.Dump();
10+
11+
//定義されていない値をキャストすることが可能だが、非推奨
12+
var t3 = (LongType)23;
13+
t3.Dump();
14+
}
15+
16+
public enum Color
17+
{
18+
Red,
19+
Blue,
20+
Orange
21+
}
22+
23+
public enum LongType : long
24+
{
25+
Solid = int.MaxValue + 1L,
26+
Soft = 1,
27+
Hard = Solid
28+
}

0 commit comments

Comments
 (0)