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

    Generic Tadalafil 20mg price MensRxGuide – Cheap Cialis

  2. olxtoto link's avatar olxtoto link says:

    Thanks , I’ve just been looking for information about this subject for ages and yours is the greatest I have discovered so far. But, what about the conclusion? Are you sure about the source?

  3. Kennethcrame's avatar Kennethcrame says:

    Cialis without a doctor prescription п»їcialis generic – cialis for sale

  4. I have been checking out many of your posts and i can claim pretty good stuff. I will definitely bookmark your site.

  5. My brother suggested I might like this website. He was entirely right. This post truly made my day. You can not imagine simply how much time I had spent for this info! Thanks!

  6. Good write-up, I am regular visitor of one’s website, maintain up the excellent operate, and It’s going to be a regular visitor for a lengthy time.

  7. slot gacor's avatar slot gacor says:

    You have brought up a very good details, thanks for the post.

  8. LhanepiliA's avatar LhanepiliA says:

    An impressive share! I’ve just forwarded this onto a co-worker who had been conducting a little research on this. And he actually ordered me lunch simply because I stumbled upon it for him… lol. So allow me to reword this…. Thanks for the meal!! But yeah, thanks for spending some time to talk about this matter here on your web site.
    https://miroshina.com.ua/prozoryi-hermetyk-u-farakh-yak-dosyahty.html

  9. toto macau's avatar toto macau says:

    Along with almost everything that appears to be developing throughout this particular subject matter, many of your viewpoints are generally relatively radical. Having said that, I appologize, but I do not subscribe to your entire suggestion, all be it stimulating none the less. It appears to everybody that your opinions are not totally rationalized and in simple fact you are your self not really completely confident of the argument. In any event I did take pleasure in looking at it.

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

  11. EarnestCor's avatar EarnestCor says:

    Good day! Would you mind if I share your blog with my facebook group? There’s a lot of people that I think would really appreciate your content. Please let me know. Cheers

    https://www.google.mg/maps/d/edit?mid=14i7FLjDQyujYX0eb5iKDp8HcsvhkHNo

  12. olxtoto's avatar olxtoto says:

    This really answered my downside, thanks!

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

    I have recently started a website, the info you offer on this site has helped me tremendously. Thanks for all of your time & work.

  14. Timsothybioli's avatar Timsothybioli says:

    Hi there! Do you use Twitter? I’d like to follow you if that would be okay. I’m absolutely enjoying your blog and look forward to new posts.
    บาคาร่า

  15. AnthonyJoiff's avatar AnthonyJoiff says:

    Cheap generic Viagra online buy viagra here – Cheap Sildenafil 100mg

  16. 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 wrote 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 suggestions for rookie blog writers? I’d really appreciate it.

  17. Hi, Neat post. There is a problem with your website in internet explorer, would test this… IE still is the market leader and a large portion of people will miss your excellent writing due to this problem.

  18. Donaldtix's avatar Donaldtix says:

    trustworthy online pharmacy Victo Pharm no rx needed pharmacy

  19. LewisGor's avatar LewisGor says:

    Hello to every body, it’s my first pay a quick visit of this blog; this blog contains remarkable and in fact good data in favor of readers.
    online casino mit paysafe

  20. JoshuaNet's avatar JoshuaNet says:

    victoza delivery liraglutide pharmacy liraglutide online

  21. ShanepiliA's avatar ShanepiliA says:

    Hey there! I know this is kinda off topic but I’d figured I’d ask. Would you be interested in exchanging links or maybe guest authoring a blog post or vice-versa? My website addresses a lot of the same topics as yours and I believe we could greatly benefit from each other. If you happen to be interested feel free to shoot me an e-mail. I look forward to hearing from you! Great blog by the way!
    casino en ligne retrait immediat

  22. GichardAmomi's avatar GichardAmomi says:

    I’m not sure why but this website is loading incredibly slow for me. Is anyone else having this problem or is it a issue on my end? I’ll check back later on and see if the problem still exists.
    บาคาร่า

  23. Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your point. You obviously know what youre talking about, why throw away your intelligence on just posting videos to your weblog when you could be giving us something enlightening to read?

  24. Hey, you used to write magnificent, but the last several posts have been kinda boring?K I miss your tremendous writings. Past few posts are just a little bit out of track! come on!

  25. Hi, Neat post. There is a problem with your website in internet explorer, would test this… IE still is the market leader and a large portion of people will miss your great writing due to this problem.

  26. I’m extremely impressed together with your writing talents and also with the layout on your weblog. Is that this a paid subject matter or did you customize it yourself? Either way keep up the excellent high quality writing, it’s rare to see a great blog like this one nowadays..

  27. Thank you for sharing with us, I conceive this website genuinely stands out :D.

  28. dewi11's avatar dewi11 says:

    Thanks for this rattling post, I am glad I detected this internet site on yahoo.

  29. OLanepiliA's avatar OLanepiliA says:

    Just wish to say your article is as surprising. The clarity in your post is simply spectacular and i can assume you are an expert on this subject. Fine with your permission let me to grab your feed to keep up to date with forthcoming post. Thanks a million and please carry on the gratifying work.
    บาคาร่า

  30. LewisGor's avatar LewisGor says:

    Superb post but I was wanting to know if you could write a litte more on this subject? I’d be very grateful if you could elaborate a little bit further. Bless you!
    viagra pills sexual xxx porn pills

  31. I am always searching online for tips that can help me. Thanks!

  32. OLanepiliA's avatar OLanepiliA says:

    Useful information. Fortunate me I discovered your web site by accident, and I’m surprised why this coincidence didn’t happened earlier! I bookmarked it.
    meilleur casino en ligne

  33. Timsothybioli's avatar Timsothybioli says:

    WOW just what I was searching for. Came here by searching for %keyword%
    หวยออนไลน์

  34. EarnestCor's avatar EarnestCor says:

    This information is priceless. When can I find out more?
    在线购买大麻用于XXX成人色情视频

  35. ShanepiliA's avatar ShanepiliA says:

    An outstanding share! I’ve just forwarded this onto a colleague who has been conducting a little research on this. And he actually ordered me dinner due to the fact that I stumbled upon it for him… lol. So allow me to reword this…. Thank YOU for the meal!! But yeah, thanks for spending the time to discuss this issue here on your site.
    viagra pills sexual xxx porn pills

  36. ShanepiliA's avatar ShanepiliA says:

    Hello, of course this paragraph is truly good and I have learned lot of things from it about blogging. thanks.
    شراء تادالافيل عبر الإنترنت لممارسة الجنس الشرجي xxx

  37. LewisGor's avatar LewisGor says:

    Hi just wanted to give you a brief heads up and let you know a few of the pictures aren’t loading correctly. I’m not sure why but I think its a linking issue. I’ve tried it in two different web browsers and both show the same results.
    adult xxx video porn site xxx sex video

  38. rajawd's avatar rajawd says:

    This web site is really a walk-through for all of the info you wanted about this and didn’t know who to ask. Glimpse here, and you’ll definitely discover it.

  39. EarnestCor's avatar EarnestCor says:

    Thank you for another excellent article. The place else may just anyone get that type of info in such an ideal method of writing? I have a presentation subsequent week, and I’m on the search for such information.
    viagra pills sexual xxx porn pills

  40. hello there and thank you for your info – I have certainly picked up anything new from right here. I did however expertise some technical issues using this website, as I experienced to reload the website a lot of times previous to I could get it to load properly. I had been wondering if your hosting is OK? Not that I’m complaining, but slow loading instances times will sometimes affect your placement in google and can damage your high quality score if ads and marketing with Adwords. Well I’m adding this RSS to my email and can look out for a lot more of your respective exciting content. Make sure you update this again very soon..

  41. Hi, I think your site might be having browser compatibility issues. When I look at your website in Safari, 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, fantastic blog!

  42. The following time I learn a blog, I hope that it doesnt disappoint me as much as this one. I mean, I know it was my choice to read, but I actually thought youd have something interesting to say. All I hear is a bunch of whining about one thing that you might fix for those who werent too busy searching for attention.

  43. SCAM's avatar SCAM says:

    I am curious to find out what blog system you have been utilizing?

    I’m experiencing some small security issues with my latest blog and I’d like to find something more safe.

    Do you have any recommendations?

  44. Normanstida's avatar Normanstida says:

    pharmacy fresh pharm pharmacy online

  45. WilliamPlato's avatar WilliamPlato says:

    Fresh Pharm no prescription pharmacy paypal

  46. rajawd's avatar rajawd says:

    Just wish to say your article is as astonishing. The clarity in your post is simply excellent and i could assume you are an expert on this subject. Fine with your permission allow me to grab your feed to keep up to date with forthcoming post. Thanks a million and please keep up the gratifying work.

Comments are closed.