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. I would like to convey my affection for your generosity in support of persons who absolutely need guidance on this one situation. Your real dedication to passing the message all through has been especially valuable and have consistently enabled most people like me to achieve their ambitions. Your new warm and helpful guidelines entails a lot to me and especially to my office workers. Best wishes; from each one of us.

  2. OLanepiliA's avatar OLanepiliA says:

    Its like you read my mind! You seem to know a lot about this, like you wrote the book in it or something. I think that you could do with a few pics to drive the message home a little bit, but other than that, this is excellent blog. A fantastic read. I will certainly be back.
    buy viagra sexual porno xxx adults pills

  3. Andreimiva's avatar Andreimiva says:

    Pharmacological Sciences Research Institute Pharmacological Sciences Pharmacological Institute

  4. Whitneysnolf's avatar Whitneysnolf says:

    where can i buy ed pills Pharmacological Sciences ed medicines

  5. paman empire's avatar paman empire says:

    Someone necessarily assist to make significantly posts I would state. This is the first time I frequented your web page and so far? I amazed with the analysis you made to create this actual publish incredible. Wonderful process!

  6. Very good written post. It will be helpful to anybody who utilizes it, as well as yours truly :). Keep up the good work – i will definitely read more posts.

  7. app insights's avatar app insights says:

    Does your blog have a contact page? I’m having problems locating it but, I’d like to shoot you an e-mail. I’ve got some creative ideas for your blog you might be interested in hearing. Either way, great blog and I look forward to seeing it grow over time.

  8. Pretty section of content. I just stumbled upon your site and in accession capital to assert that I acquire in fact enjoyed account your blog posts. Any way I will be subscribing to your feeds and even I achievement you access consistently rapidly.

  9. I’ve recently started a web site, the info you offer on this web site has helped me tremendously. Thank you for all of your time & work. “Never trust anybody who says ‘trust me.’ Except just this once, of course. – from Steel Beach” by John Varley.

  10. Arthurnor's avatar Arthurnor says:

    erectile dysfunction pills for sale: cheap ed meds – where can i get ed pills

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

  12. Hey! I could have sworn I’ve been to this website before but after reading through some of the post I realized it’s new to me. Anyhow, I’m definitely delighted I found it and I’ll be book-marking and checking back often!

  13. pgdemo's avatar pgdemo says:

    Carabao Cup live scores, English League Cup with Premier League teams playing

  14. Hectorzenna's avatar Hectorzenna says:

    Order Viagra 50 mg online: NovaMen Pharmacy – viagra canada

  15. Thanks for one’s marvelous posting! I actually enjoyed reading it, you are a great author.I will make sure to bookmark your blog and definitely will come back someday. I want to encourage you to ultimately continue your great job, have a nice morning!

  16. Hectorzenna's avatar Hectorzenna says:

    CoreVital Pharmacy: Cialis without a doctor prescription – Buy Tadalafil 5mg

  17. Throughout the awesome scheme of things you receive a B+ for effort. Exactly where you confused us was in all the particulars. As as the maxim goes, the devil is in the details… And it could not be more accurate right here. Having said that, allow me say to you exactly what did deliver the results. Your text is definitely pretty engaging and that is probably the reason why I am taking the effort to opine. I do not really make it a regular habit of doing that. Secondly, although I can certainly notice the leaps in logic you come up with, I am not sure of how you appear to connect your points which help to make the actual conclusion. For the moment I will yield to your point however wish in the near future you actually link your dots better.

  18. pria4d link's avatar pria4d link says:

    Useful information. Fortunate me I discovered your website unintentionally, and I am stunned why this accident did not happened earlier! I bookmarked it.

  19. Thomassnice's avatar Thomassnice says:

    top online pharmacy india: TrustRx India – mail order pharmacy india

  20. Patrickfuh's avatar Patrickfuh says:

    MedBridge Pharmacy: MedBridge Pharmacy – no script pharmacy

  21. Thomassnice's avatar Thomassnice says:

    pharmacy in mexico: MedicoBridge RX – mexican pharmacies that ship to us

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

  23. Thomassnice's avatar Thomassnice says:

    top 10 online pharmacy in india: top 10 online pharmacy in india – top online pharmacy india

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

  25. PatrickZoolf's avatar PatrickZoolf says:

    online pharmacy drop shipping: VitalCore Pharmacy – canadapharmacyonline com

  26. neurodyne's avatar neurodyne says:

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

  27. AnthonyJonry's avatar AnthonyJonry says:

    pet pharmacy USA pet pharmacy USA pet pharmacy USA

  28. I got what you mean , thankyou for posting.Woh I am pleased to find this website through google.

  29. RichardGit's avatar RichardGit says:

    professional pharmacy: VitalCore Pharmacy – canada drugs online reviews

  30. Youre so cool! I dont suppose Ive learn anything like this before. So nice to find any person with some original ideas on this subject. realy thank you for beginning this up. this web site is something that’s wanted on the internet, someone with a little bit originality. useful job for bringing one thing new to the web!

  31. AnthonyJonry's avatar AnthonyJonry says:

    pet drugs online pet antibiotics and care pet pharmacy USA

  32. I have been browsing online greater than 3 hours these days, but I never found any fascinating article like yours. It is pretty price sufficient for me. In my view, if all website owners and bloggers made just right content material as you probably did, the internet will probably be much more useful than ever before.

  33. RichardGit's avatar RichardGit says:

    pet antibiotics and care: pet rx – dog and cat medicine supply

  34. Normally I do not 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 surprised me. Thanks, very nice article.

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

  36. PatrickZoolf's avatar PatrickZoolf says:

    pharmacy open near me: Generic Viagra online – canadian pharmacy without prescription

  37. RichardGit's avatar RichardGit says:

    ed prescription online: True Health Pharm – ed medications cost

  38. What’s Taking place i’m new to this, I stumbled upon this I have found It positively helpful and it has helped me out loads. I’m hoping to contribute & aid other customers like its aided me. Great job.

  39. Howdy very cool web site!! Guy .. Excellent .. Amazing .. I will bookmark your website and take the feeds additionally…I am satisfied to seek out numerous helpful info here within the publish, we’d like develop extra techniques on this regard, thanks for sharing. . . . . .

  40. PatrickZoolf's avatar PatrickZoolf says:

    pet antibiotics and care: dog and cat medicine supply – pet pharmacy USA

  41. dewi11 slot's avatar dewi11 slot says:

    Great website. Lots of helpful information here. I’m sending it to a few buddies ans also sharing in delicious. And naturally, thanks to your effort!

  42. dewi11 slot's avatar dewi11 slot says:

    After examine a couple of of the blog posts in your website now, and I actually like your method of blogging. I bookmarked it to my bookmark web site list and might be checking again soon. Pls take a look at my website online as well and let me know what you think.

Comments are closed.