-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathall_csharp_examples.cs
More file actions
59 lines (50 loc) · 932 Bytes
/
Copy pathall_csharp_examples.cs
File metadata and controls
59 lines (50 loc) · 932 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//functions
int[] numbers = new[] { 1, 2, 3, 4, 5, 6 };
foreach (var n in numbers)
{
Console.Write(n + ",");
}
//yield use
void Main()
{
var nums = FilterNumbers(n=> n % 2 == 0);
foreach (var n in nums)
{
Console.WriteLine(n);
}
}
IEnumerable<int> FilterNumbers(Predicate<int> predicate)
{
for (int i = 0; i < 100; i++)
{
if(predicate(i))
yield return i;
}
}
#lambda
Func<int, int, int> add = (int a, int b) => a + b;
var c = add(5, 6);
Console.Write(c);
# Custom Attributes
void Main()
{
var attrs = Attribute.GetCustomAttributes(typeof(SampleClass));
var author = (Author)attrs[0];
Console.Write(author.version);
}
[AttributeUsage(AttributeTargets.Class |
AttributeTargets.Struct)]
public class Author : System.Attribute
{
private string name;
public double version;
public Author(string name)
{
this.name = name;
version = 1.0;
}
}
[Author("Janier", version = 1.1)]
class SampleClass
{
}