tag:blogger.com,1999:blog-8980734269859237005.post8371426386504577390..comments2024-01-30T01:03:21.768-08:00Comments on DevCurry: Different Ways of using the C# Null Coalescing OperatorSuprotim Agarwalhttp://www.blogger.com/profile/08349831623922214390noreply@blogger.comBlogger9125tag:blogger.com,1999:blog-8980734269859237005.post-48797016187974681462011-08-03T00:36:51.765-07:002011-08-03T00:36:51.765-07:00For those who are worried about empty strings in M...For those who are worried about empty strings in Method 1, you can also adopt this piece of code if it helps<br /><br />string address = new[]{ homeaddr, homeaddr2, offaddr, "No address" }.First(a => !string.IsNullOrEmpty(a));Suprotim Agarwalhttps://www.blogger.com/profile/08349831623922214390noreply@blogger.comtag:blogger.com,1999:blog-8980734269859237005.post-75195327954539299782011-03-01T15:21:55.691-08:002011-03-01T15:21:55.691-08:00@Anonymous (whoever you may be)
Sorry if I bother ...@Anonymous (whoever you may be)<br />Sorry if I bother you... but you don't contrib even less.<br /><br />I have developed heavy multithreaded/multicore applications for a long time now and it is just a somewhat long answer to Zagrebelin's question.<br /><br />The other remarks are just because I felt like it like you did your remark.<br /><br />Regards,<br />JelleJelle Hissinkhttp://www.CodeWise.nlnoreply@blogger.comtag:blogger.com,1999:blog-8980734269859237005.post-60696997420104367552011-03-01T14:11:40.330-08:002011-03-01T14:11:40.330-08:00Jelle Hissink - OMG did you just read a book and n...Jelle Hissink - OMG did you just read a book and need to flaunt it? That is not a contrib. but just plane amateuris, speaking for the sake of it. Zip it up!Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8980734269859237005.post-3797806852325300612010-08-27T12:58:56.381-07:002010-08-27T12:58:56.381-07:00The behaviour in method 2 is changed by the replac...The behaviour in method 2 is changed by the replacement code. The origional code could result in a NullReferenceException whereas the replacement results in 0.<br /><br />Method 1 and 4 are just variations on the same thing, you should just put the code next to eachother. Method 4 just indicates you could use it for configuration options.<br /><br />@Zagrebelin:<br />Method 3 is not threadsafe, Unknownhttps://www.blogger.com/profile/02009408566972932715noreply@blogger.comtag:blogger.com,1999:blog-8980734269859237005.post-16632761878976010252010-08-27T12:58:41.784-07:002010-08-27T12:58:41.784-07:00The behaviour in method 2 is changed by the replac...The behaviour in method 2 is changed by the replacement code. The origional code could result in a NullReferenceException whereas the replacement results in 0.<br /><br />Method 1 and 4 are just variations on the same thing, you should just put the code next to eachother. Method 4 just indicates you could use it for configuration options.<br /><br />@Zagrebelin:<br />Method 3 is not threadsafe, Unknownhttps://www.blogger.com/profile/02009408566972932715noreply@blogger.comtag:blogger.com,1999:blog-8980734269859237005.post-56042885318937264442010-08-27T11:51:28.421-07:002010-08-27T11:51:28.421-07:00Is method3 is thread-safe?Is method3 is thread-safe?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8980734269859237005.post-44657318199169674432010-08-25T02:26:16.922-07:002010-08-25T02:26:16.922-07:00Point accepted and thank you. However it is not ab...Point accepted and thank you. However it is not about adopting a coding strategy. I agree there is a check needed to see if the string is empty before using this code.<br /><br />The article purely focuses on different ways to use the null-coalesce operator and I just showed one way. That extension method of yours is useful.Suprotim Agarwalhttps://www.blogger.com/profile/08349831623922214390noreply@blogger.comtag:blogger.com,1999:blog-8980734269859237005.post-78183730454903016112010-08-25T01:57:50.666-07:002010-08-25T01:57:50.666-07:00It should be noted that method 1 will get you into...It should be noted that method 1 will get you into trouble when any of the addresses is String.Emtpy,<br />Because well, String.Emtpy != null.<br /><br />I have made an Extension method to the string class: IfNullOrEmpty.<br /><br />Method 1 would translate to:<br />string address = homeAddress1<br />.IfNullOrEmpty(homeAddress2)<br />.IfNullOrEmpty(officeAddress)<br />.IfNullOrEmpty("No Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8980734269859237005.post-72500628235229756672010-08-24T04:46:22.961-07:002010-08-24T04:46:22.961-07:00You can also use ?? to null coalesce amongst class...You can also use ?? to null coalesce amongst classes sharing the same base class. Normally this wouldn't compile but there is a work around:<br /><br />class Animal{}<br /><br />class Bear: Animal{}<br /><br />class Tiger: Animal{}<br /><br />...<br /><br />Animal MyAnimal{<br />get{ return this.Bear ?? <b>(Animal)</b> this.Tiger; }<br />}alhttp://www.xircut.comnoreply@blogger.com