I want some Moore

Blog about stuff and things and stuff. Mostly about SQL server and .Net
posts - 219, comments - 2287, trackbacks - 33

My Links

Advertisement

News

Hi! My name is 
Mladen Prajdić  I'm from Slovenia and I'm currently working as a .Net (C#) and SQL Server developer.

I also speak at local user group meetings and conferences like SQLBits and NT Conference
Welcome to my blog.
SQL Server MVP

My Books

SQL Server MVP Deep Dives 2
The Red Gate Guide to SQL Server Team based Development Free e-book

My Blog Feed via Email
Follow MladenPrajdic on Twitter


Users Online: who's online

Article Categories

Archives

Post Categories

Cool software

Other Blogs

Other stuff

SQL stuff

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.

Print | posted on Monday, October 09, 2006 6:36 PM | Filed Under [ .Net ]

Feedback

Gravatar

# 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.
10/9/2006 11:45 PM | James Curran
Gravatar

# 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? :))
10/10/2006 11:04 AM | Mladen
Gravatar

# 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....
3/26/2008 9:06 PM | alok
Gravatar

# 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.
5/31/2008 4:50 PM | Prafulla
Gravatar

# 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')

6/2/2008 11:01 AM | Mladen
Comments have been closed on this topic.

Powered by:
Powered By Subtext Powered By ASP.NET