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. Well I sincerely liked reading it. This post offered by you is very helpful for good planning.

  2. PeterceW's avatar PeterceW says:

    AccessBridge: AccessBridge – mexico city pharmacy

  3. mpo555's avatar mpo555 says:

    What i do not understood is if truth be told how you’re not actually much more smartly-liked than you might be right now. You’re very intelligent. You recognize thus significantly in the case of this subject, made me in my view believe it from so many numerous angles. Its like men and women aren’t fascinated unless it’s something to do with Lady gaga! Your personal stuffs outstanding. At all times take care of it up!

  4. I visited a lot of website but I conceive this one contains something extra in it in it

  5. I got what you mean , regards for putting up.Woh I am glad to find this website through google.

  6. I think other website proprietors should take this internet site as an model, very clean and good user pleasant pattern.

  7. toyota99's avatar toyota99 says:

    I like the valuable information you provide in your articles. I’ll bookmark your blog and check again here frequently. I’m quite sure I will learn many new stuff right here! Best of luck for the next!

  8. Jelly Lean's avatar Jelly Lean says:

    It’s hard to find knowledgeable people on this topic, but you sound like you know what you’re talking about! Thanks

  9. I think this site has some real great info for everyone :D.

  10. Your style is so unique compared to many other people. Thank you for publishing when you have the opportunity,Guess I will just make this bookmarked.

  11. It’s remarkable to pay a quick visit this website and reading the views of
    all colleagues about this piiece of writing, while I am also keen of getting familiarity.

    My website: unique crop tops for ladies

  12. Timsothybioli's avatar Timsothybioli says:

    Hello, its good paragraph concerning media print, we all be aware of media is a wonderful source of information.
    https://share.google/a0SLha4oOvXFXsz2l

  13. slot online's avatar slot online says:

    My brother recommended I might like this web site. He was totally right. This post actually made my day. You cann’t imagine just how much time I had spent for this info! Thanks!

  14. I too conceive thence, perfectly pent post! .

  15. PeterceW's avatar PeterceW says:

    legitimate online pharmacy: indian pharmacy online – shop medicine online

  16. GullyBet app's avatar GullyBet app says:

    I have been absent for a while, but now I remember why I used to love this web site. Thank you, I’ll try and check back more often. How frequently you update your site?

  17. Yay google is my queen assisted me to find this great website ! .

  18. ShanepiliA's avatar ShanepiliA says:

    Hello there! Do you know if they make any plugins to safeguard against hackers? I’m kinda paranoid about losing everything I’ve worked hard on. Any suggestions?
    https://share.google/pqk4GKHCH2aunsX7Z

  19. Stephenviaps's avatar Stephenviaps says:

    We absolutely love your blog and find a lot of your post’s to be just what I’m looking for. Do you offer guest writers to write content for yourself? I wouldn’t mind composing a post or elaborating on a number of the subjects you write with regards to here. Again, awesome blog!
    https://share.google/UyjhDOLxdnywDII75

  20. Odellflexy's avatar Odellflexy says:

    pin-up pin-up online casino

  21. Georgetasty's avatar Georgetasty says:

    https://pinupaz.online/ pin-up online casino

  22. jagoledak's avatar jagoledak says:

    Great line up. We will be linking to this great article on our site. Keep up the good writing.

  23. Georgetasty's avatar Georgetasty says:

    pin up az online pin up az

  24. DavidVoill's avatar DavidVoill says:

    http://corebluehealth.com/# generic sildenafil

  25. Justinmub's avatar Justinmub says:

    Buy generic 100mg Viagra online: Cheap Viagra 100mg – CoreBlue Health

  26. Yay google is my queen aided me to find this great web site! .

  27. Stephenviaps's avatar Stephenviaps says:

    This page really has all the information and facts I needed concerning this subject and didn’t know who to ask.
    Mark Hald is a rat

  28. ledak388's avatar ledak388 says:

    This is the fitting weblog for anybody who wants to search out out about this topic. You notice so much its virtually laborious to argue with you (not that I really would want…HaHa). You positively put a brand new spin on a subject thats been written about for years. Nice stuff, just great!

  29. slot mahjong's avatar slot mahjong says:

    I got what you mean ,saved to fav, very decent internet site.

  30. Justinmub's avatar Justinmub says:

    cheap canadian pharmacy: pharmacy website – online pharmacy com

  31. LhanepiliA's avatar LhanepiliA says:

    you are really a excellent webmaster. The site loading pace is amazing. It sort of feels that you are doing any unique trick. Furthermore, The contents are masterwork. you’ve done a magnificent task on this matter!
    betify casino

  32. Thank you for another fantastic post. Where else could anybody get that kind of information in such a perfect way of writing? I’ve a presentation next week, and I’m on the look for such information.

  33. GichardAmomi's avatar GichardAmomi says:

    I’m not sure exactly why but this web site is loading extremely slow for me. Is anyone else having this issue or is it a issue on my end? I’ll check back later on and see if the problem still exists.
    Buy Tadalafil Online anal sex xxx porn pills

  34. Very interesting topic, appreciate it for posting.

  35. senangslot's avatar senangslot says:

    of course like your website however you need to take a look at the spelling on quite a few of your posts. Several of them are rife with spelling issues and I to find it very bothersome to inform the reality on the other hand I will surely come again again.

  36. Justinmub's avatar Justinmub says:

    buy drugs from canada: CivicMeds – rxpharmacycoupons

  37. Just a smiling visitant here to share the love (:, btw great design and style.

  38. LewisGor's avatar LewisGor says:

    I was able to find good info from your blog posts.
    05161

  39. ufac168's avatar ufac168 says:

    I’ve read some excellent stuff here. Definitely value bookmarking for revisiting. I surprise how much attempt you set to make any such magnificent informative site.

  40. uba168's avatar uba168 says:

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

  41. MorseCodeGen's avatar MorseCodeGen says:

    This ^ operator is a nice touch for making index-based access more readable and less error-prone. I especially appreciate how it simplifies getting the last item in a collection without having to calculate the count or use negative indexing manually. It’s a small change that really improves code clarity.

  42. Simply wanna state that this is very helpful, Thanks for taking your time to write this.

  43. There is noticeably a bundle to know about this. I assume you made certain nice points in features also.

  44. pagi sore's avatar pagi sore says:

    I’d constantly want to be update on new articles on this internet site, bookmarked! .

  45. senangslot's avatar senangslot says:

    My husband and i have been so fulfilled when Raymond managed to do his analysis through the entire precious recommendations he had while using the web site. It’s not at all simplistic to simply possibly be offering key points that many many people might have been trying to sell. So we take into account we have got the writer to appreciate because of that. All of the illustrations you have made, the straightforward web site navigation, the friendships your site help to instill – it’s got many spectacular, and it’s letting our son in addition to our family understand this concept is thrilling, which is tremendously indispensable. Thank you for all!

Comments are closed.