InfoQ Homepage News Build 2018: The Future of C#

# Build 2018: The Future of C#

Number one on the list of future C# features is Nullable Reference Types. We first covered this last year, but to briefly recap: all reference variables, parameters, and fields are going to be non-nullable by default. Then, like value types, if you want anything to be nullable you must explicitly indicate that by appending a question mark (?) to the type name.

This will be an optional feature and the current thought is nullable reference types will be turned off for existing projects that are being upgraded to C# 8. For new projects, Microsoft is inclined to turn the feature on by default.

The warnings will be further subdivided into potential errors and merely cosmetic warnings. For example, if p.MiddleName is a string?, then this line would be a cosmetic warning:

string middleName = p.MiddleName;

Since the danger doesn’t occur until the value is dereferenced, this assignment to a local variable isn’t really a problem. So, you can disable the warning on legacy code to cut down on the number of false positives.

Likewise, libraries predating this feature won’t trigger warnings because the compiler doesn’t know whether or not a given parameter should be treated as nullable.

A preview of nullable reference types is available on GitHub.

Switch Expressions

Switch blocks are commonly used to simply return a single value. In this common scenario, the syntax is quite verbose compared to what is being done. Consider this example using pattern matching:

static string M(Person person)
{
switch (person)
{
case Professor p:
return $"Dr. {p.LastName}"; case Studen s: return$"{s.FirstName} {s.LastName} ({s.Level})";
default:
return $"{person.FirstName} {person.LastName}"; } }  Under the new proposal, the repetitive case and return statements can be eliminated. This results in this newer, more compact syntax. static string M(Person person) { return person switch { Professor p =>$"Dr. {p.LastName}",
Student s => $"{s.FirstName} {s.LastName} ({s.Level})", _ =>$"{person.FirstName} {person.LastName}"
};
}


The exact syntax is still under debate. For example, it hasn’t been decided if this feature will use => or : to separate the pattern expression from the return value.

Property Pattern Matching

Currently it is possible to perform property level pattern matching via the when clause.

case Person p when p.LastName == "Cambell" : return $"{p.FirstName} is not enrolled"; With “property patterns” this is reduced to case Person { LastName: "Cambell"} p : return$"{p.FirstName} is not enrolled";

A small gain, but it does make the code clearer and removes a redundant instance of the variable name.

Extracting Properties from Patterns

Taking it a step further, you can also declare variables in the pattern.

case Person { LastName: "Cambell", FirstName: var fn} p : return $"{fn} is not enrolled"; Deconstructors in Patterns A deconstructor is used to break down an object into its constituent parts. It is primarily used for multiple assignment from a tuple. With C# 7.3, you can also use deconstruction with pattern matching. In this next example, the Person class deconstructs to {FirstName, MiddleName, LastName}. Since we are not using MiddleName, an underscore is used as a placeholder for the skipped property. case Person ( var fn, _, "Cambell" ) p : return$"{fn} is not enrolled";

Recursive Patterns

For our next pattern, let us say the class Student deconstructs into {FirstName, MiddleName, LastName, Professor}. We can decompose both the Student object and its child Professor object at the same time.

};

• ##### WTF were they thinking.

Your message is awaiting moderation. Thank you for participating in the discussion.

Nullable references? Really? WTF? The thing that made nullable structs usesful is that it gave us the ability to assign null to struct based attributes and parameters. References already are nullable. This isn't adding any new capabilities to the language; this is making us jump through extra hoops just to use something that's already there.

• ##### Re: WTF were they thinking.

Your message is awaiting moderation. Thank you for participating in the discussion.

The new capability is the ability to mark reference typed variables as non-nullable.

• ##### Re: Terrible Switch

Your message is awaiting moderation. Thank you for participating in the discussion.

In my opinion the current switch statement is overly verbose. At the very least they need to eliminate the need for break statements and allow for multiple cases with having to repeat the word "case" over and over again.

• ##### Re: WTF were they thinking.

Your message is awaiting moderation. Thank you for participating in the discussion.

The new capability is the ability to mark reference typed variables as non-nullable.

Having the ability to make a reference type a non-nullable would be a new capability, but they specifically said that they will be non-nullable by default. If they are non-nullable by default, then we are not the ones that are making them non-nullable. You can not make something non-nullable if it's non-nullable to begin with. I now declare your name to be Jonathan Allen. Oh, no, wait, you already are Jonathan Allen.

Sure, there are plenty of cases where a NULL value can get a person in trouble, and inserting a null check where there should never be a null just introduces branching statements that are technically never needed. I wouldn't mind being able to make a reference into a non-nullable, but what they're doing is forcing me to make my references into nullables. Sure, it's only one extra character, but they are still forcing me to do more work than before and calling that an improvement for my benefit. Introducing the 'var' keyword was an improvement, giving us the Parallel library was an improvement, but this is BS.

To make matters worse, they're using a syntax that isn't recognized by most programmers that I know. I've met multiple programmers that have no idea what that question mark behind a struct based variable means, and when they try to search for it, all they get are links to the ternary operator. They are doubling down on something that causes quite a few bugs for experienced people that are coming in from different languages and introducing a change in the language that brakes backwards capability at the same time.

• ##### Re: WTF were they thinking.

Your message is awaiting moderation. Thank you for participating in the discussion.

Microsoft's argument is two-fold

1. The vast majority of variables are intended to not store nulls. So that should be the default.

2. This design makes it consistent with value types.

They did experiment with leaving variables as nullable by default and requiring the use of the bang operator (!) to make them non-nullable.

***

Also note that this is an optional feature. It doesn't break backwards compatibility if you don't turn it on.

by M Whitener /

Your message is awaiting moderation. Thank you for participating in the discussion.

C# will always be a multi-paradigm language. As such, expect many strange and wonderful tricks and syntax enhancements.

Retro-purist complainers here should stick with Java ... or C. Actually I don't remember any purists in the C world, so I guess that won't work for them.

by M Whitener /

Your message is awaiting moderation. Thank you for participating in the discussion.

As purist OO (really, Class Orientation) completes its nauseating arc into obscurity, I hope I live long enough to see the best possible implementation of Traits in C# / or the ascendance of a proper successor language.

But the discussion on this page proves that C# has been its own successor, so far.

• ##### Long live F#

Your message is awaiting moderation. Thank you for participating in the discussion.

All this features are coming from F#. Better to use f# and a functional first language.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Is your profile up-to-date? Please take a moment to review and update.

Note: If updating/changing your email, a validation request will be sent

Company name:
Company role:
Company size:
Country/Zone:
State/Province/Region:
You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.