What's new in C# 7... and 8!

Who Am I?

Marcin Zajkowski

Umbraco Developer, Trainer and Certified Master @ The Cogworks

Team, Technology & Knowledge Wizard (CKO, CIO) / PedagoGEEK @ WOW School

Me @ udfnd.pl

@zajkowskimarcin

@zajkowskimarcin

Let's start with some questions...

Question 1/64

Who is using Visual Studio 2017?

Question 2/64

Who is using C# 7 features?

If it's there - use it.

If you can't use latest C# at work, do it at least at home! It's just better :)

public class GoldenThough1 : ITweetable {
}

C# history in a nutshell

  • C# 3 - LINQ FTW!
  • C# 4 - Dynamics FTW!
  • C# 5 - Async FTW!
  • C# 6 - ???
  • C# 7 - ???
  • C# 8 - ???

C# FTW!

C# 7.1

14th August 2017
Visual Studio 2017 Update 3 (version 15.3)

https://github.com/dotnet/csharplang

Contribute!

Not only should (programming) languages evolve, but also tools to work with them.

public class GoldenThough2 : ITweetable {
}

...and VS is changing!

(and not only getting closer to ReSharper ;))

C# 7.x with Umbraco?

(or any other, existing .NET app)

Yes, we can!

Update / Reinstall NuGet pkgs

  • Microsoft.CodeAnalysis.Analyzers
  • Microsoft.CodeAnalysis.Common
  • Microsoft.CodeAnalysis.CSharp
  • Microsoft.CodeDom.Providers.DotNetCompilerPlatform
install-package System.ValueTuple

(only for .NET Framework < 4.7)

Demo?

C# 8!

Default Interface Members

public interface ICanHasDefaultMembers
{
    void FireDev();

    void FireAllDevs()
    {
        foreach (var dev in theCogworks)
        {
            FireDev();
        }
    }
}

public class MySweetClassWhichIDontWantToModify : ICanHasDefaultMembers
{
    public void FireDev()
    {
        // You know what to do :>
    }
}
public interface ICanHasDefaultMembers
{
    void FireDev();

    void FireAllDevs()
    {
        foreach (var dev in theCogworks)
        {
            FireDev();
        }
    }
}

public class MySweetClassWhichIDontWantToModify : ICanHasDefaultMembers
{
    public void FireDev()
    {
        // You know what to do :>
    }

    private void FireAllDevs() 
    {
        // My private member
    }
}
MySweetClassWhichIDontWantToModify myClass = new MySweetClassWhichIDontWantToModify();
myClass.FireAllDevs(); // Doesn't work because FireAllDevs() is private

ICanHasDefaultMembers myClassOnSteroids = (ICanHasDefaultMembers)myClass;
myClassOnSteroids.FireAllDevs(); // It works!

Nullable
(non-nullable / null) Reference Types

int i = 0; // I can't be null
int? i = null; // I can be null!
string s;
string? t;

t = null;
int i = t.Length; // Warning

if(t != null) {
    int j = t.Length; // No warning - smart compiler!
}

if(!t.IsNullOrWhiteSpace()) {
    int l = t.Length; // Warning!
    int m = t!.Length; // I'm smarter than compiler, and DAMNIT, I don't have null here for sure! :>
}
string s; // Should never be null
string? s1; // Can be null

s?.Length; // Check this one for null, please!
s!.Length; // Don't check this, I'm smarter than you :)

Async Streams

async Task<bool> ThisIsWellKnownAndUsedTaskAsync(){
    return true;
}

await ThisIsWellKnownAndUsedTaskAsync();

// ---------------------------------------------------

IAsyncEnumerable<T> willItBlowOrNotHmmmWeWillSee = ...

foreach await(var bang in willItBlowOrNotHmmmWeWillSee)
{
    // Thread is not blocked and item is retrieved
    // Move next when data is here!
}

Pull model other than push model in IObservable for example

Extension Everything

public class SomeClassWhichNeedsToBeExtended
{

}

public static class SomeClassWhichNeedsToBeExtendedExtensions
{
    public static T MethodName<T>(this SomeClassWhichNeedsToBeExtended instance)
    {
        return default(T);
    }
}

What we're doing now...

extension MyClassExtension extends ClassToBeExtended
{
    public int AddAnotherHead()
    {
        this.Heads++; // it call the current instance of MyClassExtension
    }

    public int Heads()
    {
        get { ... }
    }
}

// --------------------------------------------------------------------

extension SomeComplexExtension extends MyClass : IMyInterface
{
    ...
}

Where to look for more

So.. you want to win a (dev)date or JetBrains licence? :)

(dev)date

Name 3 methods or variables used in my demos today!

JetBrains Licence

What's the due date of C# 8.0 release (placed in the Github roadmap)?

Tweet what's Tweetable ;)

...and just use latest C#!

public class GoldenThough3 : ITweetable {
}

Thank You! Questions?

@zajkowskimarcin  /  marcin.zajkowski@thecogworks.com

What's new in C# 7... and 8!

By Marcin Zajkowski

What's new in C# 7... and 8!

Some developers haven't switched their manners to using the new C# 6 features yet and now we have version 7 and 8 on the map! What’s new in the each version? Should we be scared of the coming changes? Should we refactor current solutions or use the latest features in the next projects? For those and other questions we will answer during this talk..

  • 3,454