Skip to main content

C# - Using Named Tuples As 'Object On The Fly'

My photo
Introduction
As the date of today, we have seen various evolution of C# in many ways, In this article we are going to discuss on the new features release in C#7.0 called Named Tuples, Throughout our experience in making use of the normal tuples, we usually instantiate an instance of a tuple and then we may get the assigned values with something like 'Item1' , 'Item2'. Now, they have come up with some 'sugar' by enabling us to assign it a name, I personally think that this features really help us as a developer to understand our code more easily. This somehow portray that C# is becoming more dynamic towards its programming pattern. Note than the title 'Object On The Fly' is somehow a sugar of my own, and not related to any programming technique or any design patterns. But i am pretty sure that we might going to encounter it a lot in the future.

Consideration
The code involved in this article is based on assumptions, It was proposed for the intention to show its functions/features based on the article title. For production/release we might need to tweak it to meet the company policy, best practice and industry standards. The code snippet is free to use anywhere.

Jump Straight Right In
Sometimes, we require a return Type that is unique without its Class declaration, In the example code below, we are trying to make use Named Tuples as a return type of a single method.


From the code above we may see that the return type of the method is slightly different from normal Tuple This might as well can be accomplished using something like this  Tuple<string,string,List<Tuple<string, string>>,DateTime> But now we are using Named Tuple.

Making Use Of It
Assuming we wanted to check if a specific user are allowed to view a specific invoice, we may make use the method above like below
Note that we have used both Named Tuple along with the normal tuple. The new features somehow allow us to treat the return object from the method as a normal object with properties.

The Downside
Assuming we wanted to serialize the return object across our system boundaries, As the date of today, we are still unable to do so. For example returning the data as Json. We will still treat it as a normal Tuple. For Example
When we called the API, it will return like below.

Current Discussion
There is a lot of active discussion on this right now, Here is one of them taken from GitHub

Conclusion
From the article, we may conclude that using Named Tuple somehow will help us in code maintenance by improving code readable. To understand more, visit Microsoft official website Here. As for the date of this writing, there is still no support yet to serialize it throughout our system boundaries.

Published on : 7-Jan-2018
Ref no : DDN-WPUB-000215

About Author

My photo
Wan Mohd Adzha MCPD,MCSD,MCSE
I am passionate about technology and of course love Durians. Certified by Microsoft as MCP Since 2011. Blogging from Malaysia

Comments