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

    My partner and I stumbled over here coming from a different web address and thought I might check things out. I like what I see so now i’m following you. Look forward to looking into your web page again.

  2. Great wordpress blog here.. It’s hard to find quality writing like yours these days. I really appreciate people like you! take care

  3. senang4d's avatar senang4d says:

    That is the correct weblog for anyone who needs to search out out about this topic. You notice a lot its virtually arduous to argue with you (not that I actually would want…HaHa). You undoubtedly put a brand new spin on a topic thats been written about for years. Nice stuff, just nice!

  4. Very interesting information!Perfect just what I was looking for! “Some people don’t get it when I’m being sarcastic.” by Leonardo DiCaprio.

  5. OLanepiliA's avatar OLanepiliA says:

    Thanks , I’ve just been searching for info approximately this topic for ages and yours is the greatest I have found out till now. However, what concerning the bottom line? Are you certain about the source?
    https://vodamarket.od.ua/linzy-v-fary-audi-q5-yak-pokrashchyty-svitlo-dlya.html

  6. You must take part in a contest for among the finest blogs on the web. I will advocate this website!

  7. Hi! I’m at work surfing around your blog from my new iphone! Just wanted to say I love reading your blog and look forward to all your posts! Carry on the superb work!

  8. LamarBlick's avatar LamarBlick says:

    Generic Viagra for sale CoreBlue Health Generic Viagra for sale

  9. I have been surfing on-line more than three hours nowadays, but I never discovered any fascinating article like yours. It is pretty value enough for me. Personally, if all website owners and bloggers made excellent content as you did, the internet will be much more useful than ever before. “Wherever they burn books, they will also, in the end, burn people.” by Heinrich Heine.

  10. You made some nice points there. I looked on the internet for the subject matter and found most guys will consent with your site.

  11. olxtoto 4d's avatar olxtoto 4d says:

    It’s really a nice and helpful piece of information. I am glad that you shared this useful info with us. Please keep us up to date like this. Thanks for sharing.

  12. Thanks for the sensible critique. Me and my neighbor were just preparing to do some research on this. We got a grab a book from our local library but I think I learned more from this post. I’m very glad to see such magnificent info being shared freely out there.

  13. jago22's avatar jago22 says:

    I really like your writing style, excellent info , thankyou for putting up :D.

  14. olxtoto's avatar olxtoto says:

    I think other web-site proprietors should take this website as an model, very clean and wonderful user genial style and design, let alone the content. You are an expert in this topic!

  15. jagoledak's avatar jagoledak says:

    Would you be excited by exchanging hyperlinks?

  16. I like what you guys tend to be up too. Such clever work and coverage! Keep up the good works guys I’ve included you guys to my personal blogroll.

  17. jagoledak's avatar jagoledak says:

    I went over this site and I think you have a lot of great info , saved to my bookmarks (:.

  18. MichaelBiomo's avatar MichaelBiomo says:

    buy medicines online in india: indian pharmacies safe – mail order pharmacy india

  19. Howdy would you mind stating which blog platform you’re working with? I’m planning to start my own blog in the near future but I’m having a hard time choosing between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your design and style seems different then most blogs and I’m looking for something completely unique. P.S My apologies for getting off-topic but I had to ask!

  20. After study just a few of the blog posts on your web site now, and I really like your way of blogging. I bookmarked it to my bookmark web site checklist and will be checking again soon. Pls try my web page as nicely and let me know what you think.

  21. Aw, this was a really nice post. In concept I would like to put in writing like this additionally – taking time and precise effort to make a very good article… however what can I say… I procrastinate alot and in no way seem to get something done.

  22. Tonic greens's avatar Tonic greens says:

    Hi there, i read your blog from time to time and i own a similar one and i was just wondering if you get a lot of spam remarks? If so how do you protect against it, any plugin or anything you can recommend? I get so much lately it’s driving me crazy so any help is very much appreciated.

  23. kera288's avatar kera288 says:

    Hi, I think your site might be having browser compatibility issues. When I look at your website in Chrome, it looks fine but when opening in Internet Explorer, it has some overlapping. I just wanted to give you a quick heads up! Other then that, superb blog!

  24. Just a smiling visitant here to share the love (:, btw outstanding layout.

  25. ledak388's avatar ledak388 says:

    Great web site. A lot of useful information here. I am sending it to a few buddies ans also sharing in delicious. And certainly, thank you on your sweat!

  26. poolzubehör's avatar poolzubehör says:

    You are a very smart individual!

  27. ultra33's avatar ultra33 says:

    This is very interesting, You are a very skilled blogger. I have joined your feed and look forward to seeking more of your excellent post. Also, I have shared your website in my social networks!

  28. dewi288's avatar dewi288 says:

    I believe this web site has very great composed articles content.

  29. dewi5000's avatar dewi5000 says:

    You have remarked very interesting details! ps nice site. “There’s always one who loves and one who lets himself be loved.” by W. Somerset Maugham.

  30. ledak788's avatar ledak788 says:

    I have been exploring for a little bit for any high quality articles or blog posts on this sort of house . Exploring in Yahoo I eventually stumbled upon this website. Reading this information So i am satisfied to exhibit that I’ve a very just right uncanny feeling I came upon just what I needed. I so much for sure will make certain to don’t overlook this web site and provides it a look regularly.

  31. dewi188's avatar dewi188 says:

    Have you ever thought about creating an ebook or guest authoring on other websites? I have a blog based upon on the same ideas you discuss and would really like to have you share some stories/information. I know my subscribers would appreciate your work. If you’re even remotely interested, feel free to shoot me an e-mail.

  32. ledak188's avatar ledak188 says:

    Thank you a bunch for sharing this with all of us you really realize what you’re speaking about! Bookmarked. Kindly also seek advice from my website =). We will have a hyperlink exchange arrangement among us!

  33. dewi11's avatar dewi11 says:

    I really thankful to find this web site on bing, just what I was searching for : D too saved to bookmarks.

  34. dewi11's avatar dewi11 says:

    You have remarked very interesting details! ps decent website.

  35. dewi11's avatar dewi11 says:

    An impressive share, I just given this onto a colleague who was doing a little analysis on this. And he in fact bought me breakfast because I found it for him.. smile. So let me reword that: Thnx for the treat! But yeah Thnkx for spending the time to discuss this, I feel strongly about it and love reading more on this topic. If possible, as you become expertise, would you mind updating your blog with more details? It is highly helpful for me. Big thumb up for this blog post!

  36. ledak388's avatar ledak388 says:

    Thanks for sharing excellent informations. Your site is so cool. I’m impressed by the details that you have on this blog. It reveals how nicely you perceive this subject. Bookmarked this website page, will come back for extra articles. You, my pal, ROCK! I found simply the information I already searched all over the place and simply could not come across. What an ideal site.

  37. AllenLew's avatar AllenLew says:

    https://ivermectinfast.shop/# antiparasitic medication usa

  38. obviously like your website but you need to check the spelling on quite a few of your posts. Several of them are rife with spelling issues and I find it very troublesome to inform the truth nevertheless I’ll certainly come back again.

  39. olxtoto's avatar olxtoto says:

    Would you be serious about exchanging hyperlinks?

  40. RayfordRhymn's avatar RayfordRhymn says:

    prednisone 5 mg tablet cost: prednisone for inflammation treatment – buy prednisone online paypal

  41. Good info. Lucky me I reach on your website by accident, I bookmarked it.

  42. Kevinvinge's avatar Kevinvinge says:

    amoxil without prescription usa: bacterial infection medication – buy amoxicillin 500mg usa

  43. Kevinvinge's avatar Kevinvinge says:

    ivermectin fast: parasite infection medication – antiparasitic medication usa

  44. rajabandot's avatar rajabandot says:

    Hmm it looks like your site ate my first comment (it was super 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 blogger but I’m still new to the whole thing. Do you have any suggestions for beginner blog writers? I’d certainly appreciate it.

  45. slot88's avatar slot88 says:

    It’s in reality a great and helpful piece of information. I am happy that you simply shared this helpful information with us. Please keep us up to date like this. Thank you for sharing.

  46. senangslot's avatar senangslot says:

    I really like your writing style, superb info, thanks for putting up :D. “Kennedy cooked the soup that Johnson had to eat.” by Konrad Adenauer.

  47. OLanepiliA's avatar OLanepiliA says:

    We absolutely love your blog and find many of your post’s to be just what I’m looking for. can you offer guest writers to write content for you personally? I wouldn’t mind composing a post or elaborating on a number of the subjects you write regarding here. Again, awesome website!
    https://teletype.in/@avtobloggerua/B8-lsvQ4Hyu

  48. togel online's avatar togel online says:

    After I initially commented I clicked the -Notify me when new feedback are added- checkbox and now every time a comment is added I get 4 emails with the identical comment. Is there any approach you’ll be able to remove me from that service? Thanks!

  49. result macau's avatar result macau says:

    I loved as much as you’ll receive carried out right here. The sketch is tasteful, your authored material stylish. nonetheless, you command get bought an edginess over that you wish be delivering the following. unwell unquestionably come further formerly again since exactly the same nearly very often inside case you shield this increase.

Comments are closed.