13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

262 CHAPTER 10 Extension methodsnamespace for the sake of mak<strong>in</strong>g types available us<strong>in</strong>g only their simple names, butwithout mak<strong>in</strong>g the extension methods with<strong>in</strong> that namespace available at the sametime. I recommend us<strong>in</strong>g a namespace that solely conta<strong>in</strong>s static classes with extensionmethods to mitigate this problem.There’s one aspect of extension methods that can be quite surpris<strong>in</strong>g when youfirst encounter it but is also useful <strong>in</strong> some situations. It’s all about null references—let’s take a look.10.2.4 Call<strong>in</strong>g a method on a null referenceI’d be amazed if I ever encountered anyone who’d done a significant amount of .NETprogramm<strong>in</strong>g without see<strong>in</strong>g a NullReferenceException due to call<strong>in</strong>g a method witha variable whose value turned out to be a null reference. You can’t call <strong>in</strong>stance methodson null references <strong>in</strong> <strong>C#</strong> (although IL itself supports it for nonvirtual calls)—but you cancall extension methods with a null reference. This is demonstrated by list<strong>in</strong>g 10.5. Notethat this isn’t a snippet s<strong>in</strong>ce nested classes can’t conta<strong>in</strong> extension methods.List<strong>in</strong>g 10.5Extension method be<strong>in</strong>g called on a null referenceus<strong>in</strong>g System;public static class NullUtil{public static bool IsNull(this object x){return x==null;}}public class Test{static void Ma<strong>in</strong>(){object y = null;Console.WriteL<strong>in</strong>e(y.IsNull());y = new object();Console.WriteL<strong>in</strong>e(y.IsNull());}}The output of list<strong>in</strong>g 10.5 is “True” then “False”—if IsNull had been a normal<strong>in</strong>stance method, an exception would have been thrown <strong>in</strong> the second l<strong>in</strong>e of Ma<strong>in</strong>.Instead, IsNull was called with null as the argument. Prior to the advent of extensionmethods, <strong>C#</strong> had no way of lett<strong>in</strong>g you write the more readable y.IsNull() formsafely, requir<strong>in</strong>g NullUtil.IsNull(y) <strong>in</strong>stead. There’s one particularly obvious example<strong>in</strong> the framework where this could be useful: str<strong>in</strong>g.IsNullOrEmpty. <strong>C#</strong> 3 allowsyou to write an extension method that has the same signature (other than the “extra”parameter for the extended type) as an exist<strong>in</strong>g static method on the extended type.To save you read<strong>in</strong>g through that sentence several times, here’s an example—eventhough the str<strong>in</strong>g class has a static, parameterless method IsNullOrEmpty, you canstill create and use the follow<strong>in</strong>g extension method:Licensed to Rhona Hadida

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!