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

    An interesting discussion is worth comment. I feel that it is best to write extra on this topic, it won’t be a taboo subject but generally persons are not sufficient to speak on such topics. To the next. Cheers

  2. kopitogel's avatar kopitogel says:

    As a Newbie, I am always exploring online for articles that can help me. Thank you

  3. nervital's avatar nervital says:

    Thank you, I’ve just been looking for information about this topic for a long time and yours is the greatest I have discovered till now. However, what about the bottom line? Are you positive concerning the source?

  4. kopitogel's avatar kopitogel says:

    Awsome blog! I am loving it!! Will come back again. I am taking your feeds also.

  5. I as well believe therefore, perfectly indited post! .

  6. Hi there! This post couldn’t be written any better! Reading through this post reminds me of my previous room mate! He always kept talking about this. I will forward this article to him. Pretty sure he will have a good read. Thank you for sharing!

  7. I love your writing style genuinely enjoying this internet site.

  8. I enjoy you because of your whole labor on this website. Ellie loves managing research and it is easy to see why. My partner and i know all about the lively method you make important strategies via your web blog and as well encourage response from others on that situation while our girl has always been becoming educated a lot. Enjoy the rest of the year. You are doing a tremendous job.

  9. 308075 909945excellent issues altogether, you merely gained a new reader. What could you recommend about your post that you created some days in the past? Any positive? 76097

  10. Jav's avatar Jav says:

    39507 642651Hi, ich habe Ihre Webseite bei der Suche nach Fernbus Hamburg im Internet gefunden. Schauen Sie doch mal auf meiner Seite vorbei, ich habe dort viele Testberichte zu den aktuellen Windeleimern geschrieben. 529566

  11. 599382 649607Can I just say what a relief to search out somebody who actually is aware of what theyre speaking about on the internet. You undoubtedly know how to deliver a issue to light and make it essential. Extra folks want to learn this and perceive this facet of the story. I cant consider youre no more common because you positively have the gift. 636919

  12. nagaempire's avatar nagaempire says:

    Very interesting points you have remarked, appreciate it for putting up.

  13. PH22's avatar PH22 says:

    Thanks for the post, can I set it up so I get an alert email whenever you write a fresh article?

  14. jwin bet's avatar jwin bet says:

    Very interesting topic, thanks for putting up. “Not by age but by capacity is wisdom acquired.” by Titus Maccius Plautus.

  15. I’m not sure exactly why but this website is loading very 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.

  16. 271120 860957After examine a couple of with the weblog posts inside your internet site now, and I truly like your manner of blogging. I bookmarked it to my bookmark web site list and may be checking back soon. Pls take a appear at my website as effectively and let me know what you believe. 849655

  17. As soon as I noticed this site I went on reddit to share some of the love with them.

  18. rajabotak's avatar rajabotak says:

    I like this post, enjoyed this one appreciate it for posting. “When you make a world tolerable for yourself, you make a world tolerable for others.” by Anais Nin.

  19. densustoto's avatar densustoto says:

    Having read this I thought it was very informative. I appreciate you taking the time and effort to put this article together. I once again find myself spending way to much time both reading and commenting. But so what, it was still worth it!

  20. komik hentai's avatar komik hentai says:

    Hiya! I know this is kinda off topic but I’d figured I’d ask. Would you be interested in trading links or maybe guest writing a blog post or vice-versa? My website discusses a lot of the same topics as yours and I think we could greatly benefit from each other. If you might be interested feel free to shoot me an email. I look forward to hearing from you! Excellent blog by the way!

  21. I think this site holds some very excellent info for everyone :D. “Nothing surely is so disgraceful to society and to individuals as unmeaning wastefulness.” by Count Benjamin Thompson Rumford.

  22. Excellent web site. Lots of useful information here. I’m sending it to several friends ans also sharing in delicious. And certainly, thanks for your sweat!

  23. I really appreciate this post. I have been looking all over for this! Thank goodness I found it on Bing. You have made my day! Thx again

  24. wbo777's avatar wbo777 says:

    I have been absent for some time, 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?

  25. Excellent beat ! I would like to apprentice while you amend your web site, how could i subscribe for a blog site? The account helped me a acceptable deal. I had been a little bit acquainted of this your broadcast offered bright clear concept

  26. Some times its a pain in the ass to read what blog owners wrote but this website is real user friendly! .

  27. pestoto's avatar pestoto says:

    I like what you guys are up too. Such intelligent work and reporting! Carry on the excellent works guys I have incorporated you guys to my blogroll. I think it will improve the value of my web site 🙂

  28. slot terbaru's avatar slot terbaru says:

    I liked up to you will obtain carried out right here. The sketch is attractive, your authored subject matter stylish. nevertheless, you command get got an edginess over that you want be turning in the following. unwell definitely come more previously once more since exactly the similar nearly a lot frequently inside case you protect this hike.

  29. toto303's avatar toto303 says:

    Thank you for another informative blog. Where else could I get that type of information written in such an ideal way? I’ve a project that I’m just now working on, and I have been on the look out for such information.

  30. hepato burn's avatar hepato burn says:

    I’ve been exploring for a little bit for any high-quality articles or blog posts on this kind of area . Exploring in Yahoo I at last stumbled upon this site. Reading this info So i’m happy to convey that I’ve a very good uncanny feeling I discovered just what I needed. I most certainly will make certain to do not forget this web site and give it a glance on a constant basis.

  31. pestoto's avatar pestoto says:

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

  32. It is the best time to make some plans for the future and it is time to be happy. I have read this post and if I could I want to suggest you few interesting things or tips. Maybe you could write next articles referring to this article. I wish to read more things about it!

  33. togel online's avatar togel online says:

    Whats Going down i’m new to this, I stumbled upon this I’ve discovered It positively helpful and it has aided me out loads. I hope to contribute & help different customers like its aided me. Good job.

  34. situs slot's avatar situs slot says:

    Pretty nice post. I just stumbled upon your blog and wished to say that I have really enjoyed browsing your blog posts. In any case I will be subscribing to your rss feed and I hope you write again very soon!

  35. akongcuan's avatar akongcuan says:

    Hello. impressive job. I did not imagine this. This is a remarkable story. Thanks!

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

  37. Hello just wanted to give you a quick heads up. The text in your article seem to be running off the screen in Ie. I’m not sure if this is a formatting issue or something to do with internet browser compatibility but I thought I’d post to let you know. The style and design look great though! Hope you get the problem solved soon. Thanks

  38. Absolutely written content material, Really enjoyed reading.

  39. you are in point of fact a just right webmaster. The website loading velocity is incredible. It seems that you’re doing any unique trick. Moreover, The contents are masterwork. you have done a wonderful job in this subject!

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

  41. I like this blog so much, saved to favorites. “American soldiers must be turned into lambs and eating them is tolerated.” by Muammar Qaddafi.

  42. hptoto's avatar hptoto says:

    Good day! This is kind of off topic but I need some advice from an established blog. Is it very difficult to set up your own blog? I’m not very techincal but I can figure things out pretty fast. I’m thinking about setting up my own but I’m not sure where to start. Do you have any points or suggestions? Many thanks

  43. toto macau's avatar toto macau says:

    Today, I went to the beach with my children. I found a sea shell and gave it to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.” She put the shell to her ear and screamed. There was a hermit crab inside and it pinched her ear. She never wants to go back! LoL I know this is entirely off topic but I had to tell someone!

  44. You actually make it appear so easy with your presentation but I in finding this topic to be really something which I believe I’d by no means understand. It seems too complicated and very vast for me. I’m taking a look ahead on your subsequent post, I?¦ll try to get the dangle of it!

  45. art elements's avatar art elements says:

    Hello, Neat post. There is a problem with your web site in internet explorer, could test this?K IE still is the market chief and a large portion of other people will miss your magnificent writing due to this problem.

  46. I like this web blog very much so much wonderful information.

  47. Awsome website! I am loving it!! Will come back again. I am bookmarking your feeds also

  48. rajabandot's avatar rajabandot says:

    I loved as much as you’ll obtain carried out proper here. The sketch is tasteful, your authored material stylish. nonetheless, you command get got an edginess over that you want be handing over the following. sick certainly come more earlier again since exactly the similar nearly a lot often inside case you shield this hike.

  49. I’d have to examine with you here. Which isn’t one thing I normally do! I take pleasure in reading a publish that will make individuals think. Also, thanks for permitting me to comment!

  50. Thank you a lot for sharing this with all people you actually recognise what you’re speaking approximately! Bookmarked. Please also seek advice from my website =). We could have a link alternate arrangement among us!

Comments are closed.