Skip to content

Commit 36291fc

Browse files
author
tanaka_733
authored
Merge pull request #1 from tanaka-takayoshi/csharp7
C# 7.0
2 parents 32dc523 + 052f28d commit 36291fc

File tree

138 files changed

+10447
-78
lines changed

Some content is hidden

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

138 files changed

+10447
-78
lines changed

.vscode/launch.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": ".NET Core Launch (console)",
6+
"type": "coreclr",
7+
"request": "launch",
8+
"preLaunchTask": "build",
9+
"program": "${workspaceRoot}\\CheatSheetConsoleApp\\bin\\Debug\\netcoreapp1.0\\CheatSheetConsoleApp.dll",
10+
"args": ["Hello C#"],
11+
"cwd": "${workspaceRoot}\\CheatSheetConsoleApp",
12+
"console": "internalConsole",
13+
"stopAtEntry": false,
14+
"internalConsoleOptions": "openOnSessionStart"
15+
},
16+
{
17+
"name": ".NET Core Attach",
18+
"type": "coreclr",
19+
"request": "attach",
20+
"processId": "${command:pickProcess}"
21+
}
22+
]
23+
}

.vscode/tasks.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"version": "0.1.0",
3+
"command": "dotnet",
4+
"isShellCommand": true,
5+
"args": [],
6+
"tasks": [
7+
{
8+
"taskName": "build",
9+
"args": [
10+
"${workspaceRoot}\\CheatSheetConsoleApp\\CheatSheetConsoleApp.csproj"
11+
],
12+
"isBuildCommand": true,
13+
"problemMatcher": "$msCompile"
14+
}
15+
]
16+
}

CSharp CheatSeet/17-1 Exception.linq

Lines changed: 0 additions & 28 deletions
This file was deleted.

CSharp CheatSeet/3-5 CastOperator_is_as.linq

Lines changed: 0 additions & 38 deletions
This file was deleted.

CSharp CheatSeet/10-1 Indexer.linq renamed to CSharp CheatSheet/10-1 Indexer.linq

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,15 @@ public class MyClass
2626
}
2727

2828
public int this[string s] => dict.ContainsKey(s) ? dict[s] : 0;
29-
}
29+
}
30+
31+
public class MyClass2
32+
{
33+
private Dictionary<string, int> dict = new Dictionary<string, int>();
34+
//C# 7.0より式形式で記述可能
35+
public int this[string s]
36+
{
37+
get => dict.ContainsKey(s) ? dict[s] : 0;
38+
set => dict[s] = value;
39+
}
40+
}

CSharp CheatSeet/12-1 InstanceConstructor.linq renamed to CSharp CheatSheet/12-1 InstanceConstructor.linq

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,9 @@ public class MyClass2
3333
private MyClass2()
3434
{}
3535
}
36+
37+
public class MyClass3
38+
{
39+
private static int counter = 0;
40+
public MyClass3() => ++counter;
41+
}

CSharp CheatSeet/12-2 StaticConstructor.linq renamed to CSharp CheatSheet/12-2 StaticConstructor.linq

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,12 @@ public class MyClass
1818
}
1919

2020
public int Value { get; set; } = DefaultValue;
21+
}
22+
23+
public static class MyClass2
24+
{
25+
private static int counter = 0;
26+
private static string counterValue;
27+
//C#7.0からは式形式で記述可能
28+
static MyClass2() => counterValue = counter.ToString();
2129
}

CSharp CheatSeet/12-3 Destructor.linq renamed to CSharp CheatSheet/12-3 Destructor.linq

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,10 @@ public class MyClass
1616
Console.WriteLine("Destruct");
1717
}
1818
}
19+
20+
public class MyClass2
21+
{
22+
private static int counter = 0;
23+
//C#7.0からは式形式で記述可能
24+
~MyClass2() => --counter;
25+
}

CSharp CheatSheet/12B-1 Tuple.linq

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<Query Kind="Program" />
2+
3+
void Main()
4+
{
5+
// 文字列を全て小文字にしたものと全て大文字にしたものを取得する
6+
var texts = new[] { "aaA", "bBb", "cCC" };
7+
8+
// 匿名オブジェクトを利用
9+
foreach (var text in texts.Select(x => new { lower = x.ToLower(), upper = x.ToUpper() }))
10+
{
11+
Console.WriteLine(text);
12+
}
13+
14+
// Tupleを利用
15+
foreach (var text in texts.Select(x => new Tuple<string, string>(x.ToLower(), x.ToUpper())))
16+
{
17+
Console.WriteLine(text);
18+
}
19+
20+
// C# 7.0のタプルを利用
21+
foreach (var text in texts.Select(Convert))
22+
{
23+
Console.WriteLine($"lower={text.lower}, upper={text.upper}");
24+
}
25+
26+
var str1 = "aaAA";
27+
// タプルリテラルで宣言
28+
var t1 = (str1.ToLower(), str1.ToUpper()); // 要素名は任意なのでつけなくてもよい
29+
Console.WriteLine($"Item1={t1.Item1}"); //つけない場合はC#6.0以前のタプルと同様のItemNという名前で参照
30+
var t2 = (lower: str1.ToLower(), upper: str1.ToUpper());
31+
// new形式はコンパイルエラー
32+
//var t3 = new (int, int)(0, 1);
33+
var t4 = (lower: str1.ToLower(), str1.ToUpper()); // 一部のみ要素名を省略することも可
34+
Console.WriteLine($"{t4.lower}, {t4.Item2}");
35+
// 同じ要素名で重複するとコンパイルエラー
36+
//var t5 = (lower: text.ToLower(), lower: text.ToLower());
37+
}
38+
39+
(string lower, string upper) Convert(string text)
40+
{
41+
return (lower: text.ToLower(), upper: text.ToUpper());
42+
}

0 commit comments

Comments
 (0)