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. jewe43 slot's avatar jewe43 slot says:

    I’ll immediately take hold of your rss as I can’t find your e-mail subscription hyperlink or e-newsletter service. Do you have any? Please allow me know so that I may subscribe. Thanks.

  2. Hello just wanted to give you a brief 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 results.

  3. Very interesting points you have remarked, appreciate it for putting up. “It’s the soul’s duty to be loyal to its own desires. It must abandon itself to its master passion.” by Rebecca West.

  4. olxtoto's avatar olxtoto says:

    Wow, superb blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your web site is fantastic, let alone the content!

  5. I precisely desired to thank you so much once again. I do not know what I might have followed without these tips discussed by you relating to such a situation. It previously was a very terrifying difficulty in my opinion, nevertheless looking at a new skilled tactic you resolved that forced me to jump over delight. I will be grateful for your work and thus believe you are aware of a great job you are getting into training other individuals with the aid of your web page. More than likely you have never met any of us.

  6. I am continuously searching online for ideas that can benefit me. Thx!

  7. toto slot's avatar toto slot says:

    Can I just say what a relief to find someone who actually knows what theyre talking about on the internet. You definitely know how to bring an issue to light and make it important. More people need to read this and understand this side of the story. I cant believe youre not more popular because you definitely have the gift.

  8. Very interesting information!Perfect just what I was looking for! “Time is money.” by Benjamin Franklin.

  9. Attractive section of content. I just stumbled upon your blog and in accession capital to assert that I acquire in fact enjoyed account your blog posts. Anyway I’ll be subscribing to your augment and even I achievement you access consistently quickly.

  10. Some really choice blog posts on this site, saved to bookmarks.

  11. Greetings! Very helpful advice on this article! It is the little changes that make the biggest changes. Thanks a lot for sharing!

  12. I believe this is one of the most important information for me. And i am satisfied studying your article. However should remark on some basic issues, The site taste is ideal, the articles is in point of fact nice : D. Just right activity, cheers

  13. I got what you mean , regards for posting.Woh I am delighted to find this website through google.

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

  15. I enjoy studying and I believe this website got some really utilitarian stuff on it! .

  16. I think this website has some really wonderful information for everyone : D.

  17. I went over this site and I think you have a lot of excellent info , saved to fav (:.

  18. Well I really liked reading it. This tip procured by you is very useful for accurate planning.

  19. olxtoto's avatar olxtoto says:

    It’s actually a great and useful piece of information. I am glad that you shared this useful info with us. Please keep us informed like this. Thanks for sharing.

  20. situs toto's avatar situs toto says:

    This design is spectacular! You most certainly know how to keep a reader entertained. Between your wit and your videos, I was almost moved to start my own blog (well, almost…HaHa!) Excellent job. I really loved what you had to say, and more than that, how you presented it. Too cool!

  21. 497119 821946Get started with wales ahead almost every planking. Ones wales truly are a compilation of huge planks one certain depth advisors certainly will be the identical towards the entire hull planking even so with even bigger density to successfully thrust outward beyond the planking. planking 561314

  22. Having read this I thought it was very informative. I appreciate you taking the time and effort to put this article together. I once again find myself spending way to much time both reading and commenting. But so what, it was still worth it!

  23. olxtoto slot's avatar olxtoto slot says:

    I’ve recently started a web site, the info you provide on this web site has helped me greatly. Thanks for all of your time & work.

  24. macau's avatar macau says:

    My brother recommended I may like this blog. He was entirely right. This submit truly made my day. You can not believe simply how so much time I had spent for this info! Thank you!

  25. 클릭's avatar 클릭 says:

    Just wish to say your article is as surprising. The clearness to your submit is simply cool and i can think you are knowledgeable in this subject. Well with your permission let me to grasp your feed to stay updated with impending post. Thank you a million and please carry on the gratifying work.

  26. Very well written post. It will be valuable to everyone who usess it, as well as myself. Keep doing what you are doing – can’r wait to read more posts.

  27. 757699 661035Woh I enjoy your posts , bookmarked ! My wife and i take problem along with your last point. 123431

  28. BETFLIX's avatar BETFLIX says:

    638535 147274Just wanna comment that you have a quite good internet web site , I really like the design it actually stands out. 227237

  29. Thank you for sharing excellent informations. Your web site is so cool. I’m impressed by the details that you’ve on this blog. It reveals how nicely you understand this subject. Bookmarked this website page, will come back for extra articles. You, my pal, ROCK! I found just the information I already searched everywhere and simply couldn’t come across. What an ideal site.

  30. Hello there, I found your blog via Google while searching for a related topic, your web site came up, it looks great. I have bookmarked it in my google bookmarks.

  31. Great post. I am facing a couple of these problems.

  32. 41072 741660This is some excellent details. I expect additional facts like this was distributed across the internet today. 450154

  33. ceritoto's avatar ceritoto says:

    I have read some good stuff here. Definitely price bookmarking for revisiting. I wonder how a lot attempt you set to create one of these magnificent informative web site.

  34. I conceive you have remarked some very interesting points, appreciate it for the post.

  35. ceritoto's avatar ceritoto says:

    Really wonderful visual appeal on this website , I’d rate it 10 10.

  36. I view something really special in this internet site.

  37. I’d have to examine with you here. Which is not one thing I usually do! I take pleasure in reading a post that may make folks think. Additionally, thanks for permitting me to comment!

  38. ceritoto's avatar ceritoto says:

    Thanks for any other informative web site. Where else could I get that kind of information written in such a perfect approach? I’ve a challenge that I’m simply now working on, and I have been at the look out for such information.

  39. I dugg some of you post as I cerebrated they were handy extremely helpful

  40. akongcuan's avatar akongcuan says:

    Good write-up, I?¦m regular visitor of one?¦s blog, maintain up the nice operate, and It is going to be a regular visitor for a lengthy time.

  41. 777pub's avatar 777pub says:

    I’m extremely impressed with your writing skills and also with the layout on your weblog. Is this a paid theme or did you modify it yourself? Anyway keep up the excellent quality writing, it is rare to see a great blog like this one nowadays..

  42. mukapoker's avatar mukapoker says:

    I got what you mean , thankyou for posting.Woh I am thankful to find this website through google.

  43. ceritoto's avatar ceritoto says:

    What¦s Going down i am new to this, I stumbled upon this I have found It positively helpful and it has aided me out loads. I hope to contribute & help different users like its helped me. Great job.

  44. I’m still learning from you, while I’m trying to achieve my goals. I definitely liked reading all that is written on your website.Keep the posts coming. I liked it!

  45. ceritoto's avatar ceritoto says:

    A lot of the things you claim happens to be supprisingly appropriate and it makes me wonder why I had not looked at this in this light previously. Your article truly did switch the light on for me as far as this particular issue goes. However there is one particular position I am not necessarily too comfortable with and while I attempt to reconcile that with the central theme of your position, permit me observe just what all the rest of the subscribers have to point out.Very well done.

  46. Thank you for the good writeup. It in reality used to be a leisure account it. Look advanced to far delivered agreeable from you! However, how can we communicate?

  47. situs gacor's avatar situs gacor says:

    Wohh exactly what I was looking for, thankyou for putting up.

  48. jkt88's avatar jkt88 says:

    Great post, you have pointed out some superb details , I besides believe this s a very superb website.

  49. Hello my friend! I wish to say that this post is awesome, nice written and include approximately all important infos. I’d like to see more posts like this.

  50. dhk4d login's avatar dhk4d login says:

    This internet site is my aspiration, real good style and design and perfect written content.

Comments are closed.