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’m not that much of a internet reader to be honest but your blogs really nice, keep it up! I’ll go ahead and bookmark your site to come back later on. All the best

  2. Hello my family member! I wish to say that this article is awesome, great written and come with approximately all important infos. I’d like to look extra posts like this.

  3. olxtoto's avatar olxtoto says:

    I really appreciate this post. I have been looking everywhere for this! Thank goodness I found it on Bing. You’ve made my day! Thanks again

  4. olxtoto's avatar olxtoto says:

    Yeah bookmaking this wasn’t a bad determination outstanding post! .

  5. pcbmaster's avatar pcbmaster says:

    I keep listening to the newscast talk about getting free online grant applications so I have been looking around for the finest site to get one. Could you advise me please, where could i get some?

  6. olxtoto's avatar olxtoto says:

    Fascinating blog! Is your theme custom made or did you download it from somewhere? A design like yours with a few simple tweeks would really make my blog stand out. Please let me know where you got your theme. Thanks

  7. togel online's avatar togel online says:

    Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your point. You clearly know what youre talking about, why waste your intelligence on just posting videos to your blog when you could be giving us something informative to read?

  8. alexaslot138's avatar alexaslot138 says:

    Wonderful work! This is the type of information that should be shared around the net. Shame on Google for not positioning this post higher! Come on over and visit my website . Thanks =)

  9. susterslot's avatar susterslot says:

    you are really a good webmaster. The website loading speed is incredible. It seems that you’re doing any unique trick. Moreover, The contents are masterwork. you’ve done a magnificent job on this topic!

  10. olxtoto's avatar olxtoto says:

    Great post. I was checking constantly this blog and I am impressed! Very useful information particularly the last part 🙂 I care for such info a lot. I was looking for this particular information for a very long time. Thank you and best of luck.

  11. 973909 844371Should you happen to excited about eco items, sometimes be tough shock to anyone them recognise that to help make exclusive baskets just for this quite liquids carry basic steps liters associated ceiling fan oil producing. dc free of charge mommy blog giveaways family trip home gardening residence power wash baby laundry detergent 499130

  12. sa789's avatar sa789 says:

    335961 227130Would really like to perpetually get updated excellent blog ! . 272937

  13. situs slot's avatar situs slot says:

    I like what you guys are up too. Such clever work and reporting! Keep up the excellent works guys I have incorporated you guys to my blogroll. I think it will improve the value of my website :).

  14. I am perpetually thought about this, thankyou for posting.

  15. 44078 645817I undoubtedly did not realize that. Learnt something new appropriate now! Thanks for that. 977862

  16. kiteschool's avatar kiteschool says:

    610237 700900His or her shape of unrealistic tats were initially threatening. Lindsay utilized gun 1st basic, whereas this girl snuck outside by printer ink dog pen. I used absolutely sure the all truly on the shade, with the tattoo can be taken from the body shape. make an own temporary tattoo 319937

  17. 821434 302617Man you legend. return see my website, you must get pleasure from it. 290097

  18. Wow, awesome weblog layout! How long have you been running a blog for? you made blogging glance easy. The overall look of your site is fantastic, as smartly as the content material!

  19. 116871 41228I truly enjoy looking via on this web web site , it holds superb content material . 812897

  20. 368546 811283I genuinely enjoy searching through on this web site , it holds superb articles . 636349

  21. 739892 914634I like this website extremely much so a lot superb info . 574933

  22. 438143 621430I discovered your blog internet internet site on bing and appearance several of your early posts. Preserve up the quite very good operate. I just now additional the RSS feed to my MSN News Reader. Seeking toward reading far far more on your part down the road! 610115

  23. toto togel's avatar toto togel says:

    Dead composed subject matter, Really enjoyed reading.

  24. win678's avatar win678 says:

    I’ve been browsing online greater than three hours today, yet I by no means discovered any fascinating article like yours. It is pretty price sufficient for me. Personally, if all webmasters and bloggers made excellent content material as you did, the net shall be much more helpful than ever before.

  25. 888p's avatar 888p says:

    hi!,I like your writing so so much! proportion we keep up a correspondence extra about your post on AOL? I need a specialist in this house to solve my problem. Maybe that is you! Having a look forward to peer you.

  26. This design is spectacular! You obviously know how to keep a reader entertained. Between your wit and your videos, I was almost moved to start my own blog (well, almost…HaHa!) Fantastic job. I really loved what you had to say, and more than that, how you presented it. Too cool!

  27. Great write-up, I’m normal visitor of one’s web site, maintain up the excellent operate, and It’s going to be a regular visitor for a long time.

  28. 65590 594804Hello my family member! I wish to say that this post is incredible, fantastic written and come with approximately all vital infos. I would like to see extra posts like this . 31955

  29. as I website possessor I believe the content here is real great, thanks for your efforts.

  30. Heya i’m for the first time here. I found this board and I find It really useful & it helped me out much. I hope to give something back and help others like you helped me.

  31. prostavive's avatar prostavive says:

    Hello! I could have sworn I’ve been to this site before but after browsing through some of the post I realized it’s new to me. Nonetheless, I’m definitely glad I found it and I’ll be bookmarking and checking back often!

  32. I really thankful to find this website on bing, just what I was looking for : D as well bookmarked.

  33. Great post and straight to the point. I don’t know if this is really the best place to ask but do you guys have any ideea where to hire some professional writers? Thanks in advance 🙂

  34. 449298 3644Really fascinating topic , appreciate it for posting . 133924

  35. Saved as a favorite, I really like your blog!

  36. daftar pui68's avatar daftar pui68 says:

    Howdy, i read your blog occasionally and i own a similar one and i was just curious if you get a lot of spam comments? If so how do you reduce it, any plugin or anything you can suggest? I get so much lately it’s driving me insane so any support is very much appreciated.

  37. jalalive's avatar jalalive says:

    obviously like your web-site but you have to take a look at the spelling on quite a few of your posts. A number of them are rife with spelling issues and I in finding it very troublesome to inform the truth nevertheless I will surely come back again.

  38. ughoki's avatar ughoki says:

    You completed a number of good points there. I did a search on the topic and found most people will have the same opinion with your blog.

  39. It¦s actually a cool and helpful piece of info. I¦m glad that you just shared this useful information with us. Please stay us informed like this. Thanks for sharing.

  40. I’m not that much of a online reader to be honest but your blogs really nice, keep it up! I’ll go ahead and bookmark your website to come back later. All the best

  41. Just desire to say your article is as astounding. The clearness for your publish is just nice and i can think you’re a professional in this subject. Well along with your permission let me to clutch your RSS feed to keep updated with approaching post. Thanks one million and please keep up the rewarding work.

  42. The core of your writing while sounding agreeable originally, did not sit very well with me personally after some time. Someplace within the sentences you actually managed to make me a believer unfortunately just for a very short while. I however have got a problem with your leaps in logic and you would do nicely to help fill in those breaks. When you can accomplish that, I could surely end up being fascinated.

  43. F*ckin’ amazing things here. I am very glad to look your article. Thanks a lot and i’m looking forward to touch you. Will you kindly drop me a mail?

  44. 492559 540255Thank you for the auspicious writeup. It in reality was a amusement account it. Appear complicated to far delivered agreeable from you! Nonetheless, how can we maintain in touch? 274143

  45. I genuinely enjoy studying on this website , it contains good posts.

  46. I’ve been absent for some time, but now I remember why I used to love this blog. Thanks, I will try and check back more frequently. How frequently you update your site?

  47. I would like to thank you for the efforts you’ve put in writing this blog. I am hoping the same high-grade site post from you in the upcoming also. Actually your creative writing skills has inspired me to get my own web site now. Really the blogging is spreading its wings fast. Your write up is a great example of it.

  48. Good write-up, I am regular visitor of one?¦s web site, maintain up the nice operate, and It’s going to be a regular visitor for a lengthy time.

  49. Would you be concerned about exchanging links?

  50. 116193 531230Merely wanna input that you have a extremely good web internet site , I really like the pattern it really stands out. 734668

Comments are closed.