BT

C# 7 and Beyond with Mads Torgersen

| by Pierre-Luc Maheu Follow 4 Followers on Jun 22, 2016. Estimated reading time: 2 minutes |

Mads Torgersen, program manager of C#, presented the upcoming C# 7 at QCon New York 2016. He also explained briefly the evolution of C# and introduced some features being developed for future versions.

The emergence of the cloud and distributed systems over the last years creates new challenges for developers. As developer needs evolve, languages have to follow. Evolving a multi-purpose language like C# is a matter of balancing different concerns:
- Improving vs staying simple
- Improve existing development vs attract new users
- Embrace new paradigms vs stay true to the OOP spirit of C#

.Net as a whole also experienced significant changes over the past few years:
- Support for Windows, Mac OS and Linux (.Net Core)
- Deploy system
- Compile to native code (.Net Native)
- Open source compilers and public Roslyn APIs
- Multiple editor choices (enabled by OmniSharp and Roslyn)
- Open source framework, editors and tools

C# 7
C# 7 will be shipped in Visual Studio 15, which is currently available as a preview. In order to make new language features available faster, point releases are being considered. This would lead to language features being included in minor releases. The goal is to give early adopters the option of activating individual features as soon as they are completed.

Tuples syntax will be included in C# 7. Returning several values from a method becomes more straightforward:

static (int sum, int count) Method()
{
    return (0, 0)
}
// Calling the method and using the result
var result = Method();
Console.WriteLine($"Sum:{result.sum}.Count: {result.count}.");

Tuples can also be used inside generic types. Tuples are value types, meaning they are allocated on the stack instead of the heap. This can lead to potential performance gains, such as reducing garbage collection overhead in critical code paths.

// async method returning a tuple
static async Task<(int sum, int count)> Method()

// Dictionary using a tuple as a key
var dict = new Dictionary<(string first, string last), person>();

The cut of some pattern matching features has led to rumors that pattern matching was being cancelled altogether. Pattern matching is still in C# 7 and will be enhanced in future versions.

Beyond C# 7
Several features are currently in development for next versions of C#. Additional semantics are being explored for pattern matching to provide a terser way to handle some scenarios:

// Using C# 7 pattern matching
if (O is Point p && p.X == 5) { WriteLine($"Y: {p.Y}")}

// Same scenario with alternative syntax for future versions
if (o is Point X {var x, Y: var y} && x == 5) { WriteLine($"Y: {y}")}

if (o is Point { X : 5, Y: var y}) { WriteLine($"Y: {y}")}

if (o is Point(5, var y)) { WriteLine($"Y: {y}")} 

Nullable reference types implementation is still ongoing, the reason being to raise warnings when the compiler detects incoherent use of null values:

string? n;
string s;

n = null; // OK, nullable
s = null; // warning, shouldn't be null
s = n; // warning

WriteLine(s.Length); // Sure; it's not null
WriteLine(n.Length) // Warning! could be null

Record type, an immutable value type, is another feature being developed. Immutability makes sharing data in a concurrent environment safer, as well as makes programs easier to reason about. A record type provides a terser way to use immutability in C#. A record has default value semantic, meaning out of the box implementation for getters, GetHashCode and equality members.

Rate this Article

Adoption Stage
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

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

Email me replies to any of my messages in this thread

C# 7. Returning several values from a method by Sreejith Nair

Why don't we use struct for multiple return values (for value type) and class instance for return values for reference type. This way the function signature will be clean and can avoid extra boxing/unboxing if method return a reference type as remember of tuple.

Re: C# 7. Returning several values from a method by Pierre-Luc Maheu

I do not see in what scenarios returning a value tuple could lead to extra boxing/unboxing. Could you give an example of this?

Re: C# 7. Returning several values from a method by Sreejith Nair

.NET framework 4.0 defines Tuple as reference type; and in your article you are mentioning the return type of tuple as Value type. This was the context. Please correct me if I am wrong.

Additionally, I have used Tuples in the past (.NET 4.0) to capture multiple return values from a c# method without using the out keyword. Keeping this in mind, Is there anything C# 7.0 does which wasn't possible by C# 4.0 (specifically sending multiple return values using Tuples approach)?

Re: C# 7. Returning several values from a method by Pierre-Luc Maheu

Your description of the context is correct. Did you mean value tuples would get boxed into class tuples? If that's the case, I don't think value tuples and class tuples are meant to be compatible with each other.

Speaking strictly about usage on return values, you can do pretty much the same using either class tuple or value tuple. The one difference being that, with value tuples, we can name tuples elements. So the first item can become "Count" instead of "Item1", for example. The method signature also becomes clearer, going from
Tuple<int,int> Method()
to
(int Count, int Sum) Method()

The one major difference here would be on the performance side. There is a size overhead associated with classes which can be significant when tuples have just a few properties. There are also GC considerations, where class tuples put more pressure on the garbage collector.

Re: C# 7. Returning several values from a method by Sreejith Nair

Thanks and appreciate your briefing.
Now, looking at the Value Type Tuples, it make sense to me to use named values rather than Item 1,2 etc; and contribution towards performance.

I have one last question, What if I wanted to return a reference type tuples
(A aIns,B nIns,C cIns) Method() ( where A,B,C are reference type)
Is this possible ?
If so,
a) is this advisable?
b) how would this approach affect gc collection ?
c) should we avoid static members for such type ?

Re: C# 7. Returning several values from a method by Pierre-Luc Maheu

a) Reference types are managed pointers. Long story short, what would be inside the tuple would be three pointers and not the whole objects. Pointers are small, roughly the size of an int. So using reference types inside value tuples will work the same way as using value types such as int.

b) I'd say there's no need to worry about garbage collection unless you experience performance issues or work on high-scale systems. This is the kind of details that matters at scale, and usually other bottlenecks will get in the way before GC (ORMs, reflection, I/O, etc.)

Now to answer the question itself, it wouldn't have any significant impact on GC. The reference types themselves will behave the same way whether they are inside a value tuple or class tuple.

c) Static members are managed differently since their life cycle is not tied to a specific class instance. So there's no difference regarding tuples.

Thank you for your interest!

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

Email me replies to any of my messages in this thread

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

Email me replies to any of my messages in this thread

6 Discuss

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and don't miss out on content that matters to you

BT