-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04_DeclarationExpressions.cs
75 lines (60 loc) · 1.53 KB
/
04_DeclarationExpressions.cs
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System;
using System.Linq;
//
// All exmaples are taken from MS Roslyn Codeplex page
// https://www.codeplex.com/Download?ProjectName=roslyn&DownloadId=824694
//
namespace CSharp6Samples_DeclarationExpressions
{
#region CSharp 5
public class DeclarationExpressionsV5
{
public void Method ()
{
// explicit declaration in extra line required
int i;
if (int.TryParse ("178965", out i))
{
int doubledI = i + i;
Console.WriteLine ("{0} doubled is {1}", i, doubledI);
}
}
}
#endregion
#region CSharp 6
public class DeclarationExpressionsV6
{
public void Method ()
{
if (int.TryParse ("178965", out int i))
{
int doubledI = i + i;
Console.WriteLine ("{0} doubled is {1}", i, doubledI);
}
// automatic type inferring is possible
if (GetCoordinates (out var x, out var y))
{
}
// x is no longer in scope here
Console.WriteLine ("Result: {0}", (int x = GetValue()) * x);
object o = null;
if ((string s = o as string) != null)
{
Console.WriteLine (s);
}
// also useable with linq
string[] source = null;
var result = from s in source
select int.TryParse(s, out int i) ? i : -1;
}
private bool GetCoordinates (out int var1, out int var2)
{
throw new NotImplementedException ();
}
private int GetValue ()
{
throw new NotImplementedException ();
}
}
#endregion
}