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

    amoxicillin 500 mg without prescription Infectious Disease & Antimicrobial Research Institute amoxicillin 500mg without prescription

  2. DichaelAlono's avatar DichaelAlono says:

    Your ability to take this data and present it in such an objective and clear format is greatly appreciated. I will definitely be recommending this page to my coworkers.

    клиника Вирсавия

  3. Most of the things you assert is astonishingly accurate and that makes me ponder why I hadn’t looked at this with this light previously. This piece really did switch the light on for me personally as far as this particular subject matter goes. However there is 1 factor I am not really too comfy with so whilst I make an effort to reconcile that with the actual main idea of your point, allow me see just what the rest of the readers have to say.Well done.

  4. winhq's avatar winhq says:

    Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog that automatically tweet my newest twitter updates. I’ve been looking for a plug-in like this for quite some time and was hoping maybe you would have some experience with something like this. Please let me know if you run into anything. I truly enjoy reading your blog and I look forward to your new updates.

  5. slotvip's avatar slotvip says:

    Fantastic goods from you, man. I’ve understand your stuff previous to and you are just extremely excellent. I really like what you have acquired here, certainly like what you’re saying and the way in which you say it. You make it entertaining and you still care for to keep it wise. I can not wait to read far more from you. This is actually a terrific website.

  6. EarnestCor's avatar EarnestCor says:

    It is always a pleasant surprise to find an article that delivers exactly what it promises. Your unbiased breakdown of the facts is both refreshing and incredibly helpful, providing a highly solid foundation of knowledge for anyone who wants to dive deeper into this subject.

    модели для косметологии

  7. Hello there, I found your web site via Google while looking for a related topic, your web site came up, it looks good. I’ve bookmarked it in my google bookmarks.

  8. I like the efforts you have put in this, regards for all the great posts.

  9. TimothySet's avatar TimothySet says:

    semaglutide thyroid cancer American Metabolic Research Institute

  10. BrianTed's avatar BrianTed says:

    Advancing Clinical Research in Metabolic Health and Weight Management https://usobesityscience.org/orlistat-clinical-review/ oral semaglutide drops

  11. vapofil's avatar vapofil says:

    Thanks on your marvelous posting! I certainly enjoyed reading it, you can be a great author.I will remember to bookmark your blog and will often come back from now on. I want to encourage yourself to continue your great work, have a nice afternoon!

  12. Stephenviaps's avatar Stephenviaps says:

    It takes a lot of effort to synthesize so much information into one single post, and I want to applaud you for doing so in a way that feels completely smooth and incredibly easy to follow from beginning to end today.

    Empower RF

  13. JohnnieMut's avatar JohnnieMut says:

    rybelsus in india AMRI

  14. BrianTed's avatar BrianTed says:

    tirzepatide https://usobesityscience.org/glp-1-agonists/ buy liraglutide online

  15. Hey there! This is my 1st comment here so I just wanted to give a quick shout out and say I truly enjoy reading your articles. Can you suggest any other blogs/websites/forums that cover the same subjects? Thanks for your time!

  16. Thank you for the good writeup. It in fact was a amusement account it. Look advanced to far added agreeable from you! However, how can we communicate?

  17. BryceTew's avatar BryceTew says:

    buy victoza orlistat

  18. I went over this site and I think you have a lot of fantastic information, saved to favorites (:.

  19. I think this web site contains very superb indited content material content.

  20. TimothySet's avatar TimothySet says:

    victoza delivery AMRI

  21. Wow, fantastic weblog layout! How long have you ever been blogging for? you make blogging look easy. The full glance of your web site is excellent, let alone the content material!

  22. Fobertnap's avatar Fobertnap says:

    There is something very accessible about the way this post presents its message, because the wording feels balanced and relaxed while still keeping the discussion insightful and relevant for a broad online audience.

    no deposit bonus

  23. vvjl's avatar vvjl says:

    What’s up, after reading this remarkable post i am also happy to share my familiarity here with colleagues.

  24. As soon as I observed this web site I went on reddit to share some of the love with them.

  25. The introduction of the ‘^’ operator in C# 13 definitely streamlines accessing elements from the end of a collection, eliminating that old cumbersome syntax. It’s a small but impactful change that makes code both more readable and less error-prone when dealing with indices relative to the end.

  26. 外贸GEO's avatar 外贸GEO says:

    I am impressed with this site, very I am a big fan .

  27. 377bet's avatar 377bet says:

    Woah! I’m really loving the template/theme of this blog. It’s simple, yet effective. A lot of times it’s tough to get that “perfect balance” between superb usability and visual appearance. I must say you have done a amazing job with this. Also, the blog loads extremely fast for me on Opera. Excellent Blog!

  28. hptoto link's avatar hptoto link says:

    Some genuinely wonderful information, Sword lily I observed this. “Speak when you are angry–and you will make the best speech you’ll ever regret.” by Laurence J. Peter.

  29. I am only commenting to make you know of the nice discovery my friend’s child gained reading your webblog. She even learned several things, which include what it’s like to have a very effective teaching mindset to make most people with no trouble grasp a variety of tortuous subject matter. You really did more than our desires. I appreciate you for imparting those insightful, trustworthy, educational and also easy guidance on the topic to Gloria.

  30. hgo909 login's avatar hgo909 login says:

    Hey there, You have done a fantastic job. I’ll definitely digg it and in my view recommend to my friends. I’m confident they’ll be benefited from this web site.

  31. Hi there! I simply want to give an enormous thumbs up for the great data you have got right here on this post. I will probably be coming back to your blog for more soon.

  32. slot gacor's avatar slot gacor says:

    I must express some appreciation to the writer just for bailing me out of this particular condition. Right after surfing throughout the world-wide-web and getting things which were not helpful, I assumed my life was well over. Living devoid of the answers to the difficulties you have fixed through the short article is a serious case, and ones that might have in a wrong way affected my career if I had not noticed your blog post. Your own knowledge and kindness in taking care of the whole lot was priceless. I am not sure what I would have done if I had not come upon such a thing like this. I can also at this point look ahead to my future. Thanks for your time very much for the specialized and effective guide. I will not be reluctant to refer the blog to anybody who needs guidelines on this matter.

  33. I’ve been surfing on-line more than 3 hours these days, yet I never discovered any fascinating article like yours. It is pretty worth sufficient for me. Personally, if all webmasters and bloggers made just right content as you did, the web might be a lot more helpful than ever before.

  34. codere's avatar codere says:

    Hi, I read your blogs like every week. Your humoristic style is witty, keep doing what you’re doing!

  35. Way cool! Some extremely valid points! I appreciate you writing this post and also the rest of the site is really good.

  36. gatotkac's avatar gatotkac says:

    After study a few of the blog posts on your website now, and I truly like your way of blogging. I bookmarked it to my bookmark website list and will be checking back soon. Pls check out my web site as well and let me know what you think.

  37. BrianTed's avatar BrianTed says:

    American Metabolic Research Institute https://usobesityscience.org/tirzepatide-analysis/ icd 10 code for rybelsus

  38. link mpo's avatar link mpo says:

    so much superb info on here, :D.

  39. excellent points altogether, you just gained a new reader. What would you recommend about your post that you made some days ago? Any positive?

  40. Thank you for sharing superb informations. Your site is very cool. I’m impressed by the details that you have on this website. It reveals how nicely you perceive this subject. Bookmarked this website page, will come back for extra articles. You, my pal, ROCK! I found just the information I already searched all over the place and simply couldn’t come across. What a great site.

  41. link mpo100's avatar link mpo100 says:

    I was wondering if you ever thought of changing the layout of your blog? Its very well written; I love what youve got to say. But maybe you could a little more in the way of content so people could connect with it better. Youve got an awful lot of text for only having 1 or two pictures. Maybe you could space it out better?

  42. Fobertnap's avatar Fobertnap says:

    This is very interesting, You are a very skilled blogger. I’ve joined your rss feed and look forward to seeking more of your excellent post. Also, I’ve shared your web site in my social networks!
    Watch sexual porno video xxx sex adults site

  43. GichardAmomi's avatar GichardAmomi says:

    I believe everything said was very logical. However, what about this? suppose you composed a catchier post title? I ain’t saying your information is not good., however what if you added something that grabbed folk’s attention? I mean %BLOG_TITLE% is a little vanilla. You could glance at Yahoo’s front page and see how they create post titles to get viewers to open the links. You might try adding a video or a related pic or two to grab readers excited about what you’ve written. In my opinion, it might bring your posts a little livelier.
    Site de paris sportifs hors arjel

  44. audileaf's avatar audileaf says:

    Excellent blog here! Also your website loads up very fast! What web host are you using? Can I get your affiliate link to your host? I wish my web site loaded up as fast as yours lol

  45. Hey there are using WordPress for your site platform? I’m new to the blog world but I’m trying to get started and create my own. Do you need any html coding knowledge to make your own blog? Any help would be really appreciated!

  46. TimothyMic's avatar TimothyMic says:

    http://mensrxguide.org/# Cialis 20mg price in USA

  47. Hello.This article was extremely remarkable, especially because I was looking for thoughts on this subject last Sunday.

Comments are closed.