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

    Great article and right to the point. I don’t know if this is truly the best place to ask but do you folks have any thoughts on where to employ some professional writers? Thank you 🙂

  2. You are my inspiration, I possess few blogs and often run out from post :). “‘Tis the most tender part of love, each other to forgive.” by John Sheffield.

  3. olxtoto's avatar olxtoto says:

    Hello, i think that i saw you visited my weblog thus i came to “return the favor”.I’m attempting to find things to enhance my web site!I suppose its ok to use a few of your ideas!!

  4. I genuinely appreciate your work, Great post.

  5. olxtoto's avatar olxtoto says:

    Heya i am for the first time here. I found this board and I to find It really useful & it helped me out much. I am hoping to provide something again and help others such as you aided me.

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

  7. Thank you for sharing with us, I believe this website truly stands out : D.

  8. Hi! I’ve been following your blog for a while now and finally got the courage to go ahead and give you a shout out from New Caney Texas! Just wanted to say keep up the excellent work!

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

  10. excellent post, very informative. I wonder why the other specialists of this sector do not notice this. You must continue your writing. I’m sure, you’ve a huge readers’ base already!

  11. I?¦ll right away snatch your rss as I can’t to find your email subscription hyperlink or e-newsletter service. Do you’ve any? Please let me recognise so that I may subscribe. Thanks.

  12. slot gacor's avatar slot gacor says:

    It’s really a great and useful piece of info. I am satisfied that you just shared this helpful info with us. Please keep us informed like this. Thank you for sharing.

  13. I think this is one of the so much important information for me. And i am satisfied reading your article. But want to observation on few common issues, The web site style is perfect, the articles is in reality nice : D. Just right job, cheers

  14. dolantoto88's avatar dolantoto88 says:

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

  15. I like this blog its a master peace ! Glad I detected this on google .

  16. dolantoto's avatar dolantoto says:

    Somebody necessarily help to make critically articles I’d state. This is the very first time I frequented your web page and to this point? I amazed with the research you made to make this particular put up amazing. Fantastic job!

  17. togel macau's avatar togel macau says:

    Great post. I am facing a couple of these problems.

  18. situs togel's avatar situs togel says:

    Pretty nice post. I just stumbled upon your weblog and wished to say that I’ve truly enjoyed browsing your blog posts. In any case I will be subscribing to your rss feed and I hope you write again very soon!

  19. Greetings! This is my first visit to your blog! We are a collection of volunteers and starting a new project in a community in the same niche. Your blog provided us valuable information to work on. You have done a marvellous job!

  20. Its great as your other blog posts : D, thankyou for posting.

  21. pg slot's avatar pg slot says:

    Greetings from Los angeles! I’m bored to death at work so I decided to browse your website on my iphone during lunch break. I enjoy the info you present here and can’t wait to take a look when I get home. I’m amazed at how fast your blog loaded on my cell phone .. I’m not even using WIFI, just 3G .. Anyways, great blog!

  22. kaka hoki's avatar kaka hoki says:

    Pretty component of content. I just stumbled upon your site and in accession capital to claim that I get in fact enjoyed account your blog posts. Any way I’ll be subscribing to your augment or even I success you access constantly quickly.

  23. I have been browsing online more than three hours as of late, but I never found any attention-grabbing article like yours. It is pretty value sufficient for me. In my opinion, if all site owners and bloggers made good content as you probably did, the net shall be a lot more useful than ever before. “I thank God for my handicaps, for through them, I have found myself, my work and my God.” by Hellen Keller.

  24. kakahoki's avatar kakahoki says:

    Good blog! I truly love how it is easy on my eyes and the data are well written. I am wondering how I might be notified when a new post has been made. I’ve subscribed to your RSS which must do the trick! Have a nice day!

  25. netizen303's avatar netizen303 says:

    Yay google is my world beater aided me to find this great web site! .

  26. nagaempire's avatar nagaempire says:

    Nice post. I learn something more challenging on different blogs everyday. It will always be stimulating to read content from other writers and practice a little something from their store. I’d prefer to use some with the content on my blog whether you don’t mind. Natually I’ll give you a link on your web blog. Thanks for sharing.

  27. Wow! Thank you! I always needed to write on my website something like that. Can I take a part of your post to my site?

  28. Hey! This is my 1st comment here so I just wanted to give a quick shout out and tell you I truly enjoy reading your posts. Can you suggest any other blogs/websites/forums that deal with the same subjects? Thanks for your time!

  29. Tired of Tinnitus or Hearing Problems? Here’s a Natural Solution That’s Changing Lives Are you constantly bothered by ringing in the ears (tinnitus), hearing difficulties, or sound clarity issues?

  30. I am often to blogging and i really appreciate your content. The article has really peaks my interest. I am going to bookmark your site and keep checking for new information.

  31. What i do not understood is if truth be told how you are no longer actually much more smartly-liked than you may be right now. You are so intelligent. You recognize therefore significantly with regards to this topic, produced me in my view consider it from a lot of numerous angles. Its like men and women don’t seem to be interested unless it’s one thing to do with Woman gaga! Your personal stuffs great. At all times maintain it up!

  32. LottoChamp is a specialized lottery prediction software designed to give players a statistical edge. Instead of relying on luck or random numbers, the platform uses mathematical algorithms and historical draw data to help users make more strategic number selections.

  33. You are my intake, I possess few blogs and very sporadically run out from to brand.

  34. PROSTADINE is truly unlike anything you’ve ever encountered in your life. It stands as the sole droplet solution that incorporates nine potent natural ingredients, working in perfect synergy to safeguard the health and mineral balance of your prostate well into your golden years.

  35. 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 by no means appear to get one thing done.

  36. alexaslot138's avatar alexaslot138 says:

    I’m still learning from you, as I’m trying to reach my goals. I definitely liked reading everything that is posted on your blog.Keep the stories coming. I enjoyed it!

  37. alexaslot138's avatar alexaslot138 says:

    You really make it appear so easy with your presentation but I find this topic to be really one thing which I think I’d by no means understand. It seems too complicated and extremely broad for me. I am looking forward for your next submit, I will try to get the dangle of it!

  38. The ice water hack is a simple trick that involves using cold water to naturally activate your body’s thermogenic process. When you drink ice water, your body has to work harder to regulate its temperature, burning extra calories in the process.

  39. alexaslot138's avatar alexaslot138 says:

    When I initially commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get four emails with the same comment. Is there any way you can remove people from that service? Bless you!

  40. Many men who have tried the blue salt trick report increased stamina, better performance, and a renewed sense of confidence.

  41. siokambing5's avatar siokambing5 says:

    Of course, what a splendid blog and illuminating posts, I will bookmark your blog.All the Best!

  42. Excellent read, I just passed this onto a colleague who was doing some research on that. And he just bought me lunch since I found it for him smile So let me rephrase that: Thank you for lunch! “How beautiful maleness is, if it finds its right expression.” by D. H. Lawrence.

  43. Sleep Lean is a natural nighttime weight management supplement designed to help you burn fat, improve sleep quality, and balance hormones—all while you rest.

  44. pausempire's avatar pausempire says:

    Its like you read my mind! You seem to know so much approximately this, like you wrote the ebook in it or something. I believe that you can do with a few p.c. to drive the message house a little bit, but instead of that, that is great blog. An excellent read. I’ll definitely be back.

  45. I’ve been surfing online greater than three hours lately, yet I never found any interesting article like yours. It is pretty worth sufficient for me. Personally, if all web owners and bloggers made just right content as you probably did, the web will probably be a lot more useful than ever before. “Truth is not determined by majority vote.” by Doug Gwyn.

  46. The Pink Salt Trick is a minimalist but effective morning routine: Just drink a glass of lukewarm water mixed with a pinch of Himalayan pink salt as soon as you wake up.

  47. Thank you for sharing excellent informations. Your web site is very cool. I’m impressed by the details that you have on this web site. It reveals how nicely you perceive this subject. Bookmarked this web page, will come back for more articles. You, my friend, ROCK! I found just the info I already searched all over the place and simply could not come across. What an ideal web-site.

  48. Mitolyn's avatar Mitolyn says:

    certainly like your web site however you need to check the spelling on quite a few of your posts. Several of them are rife with spelling issues and I in finding it very bothersome to tell the reality however I’ll certainly come back again.

  49. HepatoBurn's avatar HepatoBurn says:

    This blog is definitely rather handy since I’m at the moment creating an internet floral website – although I am only starting out therefore it’s really fairly small, nothing like this site. Can link to a few of the posts here as they are quite. Thanks much. Zoey Olsen

  50. data macau's avatar data macau says:

    Merely a smiling visitor here to share the love (:, btw outstanding layout. “Better by far you should forget and smile than that you should remember and be sad.” by Christina Georgina Rossetti.

Comments are closed.