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. kaka hoki's avatar kaka hoki says:

    You are a very clever person!

  2. slot qris's avatar slot qris says:

    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!

  3. I have been absent for a while, but now I remember why I used to love this site. Thank you, I’ll try and check back more frequently. How frequently you update your web site?

  4. Would you be focused on exchanging links?

  5. I really enjoy examining on this web site, it contains excellent posts. “The living is a species of the dead and not a very attractive one.” by Friedrich Wilhelm Nietzsche.

  6. You actually make it appear so easy together with your presentation however I in finding this topic to be really one thing which I think I might never understand. It seems too complicated and very extensive for me. I’m taking a look forward to your next post, I’ll attempt to get the cling of it!

  7. judi bola's avatar judi bola says:

    Admiring the persistence you put into your blog and in depth information you offer. It’s awesome to come across a blog every once in a while that isn’t the same out of date rehashed material. Fantastic read! I’ve saved your site and I’m including your RSS feeds to my Google account.

  8. slot gacor's avatar slot gacor says:

    I precisely needed to thank you very much again. I am not sure what I would’ve handled without these advice discussed by you regarding this situation. It became a very fearsome crisis in my circumstances, however , being able to see the very professional way you solved it took me to weep for happiness. I am just happy for the advice and sincerely hope you know what a great job you are always providing training most people thru your websites. I am sure you’ve never encountered all of us.

  9. instantoto's avatar instantoto says:

    I¦ve recently started a blog, the information you offer on this site has helped me tremendously. Thank you for all of your time & work.

  10. mpo555's avatar mpo555 says:

    You are my aspiration, I have few blogs and very sporadically run out from to post .

  11. bandar toto's avatar bandar toto says:

    Absolutely composed written content, Really enjoyed looking through.

  12. mpo555's avatar mpo555 says:

    What i do not understood is in fact how you are no longer actually much more neatly-preferred than you might be right now. You’re very intelligent. You already know thus significantly when it comes to this matter, produced me individually imagine it from numerous various angles. Its like women and men are not involved until it?¦s something to accomplish with Lady gaga! Your own stuffs outstanding. Always care for it up!

  13. toto 4d's avatar toto 4d says:

    Great blog! Is your theme custom made or did you download it from somewhere? A design like yours with a few simple tweeks would really make my blog shine. Please let me know where you got your theme. Thanks

  14. ansiedade's avatar ansiedade says:

    hello!,I love your writing very much! percentage we be in contact more approximately your post on AOL? I need an expert on this house to resolve my problem. Maybe that is you! Looking forward to peer you.

  15. I visited a lot of website but I think this one contains something special in it in it

  16. Its like you read my mind! You seem to know a lot about this, like you wrote the book in it or something. I think that you can do with some pics to drive the message home a little bit, but instead of that, this is magnificent blog. A great read. I’ll definitely be back.

  17. Enjoyed examining this, very good stuff, regards.

  18. whoah this blog is wonderful i like reading your articles. Stay up the good work! You realize, many persons are looking round for this information, you could help them greatly.

  19. SAKONGTOTO's avatar SAKONGTOTO says:

    Create a positive environment. Living in a positive and supportive environment will naturally make life more enjoyable and motivating.
    linklist.bio/sakongtoto

  20. Thanks a lot for sharing this with all of us you really know what you’re talking approximately! Bookmarked. Kindly additionally discuss with my website =). We could have a hyperlink exchange contract among us!

  21. slot gacor's avatar slot gacor says:

    Super-Duper site! I am loving it!! Will come back again. I am taking your feeds also

  22. ceritoto's avatar ceritoto says:

    Of course, what a magnificent blog and enlightening posts, I surely will bookmark your site.Best Regards!

  23. ceritoto's avatar ceritoto says:

    whoah this blog is wonderful i love reading your articles. Keep up the good work! You know, many people are searching around for this info, you could aid them greatly.

  24. I’ve been browsing on-line more than three hours nowadays, yet I by no means discovered any interesting article like yours. It¦s lovely price sufficient for me. Personally, if all webmasters and bloggers made excellent content as you did, the internet will probably be much more helpful than ever before.

  25. Hello.This article was extremely fascinating, particularly since I was searching for thoughts on this issue last Thursday.

  26. slot mania's avatar slot mania says:

    Some genuinely interesting information, well written and generally user genial.

  27. this link's avatar this link says:

    I conceive you have mentioned some very interesting points, thanks for the post.

  28. ceritoto's avatar ceritoto says:

    I like this website very much, Its a rattling nice spot to read and obtain information.

  29. situs slot's avatar situs slot says:

    Howdy! Someone in my Facebook group shared this site with us so I came to give it a look. I’m definitely enjoying the information. I’m book-marking and will be tweeting this to my followers! Excellent blog and outstanding design and style.

  30. filmindo's avatar filmindo says:

    I have been absent for a while, but now I remember why I used to love this blog. Thanks, I will try and check back more often. How frequently you update your website?

  31. ceritoto's avatar ceritoto says:

    This is a very good tips especially to those new to blogosphere, brief and accurate information… Thanks for sharing this one. A must read article.

  32. situs togel's avatar situs togel says:

    Its excellent as your other blog posts : D, appreciate it for putting up. “Too much sensibility creates unhappiness too much insensibility leads to crime.” by Charles Maurice de Talleyrand.

  33. Hmm it appears like your website ate my first comment (it was extremely long) so I guess I’ll just sum it up what I submitted and say, I’m thoroughly enjoying your blog. I too am an aspiring blog writer but I’m still new to the whole thing. Do you have any tips for novice blog writers? I’d genuinely appreciate it.

  34. pestoto's avatar pestoto says:

    I like what you guys are up also. Such intelligent work and reporting! Keep up the excellent works guys I have incorporated you guys to my blogroll. I think it will improve the value of my website 🙂

  35. 無限品牌's avatar 無限品牌 says:

    Pretty! This was a really wonderful post. Thank you for your provided information.

  36. pestoto slot's avatar pestoto slot says:

    Great write-up, I’m regular visitor of one’s blog, maintain up the excellent operate, and It’s going to be a regular visitor for a lengthy time.

  37. Some genuinely wonderful info , Gladiolus I observed this.

  38. nextogel's avatar nextogel says:

    I was recommended this website by way of my cousin. I’m no longer certain whether or not this submit is written by way of him as nobody else understand such certain approximately my problem. You’re wonderful! Thank you!

  39. Rattling nice layout and fantastic written content, nothing at all else we want : D.

  40. nextogel's avatar nextogel says:

    I just could not leave your web site prior to suggesting that I really loved the standard information a person supply to your visitors? Is gonna be again regularly in order to check out new posts

  41. Attractive section of content. I just stumbled upon your blog and in accession capital to assert that I get actually enjoyed account your blog posts. Any way I’ll be subscribing on your feeds or even I success you get right of entry to constantly rapidly.

  42. malware's avatar malware says:

    Great write-up, I am normal visitor of one¦s web site, maintain up the excellent operate, and It’s going to be a regular visitor for a lengthy time.

  43. nextogel's avatar nextogel says:

    I appreciate, cause I found just what I was looking for. You have ended my four day long hunt! God Bless you man. Have a nice day. Bye

  44. 838129 290957Wow, superb weblog structure! How long have you been blogging for? you make blogging glance simple. The total look of your internet web site is exceptional, neatly as the content material! 502418

  45. 512638 517667This really is a great topic to speak about. Normally when I locate stuff like this I stumble it. This post probably wont do properly with that crowd. I will likely be certain to submit something else though. 409518

  46. 689804 349401I was recommended this internet website by my cousin. Im not sure whether this post is written by him as nobody else know such detailed about my trouble. You are amazing! Thanks! 930579

  47. malware's avatar malware says:

    Hiya, I’m really glad I have found this info. Nowadays bloggers publish only about gossips and net and this is actually annoying. A good web site with exciting content, that’s what I need. Thanks for keeping this site, I’ll be visiting it. Do you do newsletters? Cant find it.

  48. nextogel's avatar nextogel says:

    I reckon something truly special in this internet site.

  49. malware's avatar malware says:

    Thanks , I’ve just been looking for information approximately this topic for a while and yours is the best I have came upon so far. However, what in regards to the conclusion? Are you sure in regards to the supply?

  50. Malware's avatar Malware says:

    Great goods from you, man. I have take into accout your stuff prior to and you’re just too excellent. I really like what you have obtained right here, really like what you’re stating and the way by which you are saying it. You make it enjoyable and you still take care of to stay it wise. I can’t wait to learn far more from you. That is really a wonderful web site.

Comments are closed.