How can i fix the erorr Feature 'null propagating operator' is not available in C# 4 ?

I’m getting error on the line:

Transform = e.Attribute("transform")?.Value.Substring(18, 43),

Feature ‘null propagating operator’ is not available in C# 4. Please use language version 6 or greater.

It’s working fine on csharp but when i’m using the unity3d visual studio i’m getting this error.
This is the part of code:

XDocument document = XDocument.Load(@"C:\Users\myxml\Documents\my.svg");
        XNamespace ns = "http://www.w3.org/2000/svg";

        var list = document.Root.Descendants(ns + "rect").Select(e => new {
            Style = e.Attribute("style").Value.Substring(16,6),
            Transform = e.Attribute("transform")?.Value.Substring(18, 43),
            Width = e.Attribute("width").Value,
            Height = e.Attribute("height").Value,
            X = e.Attribute("x").Value
        });

How does one fix that the feature isn’t in C# 4… Hmm :slight_smile: Not sure you can.
Re-write it so you don’t require that operator is what I’d do, until if/when it’s properly supported in Unity.

3 Likes

Is there any documentation where I can see a) what .NET framework versions are supported b) which C# version is supported?
Can’t find a way to get this information from anywhere…

Unity targets .Net 3.5 with C# version 4 by default (well technically .net 2.0, but has ‘most’ features up to 3.5).

The latest version allows you go into the player settings and target .Net 4.6 with C# version 6 if you’d like. This was experimental for the last year, and I think the newest version of unity has it just now out of experimental (some one correct me if I’m wrong, I currently only run 5.6.3f1 and 2017.4).

To know what is supported by the .Net version, when going through the MSDN for .net just make sure you restrict to the version of .Net you’re attempting to stick with. Though keep in mind that since unity uses mono, and you’re cross platform, this is still limited. The documentation starts here:

As for C# language features, like ‘null propagating operator’, you’ll need to consult the language version history. Here is an MSDN article on it, and you should consult MSDN at large for most of this information as the C# standard is defined by Microsoft and not by Unity:

The general rule is… if the syntax feature was available when the .net version was released, then it’s generally supported. As a result most people don’t really keep track of the C# language version and usually refer to things as to what .net version they’re targeting.

And of course… if your compiler says it’s not supported. Well don’t use that syntactical feature.

If all else fails, write it out in full. The null propagating operator is only syntactic sugar. It doesn’t do anything real.

if (e.Attribute("transform") != null) {
    Transform = e.Attribute("transform").Value.Substring(18, 43);
} else {
    Transform = null;
}

Agreed. The main thing that operator does is make debugging harder by obfuscating what is actually being done.

I think it’s also called the head scratching operator.

1 Like

I’d replace the ?.Value part with GetValueOrDefault()

 Transform = e.Attribute("transform").GetValueOrDefault().Substring(18, 43),
1 Like

That is not true, this is a very popular operator in modern languages, Swift (optionals), JavaScript, Kotlin and now C# 4.
It makes code shorter and easier to read and understand.