MVP Showcase 2015 - C#

Preview:

Citation preview

Independent Experts – Real World AnswersIndependent Experts – Real World Answers

Microsoft MVP Showcase22 Abril 2015

C#Paulo Morgado

.NET / C#

Independent Experts – Real World AnswersIndependent Experts – Real World Answers

Social ResponsabilityMicrosoft MVP Showcase

22 Abril 2015

“OS DOUTORES PALHAÇOS

LEVAM ALEGRIA ÀS CRIANÇAS

HOSPITALIZADAS EM PORTUGAL”

http://www.narizvermelho.pt/

Independent Experts – Real World Answers

Paulo Morgado.NET / C#

PauloMorgado

Independent Experts – Real World AnswersIndependent Experts – Real World Answers

Independent Experts – Real World Answers

Independent Experts – Real World AnswersIndependent Experts – Real World Answers

Independent Experts – Real World Answers

Independent Experts – Real World AnswersIndependent Experts – Real World Answers

Independent Experts – Real World Answers

Independent Experts – Real World AnswersIndependent Experts – Real World Answers

Independent Experts – Real World Answers

Independent Experts – Real World AnswersIndependent Experts – Real World Answers

Foo(); var task = FooAsync();

From the method signature (how people call it)

void Foo(){

for (int i=0; i<100; i++)Math.Sin(i);

}

From the method implementation (what resources it uses)

async Task FooAsync(){

await client.DownloadAsync();}

Independent Experts – Real World AnswersIndependent Experts – Real World Answers

Is this true for your async methods?

Independent Experts – Real World Answers

Independent Experts – Real World AnswersIndependent Experts – Real World Answers

synchronous block the current thread

asynchronous without spawning new threads

Independent Experts – Real World AnswersIndependent Experts – Real World Answers

void Foo() { FooAsync().Wait(); } -- will deadlock!!!

Independent Experts – Real World Answers

Independent Experts – Real World AnswersIndependent Experts – Real World Answers

Independent Experts – Real World Answers

Independent Experts – Real World AnswersIndependent Experts – Real World Answers

Independent Experts – Real World Answers

Independent Experts – Real World AnswersIndependent Experts – Real World Answers

Independent Experts – Real World AnswersIndependent Experts – Real World Answers

Independent Experts – Real World AnswersIndependent Experts – Real World Answers

Independent Experts – Real World Answers

Independent Experts – Real World AnswersIndependent Experts – Real World Answers

Independent Experts – Real World Answers

Independent Experts – Real World AnswersIndependent Experts – Real World Answers

Independent Experts – Real World Answers

Independent Experts – Real World AnswersIndependent Experts – Real World Answers

public class Customer

{

public string First { get; set; } = "Jane";

public string Last { get; set; } = "Doe";

}

Independent Experts – Real World AnswersIndependent Experts – Real World Answers

public class Customer

{

public string Name { get; };

public Customer(string first, string last)

{

Name = first + " " + last;

}

}

public class Customer

{

public string First { get } = "Jane";

public string Last { get; } = "Doe";

}

Independent Experts – Real World AnswersIndependent Experts – Real World Answers

public Point Move(int dx, int dy) => new Point(x + dx, y + dy);

public static Complex operator +(Complex a, Complex b) => a.Add(b);

public static implicit operator string (Person p) => p.First + " " + p.Last;

public void Print() => Console.WriteLine(First + " " + Last);

Independent Experts – Real World AnswersIndependent Experts – Real World Answers

public string Name => First + " " + Last;public Customer this[long id] => store.LookupCustomer(id);

Independent Experts – Real World AnswersIndependent Experts – Real World Answers

using static System.Console;using static System.Math;using static System.DayOfWeek;class Program{

static void Main(){

WriteLine(Sqrt(3 * 3 + 4 * 4));WriteLine(Friday - Monday);

}}

Independent Experts – Real World AnswersIndependent Experts – Real World Answers

using static System.Linq.Enumerable; // The type, not the namespaceclass Program{

static void Main(){

var range = Range(5, 17); // Ok: not extensionvar odd = Where(range, i => i % 2 == 1); // Error, not in scopevar even = range.Where(i => i % 2 == 0); // Ok

}}

