Tuesday, August 11, 2026

"New" C# Goodies

I've been working with C# almost since it was first released, which has led me to keep using some things I learned way back then even though there are easier ways to do those same things now. This will probably be a series of posts that - if anyone is actually reading these - may cause readers to question my own development capabilities. All I can say is: some day it'll happen to you, too.

Anyway, today I learned about the "Index from end operator" that became available in C# 8.0. It's used like this: var item = list[^1]; and simply counts the collection from the end instead of the beginning, so ^1 just means "get me the first item from the collection, but start counting from the end". It's a simpler form of var item = list[list.Count - 1];

Much easier to write and read once you know it's available. 

No comments:

Post a Comment