Interesting use of C# 2.0 ?? Coalesce operator when iterating through a collection
Ever since I discovered the ?? colaesce operator I embraced it with full force.
Today I discovered another usefull use:
// this: string[] arrayOfStrings1 = new string[] { "arr1s1", "arr1s2", "arr1s3" }; foreach (string str in (arrayOfStrings1 ?? new string[0])) { // do some stuff on the array elements }// equals to this: string[] arrayOfStrings2 = new string[] { "arr2s1", "arr2s2", "arr2s3" }; if (arrayOfStrings2 != null) { foreach (string str in arrayOfStrings2) { // do some stuff on the array elements } } // in terms of what it does
This enables me to get rid of all "if null" checks. I looked at IL with MSIL dissasembler and it doesn't look like there's any penalty in performance with using this so i'll stick with it. Of course reading IL isn't one of my strong sides so maybe someone can prove me wrong. :) But performance in this case doesn't outweigh the removal of "if null" check which i really hate.
Legacy Comments
James Curran
2006-10-09 |
re: Interesting use of C# 2.0 ?? Coalesce operator when iterating through a collection Well, you're not "getting rid of" the null checks as much as hiding them --- in a way that's a bit confusing to someone not familar with the idiom. Basically, your original code say "if arrayOfStrings2 is null, don't to this". Your new code says "if arrayOfStrings1 is null, do something different". And the "something different" code is just puzzling: foreach (string str in new string[0]) { // do some stuff on the array elements } A progammer first seeing that will probably stare at it for a while before realizing it's a nop. As for efficiency, using arrays, if goes from 2 IL instructions (if & jump) to about 5 (if, jump, newarray, if len, jmp), which is of course, trivial. However, in a more real-world example (an ICollection passed as an argument rather than a local array), it jumps from 2 to 10, with those 10 including calls to GetEnumerator & MoveNext. Still probably next enough to make a noticably difference in your apps (possibly not even measurable) but it's still there. |
Mladen
2006-10-10 |
re: Interesting use of C# 2.0 ?? Coalesce operator when iterating through a collection Thanx for that explanation James. i agree that it's more readable to see an if var == null than using ??. But you do have to appreciate the coolnes factor here, don't you? :)) |
alok
2008-03-26 |
re: Interesting use of C# 2.0 ?? Coalesce operator when iterating through a collection I like what you did there. If some new guy doesn't know what ?? does, then he should learn it eventually. I think the code looks cleaner (I've been working on legacy code that looks like that), could've been cleaner but that's microsoft for you.... |
Prafulla
2008-05-31 |
how to use COALESCE with xml datatype I want to use COALESCE with xml datatype SELECT COUNT(*) AS cnt FROM Report.tbl_Report_Info AS ReportInfo INNER JOIN Report.tbl_ReportSentDetails AS SendReport ON ReportInfo.ReportID = SendReport.Report_ID WHERE(ReportName LIKE COALESCE (@ReportName,ReportName)) AND (Report_Mailed_Date = COALESCE (@Report_Mailed_Date,Report_Mailed_Date)) (Report_Mailed_To = COALESCE (@Report_Mailed_To,Report_Mailed_To)) In this 1 fileld is Miled_To , it is xml datatype .how to use it with COALESCE . i get error for that datatype rest is working fine. pls tell me solution thaks & Regards Prafulla. |
Mladen
2008-06-02 |
re: Interesting use of C# 2.0 ?? Coalesce operator when iterating through a collection this works ok so your problem is somewhere else declare @x xml select coalesce(@x, 'gews') |