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

    Easy North RX: safe online pharmacies in canada – Easy North RX

  2. I went over this internet site and I conceive you have a lot of wonderful information, saved to my bookmarks (:.

  3. It’s actually a great and helpful piece of information. I’m happy that you simply shared this useful information with us. Please keep us informed like this. Thank you for sharing.

  4. OctavioAmoub's avatar OctavioAmoub says:

    MexiCare Direct: MexiCare Direct – MexiCare Direct

  5. JamesSog's avatar JamesSog says:

    safe canadian pharmacies: Easy North RX – Easy North RX

  6. OctavioAmoub's avatar OctavioAmoub says:

    indian pharmacy paypal: buy medicines online in india – medstore online pharmacy

  7. I am curious to find out what blog system you happen to be working with? I’m experiencing some small security issues with my latest website and I’d like to find something more safe. Do you have any suggestions?

  8. Normally I do not read post on blogs, but I would like to say that this write-up very forced me to try and do it! Your writing style has been amazed me. Thanks, very nice article.

  9. hotel pms's avatar hotel pms says:

    I’ve been absent for a while, 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 website?

  10. Randynek's avatar Randynek says:

    https://mexicaredirect.com/# best online mexican pharmacy

  11. I like this weblog its a master peace ! Glad I detected this on google .

  12. nn777's avatar nn777 says:

    Wonderful goods from you, man. I’ve understand your stuff previous to and you are just too fantastic. I actually like what you have acquired here, really like what you’re stating and the way in which you say it. You make it enjoyable and you still take care of to keep it wise. I can’t wait to read much more from you. This is actually a wonderful web site.

  13. But a smiling visitor here to share the love (:, btw great pattern. “Make the most of your regrets… . To regret deeply is to live afresh.” by Henry David Thoreau.

  14. Great post and right to the point. I don’t know if this is in fact the best place to ask but do you folks have any ideea where to get some professional writers? Thanks in advance 🙂

  15. Real good visual appeal on this site, I’d rate it 10 10.

  16. OctavioAmoub's avatar OctavioAmoub says:

    canadian pharmacy meds review: Easy North RX – Easy North RX

  17. IsmaelNiz's avatar IsmaelNiz says:

    The clarity of your writing truly stands out in this article. You managed to capture all the essential points in a perfectly unbiased manner, which I always find very refreshing.

    bookmaker hors arjel

  18. memopezil's avatar memopezil says:

    I got what you mean , thanks for putting up.Woh I am delighted to find this website through google.

  19. Hiya very nice blog!! Man .. Beautiful .. Amazing .. I will bookmark your website and take the feeds additionally?KI’m satisfied to find a lot of helpful information right here within the post, we’d like work out extra techniques on this regard, thank you for sharing. . . . . .

  20. Stephenviaps's avatar Stephenviaps says:

    The level of detail provided in this write-up is genuinely valuable, and it is clear that a significant amount of thought and preparation went into ensuring the final result would be this useful to the audience.

    fastest withdrawal online casino canada

  21. memopezil's avatar memopezil says:

    Thank you, I have just been looking for info approximately this subject for ages and yours is the greatest I’ve found out till now. However, what about the bottom line? Are you certain concerning the supply?

  22. Appreciate it for helping out, fantastic information.

  23. Jamesacutt's avatar Jamesacutt says:

    https://mexicaredirect.com/# medication from mexico

  24. LhanepiliA's avatar LhanepiliA says:

    I am definitely going to save this link for future reference, because the simple and easy-to-digest explanations provided here are going to be very useful whenever I need a quick and reliable reminder about how this all works.

    casino en ligne retrait instantane

  25. It’s exhausting to search out educated folks on this subject, but you sound like you understand what you’re talking about! Thanks

  26. Hello there! Do you use Twitter? I’d like to follow you if that would be ok. I’m definitely enjoying your blog and look forward to new updates.

  27. OLanepiliA's avatar OLanepiliA says:

    Thank you for providing such a complete and objective look at this subject; it is exactly the kind of high-quality reading material I enjoy digesting when I am trying to expand my general knowledge during my downtime.

    meilleur casino en ligne retrait immediat

  28. I dugg some of you post as I thought they were extremely helpful handy

  29. prostadine's avatar prostadine says:

    I have been surfing on-line more than three hours lately, but I by no means found any fascinating article like yours. It is pretty price enough for me. In my view, if all site owners and bloggers made good content as you did, the internet can be a lot more helpful than ever before.

  30. Some really interesting info , well written and loosely user pleasant.

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

  32. of course like your web-site but you have to check the spelling on quite a few of your posts. Many of them are rife with spelling issues and I to find it very bothersome to inform the truth however I’ll surely come again again.

  33. OLanepiliA's avatar OLanepiliA says:

    There is a really good flow throughout this post that keeps the discussion simple to follow and enjoyable to follow, while also maintaining a measured tone that works well for a broad audience of different readers.

    meilleur site paris sportif hors arjel

  34. CBD Cream's avatar CBD Cream says:

    You made some respectable factors there. I appeared on the web for the issue and found most individuals will associate with together with your website.

  35. Delta 10 THC's avatar Delta 10 THC says:

    Please let me know if you’re looking for a article writer for your weblog. You have some really good articles and I think I would be a good asset. If you ever want to take some of the load off, I’d love to write some material for your blog in exchange for a link back to mine. Please shoot me an e-mail if interested. Regards!

  36. IsmaelNiz's avatar IsmaelNiz says:

    The overall tone of this post feels relaxed and useful at the same time, which helps create a positive and enjoyable reading experience for different audiences.

    Bet Way

  37. I actually wanted to send a small message in order to say thanks to you for the wonderful hints you are giving out at this site. My extensive internet lookup has now been paid with reasonable points to talk about with my close friends. I ‘d express that we website visitors actually are undeniably fortunate to be in a fabulous site with many special individuals with very helpful points. I feel truly lucky to have used your entire web site and look forward to many more amazing moments reading here. Thanks a lot again for everything.

  38. I like forgathering utile info, this post has got me even more info! .

  39. Enjoyed looking through this, very good stuff, appreciate it. “Love begets love, love knows no rules, this is the same for all.” by Virgil.

  40. Really informative and superb structure of content, now that’s user pleasant (:.

  41. Randynek's avatar Randynek says:

    http://easynorthrxs.com/# canadian neighbor pharmacy

  42. I’m not sure where you are getting your information, but great topic. I needs to spend some time learning more or understanding more. Thanks for fantastic information I was looking for this info for my mission.

Comments are closed.