Modern C# Part 2 – Accessing via Implicit Index

Until now, if you wanted to access the last item in a list you had to use a slightly cumbersome syntax. C# 13 introduces the “hat” operator (^) where ^1 is the last element in your collection, ^2 is the penultimate member, etc.

Like many things, this is best illustrated with an example. In the following code I create a simple Person class and initialize a list<Person> with three people (persons?)

 internal class Program
 {
    static void Main(string[] args)
    {
       var tester = new Tester();
       tester.Test();
    }
 }

 public class Tester()
 {
    public void Test()
    {
       var john = new Person { Name = "John", Age = 25 };
       var jane = new Person { Name = "Jane", Age = 30 };
       var joe = new Person { Name = "Joe", Age = 35 };
       var people = new List<Person> { john, jane, joe };      
    }
 }

 public class Person()
 {
    public string Name { get; set; }
    public int Age { get; set; }
 }

Straight forward. Now, if I want to access the last person in this list I can write

 var lastPerson = people[^1];
 Console.WriteLine($"Last person is {lastPerson.Name} who is {lastPerson.Age} years old.");

As you can see, this is concise and easy to understand. You read it as “lastPerson is equal to one back from the end of people.” Let’s run it:

Last person is Joe who is 35 years old.

That is all there is to it. Easy to use and somewhat helpful in simplifying your code.

Unknown's avatar

About Jesse Liberty

Jesse Liberty has three decades of experience writing and delivering software projects and is the author of 2 dozen books and a couple dozen online courses. Liberty is a Senior AI Engineer at the University of Pittsburgh Medical Center, and was a Team Lead and Senior Software Engineer for various corporations, a Senior Technical Evangelist for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS. He is a 21 year Microsoft MVP.
This entry was posted in Programming and tagged , . Bookmark the permalink.

