-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8d8288d
commit 32dc523
Showing
49 changed files
with
1,529 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.