1
1
---
2
2
title : C# 7
3
- category : others
3
+ category : C-like
4
+ updated : 2018-12-06
4
5
layout : 2017/sheet
6
+ prism_languages : [csharp]
5
7
description : |
6
8
A quick overview of C# 7
7
9
---
8
10
9
11
### Out Variables
10
12
11
- ``` cs
13
+ ``` csharp
12
14
public void PrintCoordinates (Point p )
13
15
{
14
16
p .GetCoordinates (out int x , out int y );
@@ -22,7 +24,7 @@ public void PrintCoordinates(Point p)
22
24
23
25
#### Is-expressions with patterns
24
26
25
- ``` cs
27
+ ``` csharp
26
28
public void PrintStars (object o )
27
29
{
28
30
if (o is null ) return ; // constant pattern "null"
@@ -33,7 +35,7 @@ public void PrintStars(object o)
33
35
34
36
#### Switch statements with patterns
35
37
36
- ``` cs
38
+ ``` csharp
37
39
switch (shape )
38
40
{
39
41
case Circle c :
@@ -57,55 +59,55 @@ switch(shape)
57
59
58
60
#### Tuple type
59
61
60
- ``` cs
62
+ ``` csharp
61
63
(string , string , string ) LookupName (long id ) // tuple return type
62
64
{
63
65
.. . // retrieve first, middle and last from data storage
64
66
return (first , middle , last ); // tuple literal
65
67
}
66
68
```
67
69
68
- ``` cs
70
+ ``` csharp
69
71
var names = LookupName (id );
70
72
WriteLine ($" found {names .Item1 } {names .Item3 }." );
71
73
```
72
74
73
75
#### Tuple elements with name
74
76
75
- ``` cs
77
+ ``` csharp
76
78
(string first , string middle , string last ) LookupName (long id ) // tuple elements have names
77
79
```
78
80
79
- ``` cs
81
+ ``` csharp
80
82
var names = LookupName (id );
81
83
WriteLine ($" found {names .first } {names .last }." );
82
84
```
83
85
84
86
#### Tuple Literals
85
87
86
- ``` cs
88
+ ``` csharp
87
89
return (first : first , middle : middle , last : last ); // named tuple elements in a literal
88
90
```
89
91
90
92
#### Tuple Deconstruction
91
93
92
- ``` cs
94
+ ``` csharp
93
95
(var first , var middle , var last ) = LookupName (id1 );
94
96
WriteLine ($" found {first } {last }." );
95
97
```
96
98
or
97
- ``` cs
99
+ ``` csharp
98
100
var (first , middle , last ) = LookupName (id1 ); // var outside
99
101
```
100
102
or
101
- ``` cs
103
+ ``` csharp
102
104
(first , middle , last ) = LookupName (id2 ); // assign onto existing variables
103
105
```
104
106
105
107
106
108
### Local Functions
107
109
108
- ``` cs
110
+ ``` csharp
109
111
public int Fibonacci (int x )
110
112
{
111
113
if (x < 0 ) throw new ArgumentException (" Less negativity please!" , nameof (x ));
@@ -124,20 +126,20 @@ public int Fibonacci(int x)
124
126
125
127
#### Digit Separator inside numbers literals
126
128
127
- ``` cs
129
+ ``` csharp
128
130
var d = 123_ 456 ;
129
131
var x = 0x AB_ CD_ EF ;
130
132
```
131
133
132
134
#### Binary Literals
133
135
134
- ``` cs
136
+ ``` csharp
135
137
var b = 0b 1010_ 1011_ 1100_ 1101_ 1110_ 1111 ;
136
138
```
137
139
138
140
### Ref Returns and Locals
139
141
140
- ``` cs
142
+ ``` csharp
141
143
public ref int Find (int number , int [] numbers )
142
144
{
143
145
for (int i = 0 ; i < numbers .Length ; i ++ )
@@ -160,7 +162,7 @@ WriteLine(array[4]); // prints 9
160
162
161
163
C# 7.0 adds accessors, constructors and finalizers to the list of things that can have expression bodies:
162
164
163
- ``` cs
165
+ ``` csharp
164
166
class Person
165
167
{
166
168
private static ConcurrentDictionary <int , string > names = new ConcurrentDictionary <int , string >();
@@ -178,7 +180,7 @@ class Person
178
180
179
181
### Throw Expressions
180
182
181
- ``` cs
183
+ ``` csharp
182
184
class Person
183
185
{
184
186
public string Name { get ; }
0 commit comments