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. Great post. I am facing a couple of these problems.

  2. Outstanding post, I conceive website owners should learn a lot from this web site its very user friendly.

  3. ratubet's avatar ratubet says:

    Merely wanna admit that this is very helpful, Thanks for taking your time to write this.

  4. At this time it seems like Expression Engine is the preferred blogging platform available right now. (from what I’ve read) Is that what you are using on your blog?

  5. Currently it looks like BlogEngine is the top blogging platform out there right now. (from what I’ve read) Is that what you’re using on your blog?

  6. Currently it looks like Expression Engine is the top blogging platform out there right now. (from what I’ve read) Is that what you’re using on your blog?

  7. pestoto's avatar pestoto says:

    Please let me know if you’re looking for a author for your site. You have some really great articles and I feel I would be a good asset. If you ever want to take some of the load off, I’d absolutely love to write some content for your blog in exchange for a link back to mine. Please blast me an email if interested. Cheers!

  8. JamesEsoli's avatar JamesEsoli says:

    cheapest cialis erectile dysfunction

  9. Thanks , I’ve recently been looking for information about this subject for ages and yours is the greatest I’ve discovered till now. But, what about the bottom line? Are you sure about the source?

  10. Hey there, You’ve done an excellent job. I’ll definitely digg it and in my opinion recommend to my friends. I’m sure they will be benefited from this site.

  11. Hey there, You’ve done a great job. I’ll definitely digg it and individually suggest to my friends. I am sure they will be benefited from this website.

  12. Merely wanna comment on few general things, The website design and style is perfect, the content material is really wonderful. “The enemy is anybody who’s going to get you killed, no matter which side he’s on.” by Joseph Heller.

  13. Hey there, You have performed a great job. I’ll certainly digg it and in my opinion recommend to my friends. I’m confident they will be benefited from this site.

  14. Hi there, You have done an incredible job. I will definitely digg it and in my view suggest to my friends. I’m sure they will be benefited from this website.

  15. Hello there, You have done a fantastic job. I’ll certainly digg it and individually suggest to my friends. I’m sure they’ll be benefited from this web site.

  16. Hi there, You have performed an incredible job. I will definitely digg it and for my part suggest to my friends. I’m sure they will be benefited from this site.

  17. Hello there, You have performed an incredible job. I’ll certainly digg it and in my opinion recommend to my friends. I’m confident they’ll be benefited from this web site.

  18. Hello there, You have performed an excellent job. I will certainly digg it and in my view suggest to my friends. I am sure they will be benefited from this website.

  19. GOGO电竞's avatar GOGO电竞 says:

    Hey there, You have performed a great job. I’ll certainly digg it and personally suggest to my friends. I am confident they’ll be benefited from this site.

  20. KeithDit's avatar KeithDit says:

    Cheap Levitra online: Macksood Urology – Levitra online pharmacy

  21. Hi there, You have done an incredible job. I will certainly digg it and in my opinion suggest to my friends. I am sure they will be benefited from this web site.

  22. Hi there, You have done a fantastic job. I will definitely digg it and individually suggest to my friends. I am confident they’ll be benefited from this web site.

  23. pestoto's avatar pestoto says:

    Thank you for sharing excellent informations. Your site is so cool. I’m impressed by the details that you have on this website. It reveals how nicely you perceive this subject. Bookmarked this website page, will come back for extra articles. You, my friend, ROCK! I found simply the information I already searched all over the place and simply couldn’t come across. What a perfect web site.

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

  25. Williamisono's avatar Williamisono says:

    Svenska Pharma: bakteriell vaginos apotek – Svenska Pharma

  26. At this time it seems like Drupal is the preferred blogging platform available right now. (from what I’ve read) Is that what you are using on your blog?

  27. At this time it seems like Drupal is the top blogging platform out there right now. (from what I’ve read) Is that what you are using on your blog?

  28. Right now it seems like Expression Engine is the top blogging platform available right now. (from what I’ve read) Is that what you’re using on your blog?

  29. basket168's avatar basket168 says:

    I was suggested this web site by way of my cousin. I’m now not positive whether or not this post is written by means of him as nobody else recognize such certain about my difficulty. You’re amazing! Thanks!

  30. Williamisono's avatar Williamisono says:

    mexican pharma: reputable mexican pharmacy – phentermine in mexico pharmacy

  31. Williamarini's avatar Williamarini says:

    Levitra 10 mg best price Levitra 10 mg best price Levitra online pharmacy

  32. Great post, you have pointed out some good details , I also conceive this s a very great website.

  33. Cryptify Hub's avatar Cryptify Hub says:

    把它想象成一个自助餐厅。Cryptify Hub把几百道菜(工具链接)摆在架子上,你拿个盘子(浏览器)自己选。但它不负责喂你,也不保证每道菜都合你口味、都新鲜、都没下毒。自助餐的精髓:自己选,自己吃,自己承担后果。

  34. Cryptify Hub's avatar Cryptify Hub says:

    币圈版“兴趣班”,还是免费的那种。Cryptify Hub没有学费、没有入学考试、没有毕业设计。你进去就是一名“民间学员”,可以看别人分享的链接,也可以自己发言。但兴趣班嘛,老师可能是隔壁桌的大哥,教材可能是网上找的链接合集。质量不保证,但胜在真实——至少没人骗你学费(因为根本没学费)。当作社交+导航站使用,心态稳稳的。

  35. Louistrugs's avatar Louistrugs says:

    Nicely put. Thanks.
    Wein Plaza – https://weinplaza.com/

  36. GregoryVar's avatar GregoryVar says:

    Levitra 20 mg for sale: Buy generic Levitra online – Vardenafil buy online

  37. pestoto's avatar pestoto says:

    Magnificent goods from you, man. I’ve understand your stuff previous to and you are just extremely great. I actually like what you have acquired here, certainly like what you’re stating and the way in which you say it. You make it enjoyable and you still care for to keep it smart. I cant wait to read far more from you. This is actually a wonderful site.

  38. Francisinjuh's avatar Francisinjuh says:

    mexican pharmacies that ship to us pharmacy delivery medication in mexico

  39. wanimage's avatar wanimage says:

    I really appreciate how the ‘hat’ operator significantly streamlines accessing items from the end of a list, making code much more readable than the old cumbersome syntax. It’s a subtle but powerful update that feels especially natural for developers transitioning from Python’s familiar indexing style. Looking forward to seeing how this pattern evolves in future C# tutorials alongside the new switch expressions!

  40. Jamesguism's avatar Jamesguism says:

    Viagra femme ou trouver: SildГ©nafil 100mg pharmacie en ligne – Viagra homme prix en pharmacie sans ordonnance

  41. Hello are using WordPress for your site platform? I’m new to the blog world but I’m trying to get started and create my own. Do you need any html coding knowledge to make your own blog? Any help would be greatly appreciated!

  42. You can definitely see your enthusiasm in the work you write. The world hopes for more passionate writers like you who aren’t afraid to say how they believe. Always go after your heart.

  43. rajawd link's avatar rajawd link says:

    I don’t even know how I ended up here, but I thought this post was great. I do not know who you are but certainly you’re going to a famous blogger if you aren’t already 😉 Cheers!

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

  45. rajawd's avatar rajawd says:

    Nice read, I just passed this onto a colleague who was doing a little research on that. And he just bought me lunch because I found it for him smile So let me rephrase that: Thanks for lunch!

  46. kilat333's avatar kilat333 says:

    Its like you read my mind! You appear to know so much about this, like you wrote the book in it or something. I think that you could do with some pics to drive the message home a little bit, but instead of that, this is fantastic blog. A great read. I will definitely be back.

  47. poros77 slot's avatar poros77 slot says:

    Hi there! This post couldn’t be written any better! Reading through this post reminds me of my previous room mate! He always kept talking about this. I will forward this article to him. Pretty sure he will have a good read. Thank you for sharing!

Comments are closed.