c# is the future

48
C# Is The Future Filip Ekberg

Upload: filip-ekberg

Post on 07-Aug-2015

680 views

Category:

Technology


1 download

TRANSCRIPT

C# Is The Future

Filip Ekberg

I’m Filip Ekberg

Readify

@fekberg

Author. Blogger. Speaker. .NET MVP. Xamarin MVP. Geek.

Senior Software Engineer @

C# through time

@fekberg

The evolution of C#

@fekberg

C# 2.0Generics

Partial types

Anonymous methods

Iterators

Nullable types

Getter/Setter separate accessibility

Static classes

And more..

@fekberg

Implicitly typed local variables

Object and collection initializers

Auto-properties

Anonymous types

Extension methods

Query expressions

Lambda expressions

Expression trees

And more..

Dynamic binding

Named and optional parameters

Generic co- and contravariance

And more..

C# 3.0

C# 4.0

Asynchronous methods

Caller info attributes

C# 5.0

The Future of C#

@fekberg

Starting fresh with the new compiler• .NET Compiler Platform (codename Roslyn)• Microsoft decides to re-write the compiler• Compiler written in C#• Easier to extend and maintain• Trusting the compiler to solve complexity for us• Let us be less verbose (syntactic sugar)• Open Source!

@fekberg

C# 6.0

@fekberg

Using Statements for Static Members

@fekberg

Using Statements for Static Members

@fekberg

class Program{ static void Main(string[] args) { var angle = 90d; Console.WriteLine(Math.Sin(angle)); }}

Using Statements for Static Members

@fekberg

using static System.Console;using static System.Math;

class Program{ static void Main(string[] args) { var angle = 90d; WriteLine(Sin(angle)); }}

Using Statements for Static Members

@fekberg

using static System.Console;using static System.Linq.Enumerable;

class Program{ static void Main(string[] args) { foreach (var i in Range(0, 10)) { WriteLine(i); } }}

Auto-Property Initializers

@fekberg

Auto-Property Initializers

@fekberg

class Person{ public string Name { get; set; }}

class Person{ public string Name { get; } = "Anonymous";}

Auto-Property Initializers

@fekberg

class Person{ public string Name { get; }

public Person() { Name = "Filip"; }}

Auto-Property Initializers

@fekberg

class Person{ private readonly string <Name>k__BackingField = "Filip";

public string Name { get { return this.<Name>k__BackingField; } }}

Await inside Catch & Finally blocks

@fekberg

Await inside Catch & Finally block

@fekberg

public async Task DownloadAsync(){

}

try { } catch { await Task.Delay(2000); } finally { await Task.Delay(2000); }

Null-conditional operators

@fekberg

Null-conditional operators

@fekberg

class Person{ public string Name { get; set; } public Address Address { get; set; }}

Console.WriteLine(filip.Address == null ? "No Address" : filip.Address.AddressLine1);

Console.WriteLine(filip?.Address?.AddressLine1 ?? "No Address");

var filip = new Person{ Name = "Filip"};

Null-conditional operators

@fekberg

var people = new[]{ new Person(), null};

WriteLine(people[0]?.Name);

WriteLine(people[1]?.Name);

Null-conditional operators

@fekberg

Person[] people = null;

WriteLine(people?[0]?.Name);

Person[] people = null;Console.WriteLine(

(people != null) ? ((people[0] == null) ? null :

people[0].Name) : null

);

String interpolation

@fekberg

String interpolation

@fekberg

$"My Width is {Width} and my Height is {Height}";

string.Format("My Width is {0} and my Height is {1}", Width, Height);

String interpolation

@fekberg

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));

Dictionary Intializers

@fekberg

Dictionary Intializers

@fekberg

var people = new Dictionary<string, Person>{ ["Filip"] = new Person()};

var answers = new Dictionary<int, string>{ [42] = "the answer to life the universe and everything"};

Expression-bodied members

@fekberg

Expression-bodied members

@fekberg

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}";

}

nameof operator

@fekberg

nameof operator

@fekberg

static void Main(string[] args){ WriteLine($"Parameter name is: {nameof(args)}");}

Exception filters

@fekberg

Exception Filters

@fekberg

try{ throw new CustomException { Severity = 100 };}catch (CustomException ex) when (ex.Severity > 50){ Console.WriteLine("*BING BING* WARNING *BING BING*");}catch (CustomException ex){ Console.WriteLine("Whooops!");}

C# 6.0Using Statements for Static Members

Auto-Property Initializers

Await inside Catch & Finally blocks

Null-conditional operators

String interpolation

Dictionary Initializers

Expression-bodied members

Exception Filters

nameof operator

And more..

@fekberg

C# 2.0Generics

Partial types

Anonymous methods

Iterators

Nullable types

Getter/Setter separate accessibility

Static classes

And more..

Implicitly typed local variables

Object and collection initializers

Auto-properties

Anonymous types

Extension methods

Query expressions

Lambda expressions

Expression trees

And more..

Dynamic binding

Named and optional parameters

Generic co- and contravariance

And more..

C# 3.0

C# 4.0Asynchronous methods

Caller info attributes

C# 5.0

Features withdrawn

@fekberg

Withdrawn features 1 / 2

@fekberg

0b00001000

0xFF_00_FA_AF

var client = new WebClient{ DownloadFileCompleted += DownloadFileCompletedHandler};

var y = (var x = Foo(); Write(x); x * x);[field: NonSerialized]public int Age { get; set; }

Binary literals and Digit separators Event initializers

Field targets on auto-properties Semicolon operator

Withdrawn features 2 / 2

@fekberg

class Person(string name, int age){ private string _name = name; private int _age = age;}

public void CalculateAgeBasedOn(int birthYear, out int age){ age = DateTime.Now.Year - birthYear;}

CalculateAgeBasedOn(1987, out var age);

int Avg(params IEnumerable<int> numbers)

Primary Constructors Using params with IEnumerable

Declaration Expressions

Why care about withdrawn features?

@fekberg

• Indicates the evolution and on-going work• Shows there are no promises until it is RTM• Withdrawn does not mean “Will never come to C#”

C# 7.0?

@fekberg

Tuples (Multiple return types)

@fekberg

public (int sum, int count) Calculate(IEnumerable<int> numbers){ var total = numbers.Sum(); return (total, numbers.Count());}

https://github.com/dotnet/roslyn/issues/1207

var result = Calculate(Enumerable.Range(0, 100));

WriteLine($"Total is {result.sum}, with {result.count} numbers.");

• Requires a record type• Record types uses primary constructors (see withdrawn C# 6.0 features)

Pattern Matching and Record Types

@fekberg

https://github.com/dotnet/roslyn/issues/206

public record class Point(int X, int Y);

if (expr is Point point) { // Use point here}

More potential C# 7.0 features

@fekberg

• Nullability tracking• Asynchronous streams• Declaration expressions (see withdrawn C# 6.0 features)

• Binary literals and Digit separators (see withdrawn C# 6.0

features)

• And more…

Endless potential

• Language feature discussions on Github• Compiler API at your disposal• Write code analysers and refactoring

packages• C# evolves and grows each release

@fekberg

What about…

• F#• Java• PHP• Python• Ruby• etc..

@fekberg

@fekberg

C# is here to stay,it is the future,

time to embrace it!

Questions?

Readify

@fekberg

Thank you,I’m Filip Ekberg

Readify

@fekberg

Author. Blogger. Speaker. .NET MVP. Xamarin MVP. Geek.

Senior Software Engineer @