csharp6-presentation



csharp6-presentation

0 0


csharp6-presentation

What's new in C# 6.0 presentation

On Github RaYell / csharp6-presentation

What's new in C# 6.0

Created by Lukasz Rajchel

24.11.2014

What's going on in .NET?

  • .NET fully open source

  • Mono support (partial but official)

  • Codename "Roslyn" - .NET compiler written in .NET

  • ASP.NET WebAPI & MVC architecture independent of System.Web & IIS

Work in progress

The features presented here can change before final version is released

Only selected new/updated features are part of this presentation

Syntactic sugar

Primary constructors

Old way

public class Point
{
    public int _x; 
    public int _y; 
    public int _z;

    public Person(int x, int y) 
    { 
        this._x = x; 
        this._y = y;
    }

    public Person(int x, int y, int z) : this(x, y) 
    { 
        this._z = z;
    }
}

Primary constructors

New way

public class Point(int x, int y)
{
    public int _x = x;
    public int _y = y;
    public int _z;

    public Person(int x, int y, int z) : this(x, y) 
    { 
        this._z = z; 
    }
}

Auto property initializers

Old way

public class Reservation
{
    public Reservation(string number)
    {
        this.Number = number;
        this.CreatedDate = DateTime.Now;
    }

    public DateTime CreatedDate { get; private set; }
    public string Number { get; private set; }
    public string User
    { 
        get { return Principal.WindowsPrincipal.Current.Identity.Name; }
    }
}

Auto property initializers

New way
public class Reservation(string number)
{
    public DateTime CreatedDate { get; private set; } = DateTime.Now;
    public string Number { get; private set; } = number;
    public string User { get; } = Principal.WindowsPrincipal.Current.Identity.Name;
}

Using statement for static members

Old way

using System;

Console.WriteLine("...");
Console.ReadLine();
New way
using System.Console;

WriteLine("...");
ReadLine();

Null-conditional operator

Old way

public string GetFieldText(Model model)
{
    if (model == null)
    {
        return null;
    }

    var field = model.GetField();
    if (field == null)
    {
        return null;
    }

    return field.Text;
}
New way
public string GetFieldText(Model model)
{
    return model?.GetField()?.Text;
}

Declaration expressions

& out-type inference

Old way

int parseResult;
if (int.TryParse(parameter, out parseResult))
{
    // ...
}
New way
if (int.TryParse(parameter, out int parseResult))
{
    // ...
}

or

if (int.TryParse(parameter, out var parseResult))
{
    // ...
}

Expression bodied

methods & properties

Old way

public string Text 
{
    get { return string.Format("{0} {1}", this.Process, this.Message); }
}

public override ToString()
{
    return string.Format("{0} {1}", this.FirstName, this.LastName);
}
New way
public string Text => string.Format("{0} {1}", this.Process, this.Message)

public override ToString() => 
    string.Format("{0} {1}", this.FirstName, this.LastName);

New features

Exception filters

Old way

try
{
    DoSomething();
}
catch (Exception ex)
{
    if (ex.Message.Contains("Fatal"))
    {
        Console.WriteLine("Fatal Exception");
    }
    else
    {    
        throw;
    }
}

Exception filters

New way

try
{
    DoSomething();
}
catch (Exception ex) if (ex.Message.Contains("Fatal"))
{
    Console.WriteLine("Fatal Exception");
}

Await in catch/finally blocks

Old way

try
{
    await DoSomething();
}
catch (Exception ex)
{
    await LogService.Error("Exception", ex); // compiler error
    throw;
}
finally
{
    await Logservice.Info("Finished"); // compiler error
}

Await in catch/finally blocks

Old way

ExceptionDispatchInfo capturedException = null;
try
{
    await DoSomething();
}
catch (Exception ex)
{
    capturedException = ExceptionDispatchInfo.Capture(ex);
}

if (capturedException != null)
{
    await LogService.Error("Exception", capturedException.SourceException);
    await LogService.Info("Finished"); // simulates finally
    capturedException.Throw();
}             
await LogService.Info("Finished"); // simulates finally

Await in catch/finally blocks

New way
try
{
    await DoSomething();
}
catch (Exception ex)
{
    await LogService.Error("Exception", ex);
    throw;
}
finally
{
    await Logservice.Info("Finished");
}

nameof expressions

Old way

public void Check(Person person)
{
    if (person == null) 
    {
        throw new ArgumentNullException("person"); 
    }
}

or

public void Check(Person person)
{
    if (person == null) 
    {
        throw new ArgumentNullException(GetVariableName(() => person)); 
    }
}

private static string GetVariableName<T>(Expression<Func<T>> expr)
{
    var body = (MemberExpression)expr.Body;
    return body.Member.Name;
}

nameof expressions

New way
public void Check(Person person)
{
    if (person == null) 
    {
        throw new ArgumentNullException(nameof(person)); 
    }
}

And even more...

Want to learn more?

Thank you

rayell.github.io/csharp6-presentation

Created with reveal.js and Brackets