Lesser known features of C# 7

Sign in to queue

Description

Did you know that there have been multiple minor releases of C# since 7.0? Many of these features make it much easier for developers to write more performant code by minimizing value type copying.

Our good friend and C# language designer, Mads Torgersen (@MadsTorgersen), joins this episode of On .NET to talk to us about some of these lesser known features that are available for developers to try out in their C# 7.x projects.

  • [01:20] - Motivations for the minor releases of C#
  • [02:31] - Who do the minor releases target?
  • [03:48] -  ref locals and returns (C# 7.0)
  • [10:26] - ref readonly and in parameters  (C# 7.2)
  • [13:46] -  How can these features be used with immutable types?
  • [18:42] - How to opt into C# 7.2 features? 

 

Learn more about some of the other new features of C# 7 through the links below

 

Tag:

.NET

Embed

Download

The Discussion

  • User profile image
    davewill

    Mads,

    What does this mean for object lifetimes and the garbage collector now that references to internal memory (rather than copies of data) can be passed outside a scope?

  • User profile image
    Mads​Torgersen

    @davewill: We have safety rules in the language, so that things on the stack won't escape their scope. For instance, a ref-returning method has a notion of "safe to return": it will only return refs that either a) came from the caller as parameters, or b) live in the heap. Meanwhile, the caller knows that the ref returned to them is only safe to return if all refs passed in as arguments were safe to return.

    For the heap (that is, objects and structs within them), refs to internal structs are already tracked by the GC, and these features don't change anything about that.

  • User profile image
    nando

    Mads, can we check also the Span sample you removed from the code?

  • User profile image
    Ken

    I still get a NullReferenceException when trying to mutate i1 from "ref this int i1" if it is called as an extension but not if called explicitly.

    Given: static ref int OrMaybe(ref this int i1, ref int i2) { ... }

    ref int z = ref OrMaybe(ref x, ref y); // Works as expected
    ref int zz = ref x.OrMaybe(ref y); //NullReferenceException where i1++ in OrMaybe.

  • User profile image
    TimJones

    I love the hoops that @MadsTorgersen jumps through at 5:40 to avoid saying the word "pointer" - well played, sir.

  • User profile image
    VictorGorban

    I want you at our C# university lectures)))

  • User profile image
    eduquint

    I have the latest VS 2017 version, 15.6.2, and I cannot enable the C# 7.2 features.

    I changed the Project/build/Advanced/Language version to C# 7.2 and in the project properties the C# Language Level is equal to C# 7.2.

    But, when I try to use the new features, such as in the following code line:

    ReadOnlySpan<char> worldSpan = str.AsReadOnlySpan().Slice(start: 7, length: 5);

    The ReadOnlySpan<char> and the .AsReadOnlySpan().Slice are flagged as errors and there is no suggestion to correct it.


    How should I do to have these neat new features enabled?

    Thanks,

    Eduardo Quintana

     

     

  • User profile image
    VeraRind

    I tried to use the new feature of an extension method on ref variables:

    public static void TryDisposeAndNull<T>(ref this T instance)
                where T : class
    {
        (instance as IDisposable)?.Dispose();
        instance = null;
    }

    Currently we're using the code without this and the code looks like a normal static method invoke:

    DisposeHelper.TryDisposeAndNull(ref this._member)

    It would be nice to use this cool new language feature and change the code to:

    ref this._member.TryDisposeAndNull()

    But the compiler tells me that a ref extension method must be a value type or a generic type contraint to struct.

    Why? I'm pretty sure that this is by design but I couldn't find any sources or further information for that design desicion. Please explain why it is forbidden to use ref extension methods on reference types.

  • User profile image
    Whirly101

    Can we have a complete documented language reference like the one that used to be part of the VS installation? It stopped at C#5. I appreciate language versions not tied to the VS release cycle, and that there is a *draft* of C#6, but could you not maintain an up to date language reference on the git hub repo to the same quality of the C# 5 version? Dropping the maintenance of a high-quality precise and thorough language reference document is a disconcerting fall in standards. The C#7 features are terrific; I want to be able to read about them in a single reference document and not have to trawl through lots of web pages to piece them together.

Add Your 2 Cents