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

** Note ** Jesse is currently looking for a new position. You can learn more about him at https://jesseliberty.bio Thank you. 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. His latest book, Building APIs with .NET, is now available wherever you buy your books. Liberty 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 13 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. Lupe Faeth's avatar Lupe Faeth says:

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

  2. GeorgeIdiob's avatar GeorgeIdiob says:

    mexican pharmacies: mexico online pharmacy – farmacias mexicanas

  3. After going over a few of the blog posts on your website, I honestly like your way of writing a blog. I added it to my bookmark webpage list and will be checking back soon. Please visit my web site too and let me know how you feel.

  4. ShanepiliA's avatar ShanepiliA says:

    This article serves as a fantastic starting point for anyone looking to get acquainted with the material, and your clear formatting ensures that the main takeaways are immediately visible to the audience.

    kinghills

  5. Enoch Routh's avatar Enoch Routh says:

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

  6. Kathie Doble's avatar Kathie Doble says:

    Aw, this was an extremely nice post. Spending some time and actual effort to generate a great article… but what can I say… I put things off a whole lot and never manage to get anything done.

  7. Greate article. Keep writing such kind of info on your site. Im really impressed by your site.

  8. Hiya, I am really glad I have found this info. Nowadays bloggers publish just about gossips and web and this is actually annoying. A good site with exciting content, that’s what I need. Thanks for keeping this web-site, I will be visiting it. Do you do newsletters? Can’t find it.

  9. phising site's avatar phising site says:

    I was very pleased to find this web-site.I wanted to thanks for your time for this wonderful read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you blog post.

  10. 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. Its excellent as your other content : D, regards for posting. “Love is like an hourglass, with the heart filling up as the brain empties.” by Jules Renard.

  12. Rattling nice style and fantastic content material, practically nothing else we require :D.

  13. Everything is very open and very clear explanation of issues. was truly information. Your website is very useful. Thanks for sharing.

  14. Right now it looks like Movable Type is the best blogging platform available right now. (from what I’ve read) Is that what you are using on your blog?

  15. Aw, this was an extremely good post. Taking the time and actual effort to produce a superb article… but what can I say… I procrastinate a whole lot and never manage to get anything done.

  16. Mable Allan's avatar Mable Allan 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. Wow that was unusual. 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. Anyway, just wanted to say great blog!

  18. judi bola's avatar judi bola says:

    Usually I don’t read post on blogs, but I would like to say that this write-up very forced me to try and do so! Your writing style has been amazed me. Thanks, quite nice article.

Comments are closed.