Independent Experts – Real World AnswersIndependent Experts – Real World Answers

int? length = customers?.Length; // null if customers is nullCustomer first = customers?[0]; // null if customers is null

int length = customers?.Length ?? 0; // 0 if customers is null

int? first = customers?[0].Orders?.Count(); // can be chained

PropertyChanged?.Invoke(this, args); // delegate invokation

Independent Experts – Real World AnswersIndependent Experts – Real World Answers

var s = $"{p.Name} is {p.Age} year{{s}} old";

var s = $"{p.Name,20} is {p.Age:D3} year{{s}} old";

var s = $"{p.Name} is {p.Age} year{(p.Age == 1 ? "" : "s")} old";

Independent Experts – Real World AnswersIndependent Experts – Real World Answers

if (x == null) throw new ArgumentNullException(nameof(x));

WriteLine(nameof(person.Address.ZipCode)); // prints "ZipCode"

Independent Experts – Real World AnswersIndependent Experts – Real World Answers

var numbers = new Dictionary<int, string>{

[7] = "seven",[9] = "nine",[13] = "thirteen"

};

Independent Experts – Real World AnswersIndependent Experts – Real World Answers

try { … }catch (SqlException e) when (myfilter(e)){

…}

Independent Experts – Real World AnswersIndependent Experts – Real World Answers

Resource res = null;try{

res = await Resource.OpenAsync(…); // You could do this.…

}catch (ResourceException e){

await Resource.LogAsync(res, e); // Now you can do this …}finally{

await res?.CloseAsync(); // … and this.}

Independent Experts – Real World AnswersIndependent Experts – Real World Answers

Independent Experts – Real World AnswersIndependent Experts – Real World Answers

Independent Experts – Real World Answers

Independent Experts – Real World AnswersIndependent Experts – Real World Answers

Independent Experts – Real World AnswersIndependent Experts – Real World Answers

CompilationUnit

ClassDeclaration

MethodDeclaration

class C{

void M(){}

}// C▫

ParameterList Block

var tree = CSharpSyntaxTree.ParseText("...");

Independent Experts – Real World AnswersIndependent Experts – Real World Answers

CompilationUnit

ClassDeclaration

class C { MethodDeclaration }

EOF

void M ParameterList Block

( ) { }

class C{

void M(){}

}// C▫

Independent Experts – Real World AnswersIndependent Experts – Real World Answers

class∙C{∙∙∙∙void∙M()∙∙∙∙{∙∙∙∙}}// C▫

CompilationUnit

ClassDeclaration

class C { MethodDeclaration }

EOF

void M ParameterList Block

( )

{ }

SP EOL EOL // C

SPx4 SP

EOL

EOL

EOLSPx4 EOL SPx4

Independent Experts – Real World Answers

Independent Experts – Real World AnswersIndependent Experts – Real World Answers

http://blogs.msdn.com/b/lucian/archive/2013/11/23/talk-mvp-summit-async-best-practices.aspx

http://channel9.msdn.com/Series/Three-Essential-Tips-for-Async/Three-Essential-Tips-For-Async-Introduction

http://curah.microsoft.com/45553/asyncawait-general

http://curah.microsoft.com/44400/async-and-aspnet

Independent Experts – Real World AnswersIndependent Experts – Real World Answers

https://github.com/dotnet/roslyn/wiki/New-Language-Features-in-C%23-6

Independent Experts – Real World AnswersIndependent Experts – Real World Answers

https://github.com/roslyn

https://roslyn.codeplex.com/

https://github.com/code-cracker/

https://github.com/dotnetAnalyzers/

https://github.com/icsharpcode/NRefactory/tree/roslyn

Independent Experts – Real World AnswersIndependent Experts – Real World Answers

CommunitiesMicrosoft MVP Showcase

22 Abril 2015

Independent Experts – Real World AnswersIndependent Experts – Real World Answers

We <3 love our sponsors !Microsoft MVP Showcase

22 Abril 2015

Independent Experts – Real World AnswersIndependent Experts – Real World Answers

Recommended