Skip to content

Commit dccb591

Browse files
committed
Adding C-like category and C# cleanup
1 parent de5f5a4 commit dccb591

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

Diff for: _data/categories.yml

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ names:
33
- Analytics
44
- Ansible
55
- Apps
6+
- C-like
67
- CLI
78
- CSS
89
- Databases

Diff for: csharp7.md

+20-18
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
---
22
title: C# 7
3-
category: others
3+
category: C-like
4+
updated: 2018-12-06
45
layout: 2017/sheet
6+
prism_languages: [csharp]
57
description: |
68
A quick overview of C# 7
79
---
810

911
### Out Variables
1012

11-
```cs
13+
```csharp
1214
public void PrintCoordinates(Point p)
1315
{
1416
p.GetCoordinates(out int x, out int y);
@@ -22,7 +24,7 @@ public void PrintCoordinates(Point p)
2224

2325
#### Is-expressions with patterns
2426

25-
```cs
27+
```csharp
2628
public void PrintStars(object o)
2729
{
2830
if (o is null) return; // constant pattern "null"
@@ -33,7 +35,7 @@ public void PrintStars(object o)
3335

3436
#### Switch statements with patterns
3537

36-
```cs
38+
```csharp
3739
switch(shape)
3840
{
3941
case Circle c:
@@ -57,55 +59,55 @@ switch(shape)
5759

5860
#### Tuple type
5961

60-
```cs
62+
```csharp
6163
(string, string, string) LookupName(long id) // tuple return type
6264
{
6365
... // retrieve first, middle and last from data storage
6466
return (first, middle, last); // tuple literal
6567
}
6668
```
6769

68-
```cs
70+
```csharp
6971
var names = LookupName(id);
7072
WriteLine($"found {names.Item1} {names.Item3}.");
7173
```
7274

7375
#### Tuple elements with name
7476

75-
```cs
77+
```csharp
7678
(string first, string middle, string last) LookupName(long id) // tuple elements have names
7779
```
7880

79-
```cs
81+
```csharp
8082
var names = LookupName(id);
8183
WriteLine($"found {names.first} {names.last}.");
8284
```
8385

8486
#### Tuple Literals
8587

86-
```cs
88+
```csharp
8789
return (first: first, middle: middle, last: last); // named tuple elements in a literal
8890
```
8991

9092
#### Tuple Deconstruction
9193

92-
```cs
94+
```csharp
9395
(var first, var middle, var last) = LookupName(id1);
9496
WriteLine($"found {first} {last}.");
9597
```
9698
or
97-
```cs
99+
```csharp
98100
var (first, middle, last) = LookupName(id1); // var outside
99101
```
100102
or
101-
```cs
103+
```csharp
102104
(first, middle, last) = LookupName(id2); // assign onto existing variables
103105
```
104106

105107

106108
### Local Functions
107109

108-
```cs
110+
```csharp
109111
public int Fibonacci(int x)
110112
{
111113
if (x < 0) throw new ArgumentException("Less negativity please!", nameof(x));
@@ -124,20 +126,20 @@ public int Fibonacci(int x)
124126

125127
#### Digit Separator inside numbers literals
126128

127-
```cs
129+
```csharp
128130
var d = 123_456;
129131
var x = 0xAB_CD_EF;
130132
```
131133

132134
#### Binary Literals
133135

134-
```cs
136+
```csharp
135137
var b = 0b1010_1011_1100_1101_1110_1111;
136138
```
137139

138140
### Ref Returns and Locals
139141

140-
```cs
142+
```csharp
141143
public ref int Find(int number, int[] numbers)
142144
{
143145
for (int i = 0; i < numbers.Length; i++)
@@ -160,7 +162,7 @@ WriteLine(array[4]); // prints 9
160162

161163
C# 7.0 adds accessors, constructors and finalizers to the list of things that can have expression bodies:
162164

163-
```cs
165+
```csharp
164166
class Person
165167
{
166168
private static ConcurrentDictionary<int, string> names = new ConcurrentDictionary<int, string>();
@@ -178,7 +180,7 @@ class Person
178180

179181
### Throw Expressions
180182

181-
```cs
183+
```csharp
182184
class Person
183185
{
184186
public string Name { get; }

0 commit comments

Comments
 (0)