1,718 Responses to Modern C# Part 2 – Accessing via Implicit Index

  1. I must get across my admiration for your kind-heartedness giving support to women who require help with this topic. Your real commitment to passing the solution all around became pretty functional and has always permitted men and women much like me to attain their endeavors. Your entire important guidelines can mean a great deal a person like me and even further to my mates. Warm regards; from all of us.

  2. What’s Happening i’m new to this, I stumbled upon this I have found It absolutely useful and it has aided me out loads. I hope to contribute & help other users like its helped me. Great job.

  3. IsmaelNiz's avatar IsmaelNiz says:

    Heya just wanted to give you a quick heads up and let you know a few of the images aren’t loading properly. I’m not sure why but I think its a linking issue. I’ve tried it in two different internet browsers and both show the same outcome.
    https://myelectro.com.ua/ultrazvukovyi-nizh-dlya-retrofitu-yak.html

  4. This ^ operator is a nice touch for making code more readable, especially when working with collections. I like how it cleanly expresses the intent of accessing elements from the end without needing to calculate indices manually. It definitely simplifies what was previously a bit more verbose approach.

  5. I’ll immediately snatch your rss feed as I can not in finding your email subscription link or e-newsletter service. Do you’ve any? Please permit me recognise in order that I may just subscribe. Thanks.

  6. I am not sure where you are getting your information, but good topic. I needs to spend some time learning more or understanding more. Thanks for magnificent information I was looking for this info for my mission.

  7. You have brought up a very superb details , thanks for the post.

  8. alexaslot138's avatar alexaslot138 says:

    There is noticeably a bundle to know about this. I assume you made sure good points in options also.

  9. hokibos898's avatar hokibos898 says:

    You made some clear points there. I looked on the internet for the issue and found most persons will approve with your website.

  10. hokibos898's avatar hokibos898 says:

    I really appreciate this post. I’ve been looking everywhere for this! Thank goodness I found it on Bing. You have made my day! Thx again

  11. What i don’t understood is in truth how you are now not really a lot more smartly-favored than you might be now. You are very intelligent. You know thus significantly in terms of this matter, produced me for my part believe it from a lot of numerous angles. Its like women and men aren’t involved except it’s one thing to accomplish with Girl gaga! Your personal stuffs great. All the time deal with it up!

  12. Merely a smiling visitant here to share the love (:, btw outstanding style and design. “Competition is a painful thing, but it produces great results.” by Jerry Flint.

  13. It is in point of fact a nice and useful piece of info. I’m happy that you just shared this helpful info with us. Please keep us informed like this. Thanks for sharing.

  14. jamb cbt's avatar jamb cbt says:

    Hello there, just changed into aware of your blog through Google, and located that it’s really informative. I’m gonna watch out for brussels. I will be grateful when you proceed this in future. A lot of folks will likely be benefited from your writing. Cheers!

  15. hey there and thank you for your information – I’ve certainly picked up anything new from right here. I did however expertise some technical issues using this site, as I experienced to reload the website lots of times previous to I could get it to load properly. I had been wondering if your web hosting is OK? Not that I’m complaining, but sluggish loading instances times will often affect your placement in google and can damage your high-quality score if advertising and marketing with Adwords. Well I’m adding this RSS to my email and can look out for a lot more of your respective exciting content. Make sure you update this again very soon..

  16. Yesterday, while I was at work, my cousin stole my iphone and tested to see if it can survive a forty foot drop, just so she can be a youtube sensation. My apple ipad is now destroyed and she has 83 views. I know this is totally off topic but I had to share it with someone!

  17. I’ll right away clutch your rss feed as I can’t find your email subscription link or newsletter service. Do you’ve any? Please let me realize so that I could subscribe. Thanks.

  18. Hey there! Do you know if they make any plugins to help with Search Engine Optimization? I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very good results. If you know of any please share. Thanks!

  19. prodentim's avatar prodentim says:

    I visited a lot of website but I believe this one has something extra in it in it

  20. arialief's avatar arialief says:

    I was suggested this blog by my cousin. I’m not sure whether this post is written by him as no one else know such detailed about my difficulty. You’re amazing! Thanks!

  21. I am no longer certain the place you are getting your information, however good topic. I needs to spend some time finding out more or understanding more. Thank you for magnificent information I was on the lookout for this info for my mission.

  22. I was very pleased to find this web-site.I wanted to thanks for your time for this wonderful read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you blog post.

  23. toto's avatar toto says:

    Some genuinely prime posts on this internet site, bookmarked.

  24. This is a great quality-of-life improvement. The hat operator makes intent much clearer and eliminates the mental overhead of calculating list.Count – 1. It’s a small syntactic enhancement, but it significantly improves readability and expressiveness when working with collections.

  25. I really appreciate this post. I have been looking everywhere for this! Thank goodness I found it on Bing. You have made my day! Thank you again!

  26. Fobertnap's avatar Fobertnap says:

    Explore detailed insights on philips sunrise alarm clocks at TOPCLOCKS, including features, comparisons, and expert recommendations for smarter buying decisions.

  27. agen slot's avatar agen slot says:

    I don’t even understand how I ended up right here, however I believed this post was once great. I don’t know who you’re however certainly you are going to a well-known blogger if you happen to are not already 😉 Cheers!

  28. Pretty nice post. I just stumbled upon your weblog and wished to say that I have really loved browsing your weblog posts. In any case I will be subscribing in your feed and I hope you write again very soon!

  29. It’s in reality a nice and useful piece of information. I’m happy that you just shared this useful info with us. Please keep us informed like this. Thanks for sharing.

  30. I’m not certain where you’re getting your info, however good topic. I needs to spend some time studying more or working out more. Thank you for fantastic information I used to be on the lookout for this info for my mission.

  31. mega888's avatar mega888 says:

    Howdy very cool site!! Guy .. Beautiful .. Amazing .. I will bookmark your website and take the feeds also…I’m satisfied to find a lot of helpful info here in the submit, we need work out extra strategies on this regard, thank you for sharing.

  32. Someone essentially help to make seriously articles I’d state. That is the very first time I frequented your web page and thus far? I surprised with the research you made to make this particular put up incredible. Wonderful job!

  33. oma slot's avatar oma slot says:

    Good, I should definitely pronounce, impressed with your web site. I had no trouble navigating through all tabs as well as related information ended up being truly simple to do to access. I recently found what I hoped for before you know it at all. Quite unusual. Is likely to appreciate it for those who add forums or anything, website theme . a tones way for your customer to communicate. Nice task.

  34. I have been absent for some time, but now I remember why I used to love this website. Thank you, I’ll try and check back more frequently. How frequently you update your site?

  35. camars's avatar camars says:

    Very interesting info!Perfect just what I was searching for!

  36. slot gacor's avatar slot gacor says:

    I like this website very much, Its a very nice berth to read and find information. “Oregano is the spice of life.” by Henry J. Tillman.

  37. I was reading some of your posts on this site and I believe this internet site is rattling informative ! Continue posting.

  38. You actually make it seem so easy with your presentation but I find this matter to be really something which I think I would never understand. It seems too complex and extremely broad for me. I’m looking forward for your next post, I’ll try to get the hang of it!

  39. You made some good points there. I looked on the internet for the subject matter and found most persons will go along with with your website.

  40. I am not real good with English but I line up this really leisurely to read .

  41. Excellent blog! Do you have any tips for aspiring writers? I’m hoping to start my own site soon but I’m a little lost on everything. Would you propose starting with a free platform like WordPress or go for a paid option? There are so many choices out there that I’m completely confused .. Any suggestions? Thanks!

  42. Your place is valueble for me. Thanks!…

  43. Woah! I’m really digging the template/theme of this website. It’s simple, yet effective. A lot of times it’s very difficult to get that “perfect balance” between superb usability and appearance. I must say you have done a great job with this. Additionally, the blog loads extremely quick for me on Safari. Outstanding Blog!

  44. I would like to thank you for the efforts you’ve put in writing this blog. I’m hoping the same high-grade site post from you in the upcoming as well. Actually your creative writing abilities has inspired me to get my own site now. Actually the blogging is spreading its wings quickly. Your write up is a great example of it.

  45. iblbet's avatar iblbet says:

    I like the helpful information you provide to your articles. I will bookmark your blog and test again here regularly. I am slightly sure I’ll learn many new stuff proper right here! Good luck for the following!

  46. Does your website have a contact page? I’m having trouble locating it but, I’d like to shoot you an email. I’ve got some ideas for your blog you might be interested in hearing. Either way, great blog and I look forward to seeing it develop over time.

  47. Its wonderful as your other blog posts : D, regards for putting up.

  48. kursi kuliah's avatar kursi kuliah says:

    I have been browsing on-line greater than three hours lately, but I by no means found any fascinating article like yours. It is beautiful price enough for me. In my opinion, if all website owners and bloggers made excellent content material as you probably did, the internet shall be a lot more useful than ever before.

  49. slot depo 5k's avatar slot depo 5k says:

    It’s in point of fact a nice and useful piece of info. I’m glad that you simply shared this useful info with us. Please keep us informed like this. Thank you for sharing.

  50. I am extremely impressed with your writing skills as well as with the layout on your weblog. Is this a paid theme or did you customize it yourself? Anyway keep up the nice quality writing, it is rare to see a great blog like this one nowadays..

Comments are closed.