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

    https://pregabalin.life/# lyrica medication

  2. Vivienne's avatar Vivienne says:

    May I simply say what a comfort to find an individual who actually knows what they are discussing on the net. You actually know how to bring a problem to light and make it important. More and more people should check this out and understand this side of the story. It’s surprising you’re not more popular since you definitely have the gift.

  3. Qiana's avatar Qiana says:

    May I simply just say what a relief to uncover somebody who genuinely knows what they are discussing over the internet. You definitely understand how to bring an issue to light and make it important. More people really need to read this and understand this side of the story. I was surprised you are not more popular because you definitely have the gift.

  4. bos138 login's avatar bos138 login says:

    Hello, you used to write fantastic, but the last several posts have been kinda boringK I miss your great writings. Past several posts are just a little bit out of track! come on!

  5. Elfriede's avatar Elfriede says:

    Excellent pieces. Keep writing such kind of info on your page. Im really impressed by your blog.

  6. Fernande's avatar Fernande says:

    Wow that was odd. I just wrote an extremely long comment but after I clicked submit my comment didn’t appear. Grrrr… well I’m not writing all that over again. Regardless, just wanted to say fantastic blog!

  7. Jennette's avatar Jennette says:

    Excellent post. Keep posting such kind of information on your site. Im really impressed by your blog.

  8. Domonique's avatar Domonique says:

    Excellent post. Keep posting such kind of info on your page. Im really impressed by your site.

  9. Tamra's avatar Tamra says:

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

  10. Claretha's avatar Claretha says:

    Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog that automatically tweet my newest twitter updates. I’ve been looking for a plug-in like this for quite some time and was hoping maybe you would have some experience with something like this. Please let me know if you run into anything. I truly enjoy reading your blog and I look forward to your new updates.

  11. Maybell's avatar Maybell says:

    Hello there, You’ve performed a fantastic job. I will definitely digg it and for my part suggest to my friends. I’m confident they’ll be benefited from this site.

  12. Gladis's avatar Gladis says:

    Can I just say what a comfort to find somebody who genuinely understands what they’re discussing on the net. You actually know how to bring a problem to light and make it important. More people should look at this and understand this side of the story. It’s surprising you are not more popular because you definitely have the gift.

  13. Richardweivy's avatar Richardweivy says:

    https://pregabalin.life/# buy generic lyrica

  14. Kelle's avatar Kelle says:

    Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog that automatically tweet my newest twitter updates. I’ve been looking for a plug-in like this for quite some time and was hoping maybe you would have some experience with something like this. Please let me know if you run into anything. I truly enjoy reading your blog and I look forward to your new updates.

  15. Hi there, You’ve done an incredible job. I’ll certainly digg it and personally suggest to my friends. I am confident they’ll be benefited from this website.

  16. Neta's avatar Neta says:

    Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog that automatically tweet my newest twitter updates. I’ve been looking for a plug-in like this for quite some time and was hoping maybe you would have some experience with something like this. Please let me know if you run into anything. I truly enjoy reading your blog and I look forward to your new updates.

  17. Palma's avatar Palma says:

    Excellent article. Keep writing such kind of information on your page. Im really impressed by your blog.

  18. Deandra's avatar Deandra says:

    Can I simply just say what a relief to uncover someone who genuinely understands what they are discussing over the internet. You actually know how to bring an issue to light and make it important. More people ought to look at this and understand this side of your story. I was surprised that you aren’t more popular because you most certainly possess the gift.

  19. Deana's avatar Deana says:

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

  20. olxototo's avatar olxototo says:

    Thanks for every other informative website. Where else may I am getting that type of info written in such an ideal method? I’ve a project that I am simply now operating on, and I have been at the look out for such information.

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

  22. Have you ever considered publishing an e-book or guest authoring on other blogs? I have a blog centered on the same ideas you discuss and would really like to have you share some stories/information. I know my readers would value your work. If you’re even remotely interested, feel free to shoot me an e-mail.

  23. WilliamAdhed's avatar WilliamAdhed says:

    https://mexicoexpharm.click/# pharmacies in mexico that ship to the us

  24. Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog that automatically tweet my newest twitter updates. I’ve been looking for a plug-in like this for quite some time and was hoping maybe you would have some experience with something like this. Please let me know if you run into anything. I truly enjoy reading your blog and I look forward to your new updates.

  25. 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.

  26. olxtoto link's avatar olxtoto link says:

    Enjoyed reading through this, very good stuff, thanks. “Nothing happens to any thing which that thing is not made by nature to bear.” by Marcus Aurelius Antoninus.

  27. Davidfus's avatar Davidfus says:

    visir apotek: Svenska Pharma – boka pcr test apotek

  28. 必赢网站's avatar 必赢网站 says:

    Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog that automatically tweet my newest twitter updates. I’ve been looking for a plug-in like this for quite some time and was hoping maybe you would have some experience with something like this. Please let me know if you run into anything. I truly enjoy reading your blog and I look forward to your new updates.

  29. Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog that automatically tweet my newest twitter updates. I’ve been looking for a plug-in like this for quite some time and was hoping maybe you would have some experience with something like this. Please let me know if you run into anything. I truly enjoy reading your blog and I look forward to your new updates.

  30. Right now it sounds like Expression Engine is the top blogging platform out there right now. (from what I’ve read) Is that what you are using on your blog?

  31. Excellent post. Keep writing such kind of info on your site. Im really impressed by your site.

  32. Very interesting info!Perfect just what I was looking for!

  33. 立博下载's avatar 立博下载 says:

    Right now it looks like WordPress is the preferred blogging platform out there right now. (from what I’ve read) Is that what you are using on your blog?

  34. Aw, this was an exceptionally nice post. Taking a few minutes and actual effort to generate a really good article… but what can I say… I put things off a whole lot and never manage to get anything done.

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

  36. Excellent article. Keep writing such kind of info on your blog. Im really impressed by your blog.

  37. Aw, this was a very nice post. Taking a few minutes and actual effort to make a really good article… but what can I say… I hesitate a lot and never manage to get anything done.

  38. Currently it appears 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?

  39. Wow that was unusual. I just wrote an incredibly long comment but after I clicked submit my comment didn’t show up. Grrrr… well I’m not writing all that over again. Anyways, just wanted to say fantastic blog!

  40. Hey very cool web site!! Man .. Excellent .. Amazing .. I will bookmark your web site and take the feeds also…I am happy to find numerous useful info here in the post, we need develop more techniques in this regard, thanks for sharing. . . . . .

  41. After all, what a great site and informative posts, I will upload inbound link – bookmark this web site? Regards, Reader.

  42. Can I simply just say what a relief to uncover someone that genuinely knows what they are talking about on the web. You definitely understand how to bring an issue to light and make it important. A lot more people need to read this and understand this side of the story. I was surprised that you are not more popular since you most certainly have the gift.

  43. Hello! 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. Many thanks

  44. Hello there, You have done an incredible job. I’ll certainly digg it and in my view suggest to my friends. I am sure they will be benefited from this web site.

  45. 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.

  46. Hi there, You have performed an excellent job. I’ll certainly digg it and personally suggest to my friends. I am sure they will be benefited from this web site.

  47. Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog that automatically tweet my newest twitter updates. I’ve been looking for a plug-in like this for quite some time and was hoping maybe you would have some experience with something like this. Please let me know if you run into anything. I truly enjoy reading your blog and I look forward to your new updates.

  48. Travisascex's avatar Travisascex says:

    canadian pharmacy: canadian pharmacy – canadian prescription pharmacy

  49. Currently it appears like Expression Engine is the preferred blogging platform out there right now. (from what I’ve read) Is that what you are using on your blog?

  50. Currently it seems like BlogEngine is the best blogging platform out there right now. (from what I’ve read) Is that what you are using on your blog?

Comments are closed.