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 believe other website owners should take this internet site as an model, very clean and good user friendly layout.

  2. Merely wanna admit that this is invaluable, Thanks for taking your time to write this.

  3. Hello There. I found your blog using msn. This is an extremely well written article. I will be sure to bookmark it and come back to read more of your useful information. Thanks for the post. I’ll certainly comeback.

  4. olxtoto's avatar olxtoto says:

    Very good written post. It will be helpful to everyone who employess it, including me. Keep doing what you are doing – for sure i will check out more posts.

  5. Glad to be one of the visitors on this awing site : D.

  6. Good day very cool web site!! Guy .. Beautiful .. Wonderful .. I’ll bookmark your site and take the feeds additionally…I am glad to seek out so many useful information here in the post, we need work out more techniques on this regard, thanks for sharing. . . . . .

  7. Some genuinely nice stuff on this website , I enjoy it.

  8. mantap555's avatar mantap555 says:

    I’ll immediately grab your rss feed as I can not find your e-mail subscription link or e-newsletter service. Do you’ve any? Kindly let me know so that I could subscribe. Thanks.

  9. Its excellent as your other blog posts : D, regards for posting. “The real hero is always a hero by mistake he dreams of being an honest coward like everybody else.” by Umberto Eco.

  10. Pretty nice post. I just stumbled upon your blog and wanted to say that I’ve really enjoyed surfing around your blog posts. After all I’ll be subscribing to your feed and I hope you write again soon!

  11. You have brought up a very good details, regards for the post.

  12. I see something truly special in this web site.

  13. Very good written information. It will be helpful to everyone who utilizes it, as well as myself. Keep up the good work – for sure i will check out more posts.

  14. Your place is valueble for me. Thanks!…

  15. atom108 slot's avatar atom108 slot says:

    Hello my loved one! I want to say that this post is awesome, nice written and come with approximately all significant infos. I would like to see extra posts like this.

  16. You can certainly see your skills in the work you write. The world hopes for even more passionate writers like you who aren’t afraid to mention how they believe. Always go after your heart.

  17. boostaro's avatar boostaro says:

    Greetings from Los angeles! I’m bored to death at work so I decided to browse your blog on my iphone during lunch break. I really like the info you provide here and can’t wait to take a look when I get home. I’m shocked at how quick your blog loaded on my cell phone .. I’m not even using WIFI, just 3G .. Anyways, amazing site!

  18. slot 5000's avatar slot 5000 says:

    I’m not sure exactly why but this blog is loading incredibly slow for me. Is anyone else having this issue or is it a problem on my end? I’ll check back later on and see if the problem still exists.

  19. tonic greens's avatar tonic greens says:

    I?¦ve read some just right stuff here. Certainly value bookmarking for revisiting. I wonder how a lot attempt you put to create such a magnificent informative site.

  20. Timsothybioli's avatar Timsothybioli says:

    Howdy! Do you use Twitter? I’d like to follow you if that would be okay. I’m absolutely enjoying your blog and look forward to new posts.
    byueuropaviagraonline

  21. F*ckin’ tremendous things here. I am very glad to see your post. Thanks a lot and i am looking forward to contact you. Will you kindly drop me a mail?

  22. This is very interesting, You’re a very skilled blogger. I’ve joined your feed and look forward to seeking more of your excellent post. Also, I have shared your web site in my social networks!

  23. magnificent publish, very informative. I’m wondering why the opposite experts of this sector don’t notice this. You must continue your writing. I am sure, you have a huge readers’ base already!

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

  25. Do you mind if I quote a couple of your posts as long as I provide credit and sources back to your site? My blog site is in the exact same area of interest as yours and my visitors would genuinely benefit from some of the information you present here. Please let me know if this ok with you. Appreciate it!

  26. I think other web-site proprietors should take this web site as an model, very clean and great user friendly style and design, let alone the content. You are an expert in this topic!

  27. Its like you read my mind! You seem to know so much about this, like you wrote the book in it or something. I think that you can do with some pics to drive the message home a little bit, but other than that, this is magnificent blog. A great read. I will certainly be back.

  28. I don’t even know how I ended up here, but I thought this post was great. I do not know who you are but certainly you are going to a famous blogger if you are not already 😉 Cheers!

  29. I’m still learning from you, as I’m trying to reach my goals. I definitely love reading everything that is posted on your site.Keep the information coming. I liked it!

  30. Throughout this grand scheme of things you’ll get an A+ with regard to effort and hard work. Where exactly you actually misplaced everybody was in your details. As people say, the devil is in the details… And it could not be more accurate right here. Having said that, permit me tell you what did do the job. Your authoring is really persuasive which is probably why I am taking an effort in order to comment. I do not make it a regular habit of doing that. Secondly, even though I can notice the leaps in reason you make, I am definitely not sure of exactly how you appear to connect the ideas that help to make your final result. For right now I will, no doubt yield to your issue but hope in the future you actually link your facts much better.

  31. Oh my goodness! a tremendous article dude. Thanks Nonetheless I am experiencing situation with ur rss . Don’t know why Unable to subscribe to it. Is there anybody getting equivalent rss downside? Anybody who is aware of kindly respond. Thnkx

  32. Hi 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 recommendations?

  33. Hi there are using WordPress for your site platform? I’m new to the blog world but I’m trying to get started and set up my own. Do you require any coding knowledge to make your own blog? Any help would be really appreciated!

  34. Hi! 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 page to him. Fairly certain he will have a good read. Many thanks for sharing!

  35. I?¦ve been exploring for a little for any high-quality articles or blog posts in this sort of house . Exploring in Yahoo I eventually stumbled upon this site. Reading this info So i am satisfied to show that I have a very just right uncanny feeling I found out exactly what I needed. I so much certainly will make certain to do not overlook this website and provides it a look on a relentless basis.

  36. Great post. I used to be checking continuously this weblog and I am impressed! Extremely helpful info specially the closing part 🙂 I deal with such information a lot. I was seeking this particular information for a very lengthy time. Thank you and best of luck.

  37. ogłoszenia's avatar ogłoszenia says:

    Would you be desirous about exchanging links?

  38. flixy tv's avatar flixy tv says:

    Hello there! I know this is kind of off topic but I was wondering which blog platform are you using for this website? I’m getting fed up of WordPress because I’ve had problems with hackers and I’m looking at alternatives for another platform. I would be great if you could point me in the direction of a good platform.

  39. Sweet internet site, super design, very clean and utilize pleasant.

  40. Perfectly written content material, Really enjoyed studying.

  41. I’ve been browsing on-line more than 3 hours lately, but I by no means found any attention-grabbing article like yours. It is lovely value enough for me. In my opinion, if all site owners and bloggers made excellent content material as you probably did, the internet might be much more useful than ever before.

  42. Great V I should definitely pronounce, impressed with your site. I had no trouble navigating through all tabs and related info ended up being truly simple to do to access. I recently found what I hoped for before you know it at all. Reasonably unusual. Is likely to appreciate it for those who add forums or anything, web site theme . a tones way for your customer to communicate. Nice task..

  43. I haven’t checked in here for a while as I thought it was getting boring, but the last few posts are good quality so I guess I will add you back to my daily bloglist. You deserve it my friend 🙂

  44. I used to be very pleased to find this internet-site.I wanted to thanks for your time for this wonderful read!! I positively enjoying every little bit of it and I’ve you bookmarked to take a look at new stuff you blog post.

  45. Fobertnap's avatar Fobertnap says:

    WOW just what I was searching for. Came here by searching for %meta_keyword%
    byueuropaviagraonline

  46. USA Flights 24 — search engine helps you compare prices from hundreds of airlines and travel sites in seconds — so you can find cheap flights fast. Whether you’re planning a weekend getaway, a cross-country adventure, or an international vacation, we make it easy to fly for less.

  47. OLanepiliA's avatar OLanepiliA says:

    Avia Masters de BGaming es un juego crash con RTP del 97% donde apuestas desde 0,10€ hasta 1.000€, controlas la velocidad de vuelo de un avion que recoge multiplicadores (hasta x250) mientras evita cohetes que reducen ganancias a la mitad, con el objetivo de aterrizar exitosamente en un portaaviones para cobrar el premio acumulado
    https://feg.org.es/

Comments are closed.