Non-trailing named arguments in C# 7.2
One small change that comes with C# 7.2 is support for non-trailing named arguments in method calls. This post explains what are non-trailing named arguments, how to use them and how they look after compiling.
- Non-trailing named arguments in C# 7.2
- protected private access modifier in C# 7.2
Source code available! Those who want some basic samples of new features can take my solution from GitHub repository gpeipman/CSharp7. This is (almost) the same solution I’m using for presentations.
Let’s look at simple method that calculates volume of rectangular prism.
public int Volume(int a, int b, int c)
{
return a * b * c;
}
Ńamed arguments before C# 7.2
In previous versions of C# we had two ways how to use named arguments. We can specify all argument names and leave out those with default value or we specify positional arguments first and then named arguments.
v = Volume(a: 3, c: 5, b: 4);
v = Volume(3, b: 4, c: 5);
Positional arguments were not allowed after named ones before.
Non-trailing named arguments
With C# 7.2 it is now allowed to have named arguments also after positional ones. This feature is called non-trailing named arguments. In C# 7.2 we can also write a call like this.
v = Volume(3, b: 4, 5);
There are still some rules to follow and we cannot randomly mix up argument lists. Here is the example of call that is not allowed.
Method call above messes up the order of arguments. For C# compiler we have specified argument c twice – first time when specifying it as a named argument and second time by having non-named argument on position of argument c. This is not allowed and all possible intrepretations for this situation probably end up with hard to read and non-intuitive code.
Named arguments after compiling
Let’s make my favorite trick and build sample code. Before opening it in disassembler I remove pdb file to make sure that disassembler cannot find out anything about language features I used. All calls given above (except the one that doesn’t compile) are shown here. This is what we get after compiling.
internal class Program
{
private static void Main(string[] args)
{
Program.Volume(3, 4, 5);
Program.Volume(3, 4, 5);
Program.Volume(3, 4, 5);
}
public static int Volume(int a, int b, int c)
{
return a * b * c;
}
}
Named arguments – be it trailing or non-trailing – are language feature and it has nothing to do with Common Language Runtime (CLR). They exist only in C# but not in framework itself.
Wrapping up
Support for non-trailing named arguments is new language feature of C# 7.2. It’s small but nice addition that, in some cases, helps us write more readable code. On .NET Framework level it changes nothing, as we saw. C# compiler translates all method calls with named arguments to regular method calls and CLR has no idea how the call was originally written.
Pingback:The Morning Brew - Chris Alcock » The Morning Brew #2589
I fail to understand the usefulness of it. It’s either you use named arguments or you don’t. Why bother mixing up named and positional arguments if you have to have them in the right position anyway?
One case that comes to my mind is method with default values and long argument names. This is the case when non-trailing named arguments can be useful.
I can see it also being useful for bool arguments, to clarify what that value means in that context. What’s clearer:
Foo(true);
Foo(reverse: true);
If there were more arguments following ‘reverse’, older versions of C# would require them to be named as well, even if they were perfectly clear from context. For example:
var frobnicator = new Frobnicator ();
Bar(false, frobnicator);
Bar(reverse: false, frobnicator: frobnicator);
In the new version you can just write:
Bar(reverse: false, frobnicator);
Traditional advice was to replace the bool parameter with an enumeration:
enum Direction
{
Forward,
Reverse
}
Bar(Direction.Reverse, frobnicator);
but that can be a lot of additional code if you have many methods with bool parameters which don’t overlap in meaning. You end up with many enumerations that are used in very few places.
You also get naming problems if, say, Baz needs Left and Right options – you end up calling the enums BarDirection and BazDirection to avoid the name clash, meaning the calls start becoming less clear again.
Pingback:Szumma #111 – 2018 21. hét – ./d/fuel
6qybck
beas96
cv318r
9uov8o
wvl54c
y3dbxt
I don’t normally comment but I gotta tell regards for the post on this amazing one : D.
This amazing posting usually have a good deal of travelers and tourists. Exactly how do you market it? It then provides for a perfect extraordinary rotate around actions. We incuring issue substantial along with sizable to gift home elevators is the main point.
Very nice site, I just came to know of it yesterday evening. Bookmarked this and also stumbled upon it. Thanks a lot,You had some nice ideas in the post, I enjoyed scaning it.
Perfect work you have done, this website is really cool with excellent information.
I must find the potential for to thank anyone for impressive pointers There are at all times appreciated possibilities a web page. We’re anxious about any graduation to do with your college explore along with the uncut groundwork could not have already been conclude without the need of arriving in your web blog. Household . instead , may well be from a assistance other individuals, Keep away from companies enjoy it to further in what I had learned at this point.
Nice to be visiting your blog once more, it has been months for me. Well this article that ive been waited for therefore long. i want this article to finish my assignment within the faculty, and it has same topic together with your article. Thanks, nice share.
Heya i am for the primary time here. I came across this board and I to find It truly useful & it helped me out a lot. I’m hoping to provide something back and help others such as you aided me.
I will right away grasp your rss feed as I can not find your e-mail subscription hyperlink or newsletter service. Do you’ve any? Kindly allow me know so that I may just subscribe. Thanks.
very good post, i certainly enjoy this fabulous website, go on it
i always like Superman and of course the TV Series Smallville, Lana Lang is very pretty too~
I am not real superb with English but I find this very leisurely to understand .
Enjoyed reading through this, very good stuff, thankyou .
Thank you pertaining to sharing that excellent written content on your website. I ran into it on google. I am going to check back again once you publish much more aricles.
As everyone knows though it’s not really about how good the movie was, it’s about if you would watch it a second time?
I am really glad I’ve found this info. Today bloggers publish only about gossips and web and this is actually annoying. A good website with interesting content, that’s what I need. Thanks for keeping this web site, I’ll be visiting it. Do you do newsletters? Cant find it.
Hello just wanted to give you a quick heads up. The words in your article seem to be running off the screen in Opera. I’m not sure if this is a formatting issue or something to do with browser compatibility but I figured I’d post to let you know. The design and style look great though! Hope you get the issue resolved soon. Cheers
I agree with told all above. We can communicate on this theme.
Read carefully the complete short article. There is certainly some definitely insightful data here. thanks. “The soul is the captain and ruler of the life of morals.” by Sallust..
his is the right blog for anybody who desires to find out about this topic. You realize so much its virtually laborious to argue with you (not that I actually would need…HaHa). You definitely put a brand new spin on a topic thats been written about for years. Great stuff, just great!
Whoa! This blog looks just like my old one! It’s on a totally different subject but it has pretty much the same layout and design. Outstanding choice of colors!
I would point out that we website visitors actually are unequivocally lucky to exist in a useful website with so many wonderful people with very helpful concepts.
An interesting discussion may be valued at comment. I’m sure that you should write more on this topic, may possibly not become a taboo subject but generally individuals are not enough to talk on such topics. An additional. Cheers
dxegji
rxprgj
8w6vct
acyrlh
2ajmlb
I’m new to your blog and i really appreciate the nice posts and great layout.:..`’
Some genuinely superb blog posts on this website , regards for contribution.
I got what you mean, appreciate it for posting. Woh I am delighted to find this website through google.
Indeed, most of the CGI effects end up being over-the-top but that is easily overcome with Cruise’s energy in his own stunt driven scenes.
I have read several good stuff here. Definitely worth bookmarking for revisiting. I surprise how much effort you put to create such a wonderful informative web site.
Good info. I just discovered your website and had to let you know that I have actually enjoyed looking through the blog. I have became a subscriber to your blog feed and I hope you will post again soon. I am curious if I should really subscribe to comments RSS feed as well. Any helpful discussions taking place in posts comments?
wy18tz
fru6pz
wia55j
66aw7y
uxim5a
lf6zle
md47ns