This document contains slides from presentations on new features in C# 6.0 and beyond. It discusses the speaker and event details for a DotNetLombardia meetup in Milan, Italy focused on C# and .NET. The slides cover topics like auto-properties, read-only structs and fields, exception filters, null-conditional operators, string interpolation, and nameof.
2. www.slideshare.net/marco.parenzan
www.github.com/marcoparenzan
marco [dot] parenzan [at] 1nn0va [dot] it
www.1nnova.it
@marco_parenzan
Formazione ,Divulgazione e Consulenza con 1nn0va
Microsoft MVP 2015 for Microsoft Azure
Cloud Architect, NET developer
Loves Functional Programming, Html5 Game Programming and Internet of Things AZURE
COMMUNITY
BOOTCAMP 2015
IoT Day - 08/05/2015
@1nn0va
#microservicesconf2015
9 Maggio 2015
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. var code = File.ReadAllText("Code.cs");
var tree = SyntaxFactory.ParseSyntaxTree(code);
60. var tokens = node.ChildTokens();
var token = tokens.First();
token.Value;
61.
62. var options = new
CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary);
var reference = new
MetadataFileReference(typeof(object).Assembly.Location);
var compilation = CSharpCompilation.Create("Test")
.WithOptions(options)
.AddSyntaxTrees(tree)
.AddReferences(reference);
63.
64. var comp = CreateCompilation(tree, options, reference);
var model = comp.GetSemanticModel(tree);
var localDeclarationNodes = tree.GetRoot()
.DescendantNodes()
.OfType<LocalDeclarationStatementSyntax>();
foreach (var node in localDeclarationNodes)
{
var info = model.GetTypeInfo(node.Declaration.Type);
Console.WriteLine("{0} {1}", info.Type, node.Declaration);
}
65. using (var memory = new MemoryStream())
{
compilation.Emit(memory);
var assembly = Assembly.Load(memory.GetBuffer());
var type = assembly.GetType("NameOfType");
var instance = Activator.CreateInstance(type);
}
75. class Person
{
private readonly string <Name>k__BackingField = "Filip";
public string Name
{
get
{
return this.<Name>k__BackingField;
}
}
}
76.
77. struct Point
{
public int X { get; }
public int Y { get; }
}
Read Only!
public Point()
{
X = 100;
Y = 100;
}
78. Read Only!
struct Point
{
private readonly int <X>k__BackingField;
private readonly int <Y>k__BackingField;
public int X
{
get
{
return this.<X>k__BackingField;
}
}
public int Y
{
get
{
return this.<Y>k__BackingField;
}
}
public Point()
{
this.<X>k__BackingField = 100;
this.<Y>k__BackingField = 100;
}
}
79. Read Only!
struct Point
{
public int X { get; }
public int Y { get; }
public Point(int x, int y)
{
X = x;
Y = y;
}
public Point() : this(100, 100)
{
}
}
85. var people = new Dictionary<string, Person>
{
["Filip"] = new Person()
};
var answers = new Dictionary<int, string>
{
[42] = "the answer to life the universe and everything"
};
91. class Person
{
public string Name { get; set; }
public Address Address { get; set; }
}
Console.WriteLine(filip.Address.AddressLine1);
var filip = new Person
{
Name = "Filip"
};
class Address
{
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
}
92. class Person
{
public string Name { get; set; }
public Address Address { get; set; }
}
Console.WriteLine(filip.Address.AddressLine1);
var filip = new Person
{
Name = "Filip"
};
class Address
{
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
}
93. class Person
{
public string Name { get; set; }
public Address Address { get; set; }
}
Console.WriteLine(filip.Address.AddressLine1);Console.WriteLine(filip.Address == null ? "No Address" : filip.Address.AddressLine1);
var filip = new Person
{
Name = "Filip"
};
class Address
{
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
}
94. class Person
{
public string Name { get; set; }
public Address Address { get; set; }
}
Console.WriteLine(filip.Address.AddressLine1);Console.WriteLine(filip.Address == null ? "No Address" : filip.Address.AddressLine1);
Console.WriteLine(filip?.Address?.AddressLine1 ?? "No Address");
var filip = new Person
{
Name = "Filip"
};
class Address
{
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
}
95. var people = new[]
{
new Person(),
null
};
WriteLine(people[0]?.Name);
WriteLine(people[1]?.Name);
98. class Rectangle
{
public double Width { get; set; }
public double Height { get; set; }
public double Area => Width * Height;
}
99. class Rectangle
{
public double Width { get; set; }
public double Height { get; set; }
public override string ToString() =>
$"My Width is {Width} and my Height is {Height}";
}
102. public override string ToString() =>
$”My Width is {Width} and my Height is {Height}";
public override string ToString()
{
object[] args = new object[] { this.Width, this.Height };
return string.Format("My Width is {0} and my Height is {1}", args);
}
103. int age = 28;
var result = $"Hello there, I'm {age : D5} years old!";
WriteLine(result);
int num = 28;
object[] objArray1 = new object[] { num };
Console.WriteLine(string.Format("Hello there, I'm {0:D5} years old!", objArray1));
106. public double CalculateArea(int width, int height)
{
if (width <= 0)
{
throw new ArgumentException($"Parameter {nameof(width)} cannot be less than 0");
}
return width * height;
}
Editor's Notes
(c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
(c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
(c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*