+ 2

What is this syntax in C#?

Working on some legacy C# code, I came across expression syntax like this: (myString.ToUpper()?.ToString()) What does it do?

1st Jun 2022, 3:39 PM
Brian
Brian - avatar
5 ответов
+ 3
Thank you Lisa and Justice. I learned several new things about C# from the articles that Lisa linked. Humble appreciation! Now I understand the ?. operator, but I still don't grok the result of the expression. Suppose myString is non-null. It transforms the string to uppercase and then converts that string into ... a string?? 😶
3rd Jun 2022, 3:50 AM
Brian
Brian - avatar
+ 2
Lisa Yup! Tbh, it's much more of a backend thing to make sure that you're not getting the equivalent of a NullException which can be known as the Billion Dollar Mistake. Java has terrible null safety but languages like C# and Kotlin try to combat it with that specific syntax.
1st Jun 2022, 4:05 PM
Justice
Justice - avatar
+ 1
Looks like it taking a value, converting it into all uppercase and then converting into a string. The ? for most languages means that it's just checking if it's null.
1st Jun 2022, 3:55 PM
Justice
Justice - avatar
+ 1
Well folks, upon further review of the legacy code, I have concluded that someone simply did a massive find/replace, and did not check whether the code made sense still afterward.
3rd Jun 2022, 8:07 PM
Brian
Brian - avatar