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.
I’ve been browsing online more than 3 hours today, yet I never found any interesting article like yours. It is pretty worth enough for me. In my opinion, if all site owners and bloggers made good content as you did, the net will be much more useful than ever before.
Hi, Neat post. There’s a problem along with your site in internet explorer, may test thisK IE nonetheless is the marketplace chief and a big portion of other people will pass over your fantastic writing because of this problem.
I know this if off topic but I’m looking into starting my own weblog and was curious what all is required to get set up? I’m assuming having a blog like yours would cost a pretty penny? I’m not very web savvy so I’m not 100 sure. Any tips or advice would be greatly appreciated. Kudos
Magnificent site. Plenty of helpful info here. I am sending it to a few friends ans additionally sharing in delicious. And certainly, thank you on your sweat!
I believe this site has got very superb written subject matter blog posts.
I really appreciate this post. I have been looking all over for this! Thank goodness I found it on Bing. You’ve made my day! Thank you again!
Wonderful blog! I found it while browsing on Yahoo News. Do you have any tips on how to get listed in Yahoo News? I’ve been trying for a while but I never seem to get there! Cheers
Hey I am so grateful I found your blog, I really found you by accident, while I was searching on Digg for something else, Regardless I am here now and would just like to say thank you for a tremendous post and a all round enjoyable blog (I also love the theme/design), I don’t have time to look over it all at the moment but I have bookmarked it and also added in your RSS feeds, so when I have time I will be back to read a lot more, Please do keep up the awesome work.
This design is wicked! You most certainly 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!) Excellent job. I really loved what you had to say, and more than that, how you presented it. Too cool!
Heya i’m for the primary time here. I found this board and I in finding It truly helpful & it helped me out a lot. I hope to give one thing back and aid others such as you aided me.
What’s Taking place i’m new to this, I stumbled upon this I’ve found It absolutely helpful and it has aided me out loads. I am hoping to contribute & aid different users like its aided me. Good job.
Your place is valueble for me. Thanks!…
I would like to thank you for the efforts you’ve put in writing this website. I am hoping the same high-grade site post from you in the upcoming also. In fact your creative writing abilities has inspired me to get my own blog now. Actually the blogging is spreading its wings quickly. Your write up is a good example of it.
Wow! Thank you! I continually needed to write on my website something like that. Can I include a part of your post to my website?
Sugaring effektive und moderne Haarentfernung in Berlin Die Epilation mit Zuckerpaste wird von unseren speziell dafür ausgebildeten Kosmetikerinnen / Depiladoras an allen Körperregionen durchgeführt. Wir bieten diese effektive und moderne Behandlung sehr erfolgreich und schonend mit einem Maximum in der Hygiene der Anwendung an. Sugaring wird immer beliebter.