I don't know how about you, but for me using int.TryParse() always feel somehow not right, resulting in an ugly three lines of code to do just one operation: int value; if (!int.TryParse(valueToParseFromString, out value) value = 0; If you are looking for cleaner code to satisfy your inner pedant you can use this one-line solution instead: int value = int.TryParse(valueToParseFromString, out value) ? value : 0; It will parse the valueToParseFromString and on success assign it to the value variable, otherwise it will return 0.
Read MoreAbout
I'm Izabela Wlodarska and I'm a passionate solutions architect mostly focused on .NET, Sitecore and Azure.
I love constantly improving my skills and sharing my knowledge with others.
I hope that reading this blog will help you to become a better developer. Welcome on board and let's learn together!
Latest posts
- Getting started with Unicorn serialization for Sitecore Experience Accelerator (SXA) – part 2
- Setting custom field as Browser Title in SXA
- Getting started with Unicorn serialization for Sitecore – Part 1
- Azure tricks – How to show all files in Kudu Diagnostic Console, not just the first 199 items
- Elegant one-line way to use TryParse
Tags
Comments
Awesome !
This is still great, use it all the time! Only thing that's changed that can make this one-liner more elegant…
This is an excellent article, thanks for posting it! I'm looking to set up something very similar. In the time…

Yes, you have two different configurations: Content-Data and Content-Data-Configuration which are trying to serialize the same item (/sitecore/content/Data/Configuration). This is…
thank you, I use this construction: .int value= int.TryParse(valueToParseFromString, out value) ? value: 0;.