SlideShare ist ein Scribd-Unternehmen logo
1 von 5
Downloaden Sie, um offline zu lesen
Generation 5 » Extension Methods, Nulls, Namespaces and Precedence in C#

 Subscribe to our RSS Feed | About Us

Extension Methods, Nulls, Namespaces and Precedence in
C#
Introduction
Extension methods are the most controversial feature that Microsoft has introduced in
C# 3.0.  Introduced to support the LINQ query framework,  extension methods make
it possible to define new methods for existing classes.
Although extension methods can greatly simplify code that uses them,  many are
concerned that they could transform C# into something that programmers find
unrecognizable,  or that C#’s namespace mechanisms are inadequate for managing
large systems that use extension methods.  Adoption of the LINQ framework, 
however,  means that extension methods are here to stay,  and that .net
programmers need to understand how to use them effectively,  and,  in particular, 
how extension methods are different from regular methods.
This article discusses three ways in which extension methods differ from regular
methods:
1. Extension methods can be called on null objects without throwing an exception
2. Extension methods cannot be called inside of a subclass without the use of ‘this’
3. The precedence rules for extension methods

The Menace of Null Values
The treatment of null values is one of the major weaknesses of today’s generation of
languages.  Although C# makes it possible to make nullable versions of value types, 
such as int? and guid?,  there’s nothing in C# or Java like the “NOT NULL” declaration
in SQL.  As a result,  handling nulls is a significant burden to writing correct code. 
Consider the simple case where you want to write
[01] someObject.DoSomething();

(where DoSomething is an ordinary instance method)  When I type something like
this,  Resharper often highlights the line of code to warn me that someObject might
be null.  In some cases I might be confident that it never will,  but if there is any
change that it will be null,  I’ll need to write something like
[02] if(someObject!=null) {
[03]    someObject.DoSomething();
[04] }

or maybe
[05] if(someObject==null)
[06]    return;
[07] someObject.DoSomething();

Alternatively I could accepted that an exception could be thrown by the invocation
and decide to catch it (or not catch it) elsewhere in the application.  In two cases out
of three,  one line of code gets bulked up to three.  Worse than that,  I need to make
a decision at that point about what to when there’s an error condition — each
decision is a case where somebody can make the wrong decision.  Even if coders
make the wrong decision 5% of the time,  that would be 50 time bombs in your code
for every 1000 method invocations.  (Oliver Steele works out a particularly outrageous
but common case where it takes 10 lines of null-checking code to protect 1 line of
working code.)

Extension Methods Can Accept Null Values
What does this have to do with extension methods?
Unlike ordinary instance methods,  extension methods do not automatically throw an

http://gen5.info/q/2008/07/03/extension-methods-nulls-namespaces-and-precedence-in-c/[1/16/2014 4:01:00 PM]

Search for:
Search

Archives

June 2012 (1)
August 2010 (1)
May 2010 (1)
June 2009 (2)
April 2009 (1)
March 2009 (1)
February 2009 (3)
January 2009 (3)
November 2008 (1)
August 2008 (2)
July 2008 (5)
June 2008 (5)
May 2008 (2)
April 2008 (6)
March 2008 (8)
June 2006 (1)
February 2006 (1)

Categories

AJAX (2)
Asynchronous Communications (16)
Biology (1)
Books (1)
Design (1)
Distributed (1)
Exceptions (2)
Functional Programming (1)
GIS (1)
Ithaca (1)
Japan (1)
Math (1)
Media (3)
Nature (1)
Semantic Web (3)
Tools (28)
CRUD (1)
Dot Net (17)
Freebase (2)
GWT (9)
Java (7)
Linq (2)
PHP (6)
Server Frameworks (1)
Silverlight (12)
SQL (5)
Uncategorized (1)
Web (2)
Analytics (1)
Generation 5 » Extension Methods, Nulls, Namespaces and Precedence in C#

exception if you call them on a null-valued object.  Depending on your point of view, 
this can be (i) a gotcha,  or (ii) a useful tool for simplifying your code.  Here’s a little
example:
[08] namespace ExtensionMethodTest {
[09]
[10]
static class ObjectExtension {
[11]
static public bool IsNull(this object o) {
[12]
return o == null;
[13]
}
[14]
}
[15]
[16]
class Program {
[17]
static void Main(string[] args) {
[18]
String s1 = "hello";
[19]
Console.WriteLine(s1.IsNull());
[20]
String s2 = null;
[21]
Console.WriteLine(s2.IsNull());
[22]
Console.WriteLine(s2.ToUpper());
[23]
}
[24]
}
[25] }

This example does something a bit bold:  it attaches an extension method to object ,  
adding an extenson method to every object in the system.  This method, 
object.IsNull() returns true if object is null and false if it isn’t.  Some people might see
this as a nice example of syntactic sugar,  others may see it as reckless.  What’s
important is that it works:  if you run this program from the command line,  line [21]
will print ‘true’,  while line [22],  which uses an ordinary method,  will throw a
NullReferenceException.

Events and Extension Methods for Delegates
Chris Brandsma works out a practical example of how extension methods can be used
to fix a broken and dangerous API.  That is,  the event handling mechanism
commonly used in C#:
[26]
[27]
[28]
[29]
[30]

public eventEventHandler<EventArgs> OnLoadData;
...
OnLoadData += SomeEventHandler;
...
OnLoadData(this, argument);

OnLoadData is a MulticastDelegate.  You can attach an unlimited number of real
delegates to it.  The sample above works great if you attach at least one delegate, 
but it fails with a NullReferenceException if you don’t.  Perhaps this isn’t a problem for
you,  because you’re smart and you write
[31] if (OnLoadData==null) {
[32]     OnLoadData(this,argument)
[33] }

Unfortunately,  there are two little problems with that.  First,  none of us program in a
vacuum,   so many of us will end up having to maintain or use objects where
somebody forgot to include a null check.   Secondly,  the example between lines [31]
and [33] isn’t thread safe.  It’s possible that a method can be removed from
OnLoadData between the time of the null check and the call!
It turns out that extension methods can be added to delegates,  so Chris created a
really nice extension method called Fire() that encapsulates the error check code
between 31-33.   Now you can just write the code you wanted to write:
[34] OnLoadData.Fire(this,argument);

and be confident that knowledge about threads and quirks of the type system is
embedded in an extension method.

You must use this to access an extension method inside a subclass
Suppose you’re building a Silverlight application and you’d like your team to have an
important method that incorporates something tricky on their fingertips.  For
instance,  suppose you’re implementing error handling in an event handler that’s
responding to a user-initiated event or an async callback.   You can always write
[35] if(... something wrong...) {
[36]    ... several lines of code to display dialog box ...
[37]    return;
[38] }

But this is something that (i) programmers don’t want to do to begin with,  (ii) that
programmers will have to do tens or hundreds of times,  and (iii) isn’t going to be in
the main line of testing.  It’s a quality problem waiting to happen.  It’s imperative, 
therefore,  to reduce the amount of code to do the right thing as much as possible… 

http://gen5.info/q/2008/07/03/extension-methods-nulls-namespaces-and-precedence-in-c/[1/16/2014 4:01:00 PM]
Generation 5 » Extension Methods, Nulls, Namespaces and Precedence in C#

To make it easier to do the right thing than to do the wrong thing.   It’s tempting to
define an extension method like:
[39] public static void ErrorDialog(this UserControl c, string message) {
[40]    throw new ErrorMessageException(message);
[41] }

and catch the ErrorMessageException in the global error handler.  (The “method that
doesn’t return” is effective,  because it avoids the need to repeat the return,  which
occassionaly seems to vanish when people write repetitive error handling code.) 
You’d think that this simplifies the code inside the UserControls you write to:
[42] if (... something wrong...)  {
[43]    ErrorDialog(...);
[44] }

But it turns out that line [43] doesn’t actually work,  and you need to write
[45] if (... something wrong...) {
[46]    this.ErrorDialog(...);
[47] }

in which case you might as well use an ordinary static method on a helper class.

What’s wrong with extension methods?
I’ve seen two arguments against extension methods:  (i) extension methods could
make code hard to understand (and hence maintain) and (ii) extension methods are
vulnerable to namespace conflicts.  I think (i) is a specious argument,  but (ii) is
serious.
I think (i) splits into two directions.  First there’s the practical problem that a
programmer is going to see some code like
[48] String s="somebody@example.com";
[49] if (s.IsValidEmailAddress()) {
[50]     ... do something ...
[51] }

and wonder where the heck IsValidEmailAddress() comes from,  where it’s
documented,  and so forth.  Practically,  Visual Studio understands extension methods
well,  so a user that clicks on “Go To Definition” is going to get a quick answer.
Going further,  however,  one can imagine that extension methods could transform C#
unrecognizably:  I think of a friend of mine who,  in the 1980′s,  liked FORTRAN better
than C,  and abused preprocessor macros so he could write C code that looked like
FORTRAN.   This is connected with a fear of lambda expressions,  and other features
that derive from functional programming.  For instance,  that beginning programmers
just won’t get it.
We’ll see how it all works out,  but I think that new features in C# are going to help
the language evolve in a way more like jquery and prototype have transformed
javascript.  Microsoft is bringing concepts that have been locked in the ivory tower for
decades into the mainstream:  all programming languages are going to benefit in the
long term.

Extension methods,  precedence and namespaces
Here’s the killer.
I can make extension methods available by just adding a namespace to my .cs file
with a using directive.  The compiler scans the namespace for extension methods in
static classes,  and makes them available.  Pretty easy,  right?  Well,  what happens if
two extension methods with the same name get declared in two namespaces which
get included in the same file?  What if we define an extension method on class A,  but
there’s a conventional method with the same name on class B?  What if file One.cs
uses namesspace C,  and Two.cs uses namespace D,   so that ThisExtensionMethod
means something different in One.cs and Two.cs?
There are real problems in how extension methods interact with namespaces.  These
problems aren’t as fatal as namespace conflicts were in C (and C++ before
namespaces),  but they are for real.
One answer is to avoid the use of extension methods entirely,  but that causes the
loss of the benefits.  Anyone who uses extension methods should take a close look at
the C# version 3.0 specification and think about how precedence rules effect their
work:

http://gen5.info/q/2008/07/03/extension-methods-nulls-namespaces-and-precedence-in-c/[1/16/2014 4:01:00 PM]
Generation 5 » Extension Methods, Nulls, Namespaces and Precedence in C#

(i) Instance methods take precedence over extension methods.  The definition of an
instance method makes extension methods with the same name inaccessable.  This
happens at the level of methods,  not method groups,  so two methods with the same
name but different signatures can be handled by an extension method and instance
method respectively.
(ii) Once the compiler tries extension methods,  processing works backwards from the
closest enclosing namespace declaration outward,  trying extension methods defined
in using groups.
(iii) The compiler throws an error when there are two or more extension methods that
are candidates for a spot.
Matt Manela demonstrates an interesting example on the MSDN forums.  With three
examples,  he demonstrates that the existence of an instance method (that overrides
both extension methods) will suppress the generation of an error message about a
conflict between extension methods.  This indicates that potentially conflicting
extension methods in two namespaces will only cause an error if an attempt is made
to use them.

Mitigating Namespace Conflicts
Overall,  conflicts between extension methods in different namespaces will not result
in catastrophic consequences:
1. The compiler raises an error if there is any ambiguity as to which extension
method to apply at a particular invocation — code won’t silently change behavior
upon adding a new namespace in a using directive.
2. The compiler does not throw an error if potentially conflicting extension methods
are declared in two different namespaces including in distinct using directives if
those extension methods are not used — therefore,  conflicting extension
methods won’t automatically prevent you from using any namespaces you
choose.
3. If there is a conflict,  either between two extension methods or an extension
method and an instance methods,  you can always call a specific extension
method like an ordinary static example.  For instance,  in the case above:

ObjectExtension.IsNull(someObject);
You won’t end up in a situation where an extension method becomes unavailable
because of a conflict — you’ll just be forced to use an uglier syntax.  I do see two real
risks:
1. You can end up using an extension method that you don’t expect if you’re not
keeping track of which using directives are in your file,  and
2. An instance method can silently shadow an extension method.  A change in the
definition of a method could cause the behavior of a (former) extension method
cal to change in a suprising way.  On the other hand,  this could be a useful
behavior if you’d like a subclass to override a behavior defined in an extension
method.
A common bit of advice that I’ve seen circulating is that extension methods should be
defined in separate namespaces,  so that it would be possible to include or not include
extension methods associated with a namespace to avoid conflicts.  I think this is
based on superstition,  for,  as we’ve seen,  conflicting extension methods do not
preclude the use of two namespaces;  this advice is certainly not followed in the
System.Linq namespace,  which defines a number of valuable extension methods in
the System.Linq.Enumerable static class.

Conclusion
We’re still learning how to use extension methods effectively.  Although extension
methods have great promise,  they’re difference from ordinary instance methods in a
number of ways.  Some of these,  like the difference in null handling,  are minor,  and
could potentially be put to advantage.  Others,  such as the interaction with
namespaces in large projects,   are more challenging.  It’s time to start building on
our experiences to develop effective patterns for using extension methods.

Paul Houle on July 3rd 2008 in Dot Net

Comment (1)

http://gen5.info/q/2008/07/03/extension-methods-nulls-namespaces-and-precedence-in-c/[1/16/2014 4:01:00 PM]

Comments (1)
Login
Generation 5 » Extension Methods, Nulls, Namespaces and Precedence in C#

Sort by: Date Rating Last Activity
ecards · 269 weeks ago

0

Nice article. But what I'm not clear on is what's the difference between:
1) adding an extension method foobar for class A
2) adding a method foobar to a partial class A
I get the semantic differences like null checking, but there must be a more fundamental difference i'm
missing...
Reply

Post a new comment
Enter text right here!

Comment as a Guest, or login:
Name

Email

Website (optional)

Displayed next to your comments.

Not displayed publicly.

If you have a website, link to it here.

None
Subscribe to None

Submit Comment

Copyright © 2013 Generation 5.
WordPress Theme design.

http://gen5.info/q/2008/07/03/extension-methods-nulls-namespaces-and-precedence-in-c/[1/16/2014 4:01:00 PM]

Weitere ähnliche Inhalte

Was ist angesagt?

Gsp 125 Future Our Mission/newtonhelp.com
Gsp 125 Future Our Mission/newtonhelp.comGsp 125 Future Our Mission/newtonhelp.com
Gsp 125 Future Our Mission/newtonhelp.comamaranthbeg8
 
Java Multiple Choice Questions and Answers
Java Multiple Choice Questions and AnswersJava Multiple Choice Questions and Answers
Java Multiple Choice Questions and AnswersJava Projects
 
Flex 4 components from the firehose
Flex 4 components from the firehoseFlex 4 components from the firehose
Flex 4 components from the firehosemichael.labriola
 
A novel approach based on topic
A novel approach based on topicA novel approach based on topic
A novel approach based on topiccsandit
 

Was ist angesagt? (13)

Gsp 125 Future Our Mission/newtonhelp.com
Gsp 125 Future Our Mission/newtonhelp.comGsp 125 Future Our Mission/newtonhelp.com
Gsp 125 Future Our Mission/newtonhelp.com
 
Java Multiple Choice Questions and Answers
Java Multiple Choice Questions and AnswersJava Multiple Choice Questions and Answers
Java Multiple Choice Questions and Answers
 
Java mcq
Java mcqJava mcq
Java mcq
 
Clean code
Clean codeClean code
Clean code
 
Object-oriented Basics
Object-oriented BasicsObject-oriented Basics
Object-oriented Basics
 
C04701019027
C04701019027C04701019027
C04701019027
 
Intake 37 2
Intake 37 2Intake 37 2
Intake 37 2
 
Flex 4 components from the firehose
Flex 4 components from the firehoseFlex 4 components from the firehose
Flex 4 components from the firehose
 
A novel approach based on topic
A novel approach based on topicA novel approach based on topic
A novel approach based on topic
 
Asp.net main
Asp.net mainAsp.net main
Asp.net main
 
Robots in Swift
Robots in SwiftRobots in Swift
Robots in Swift
 
Intake 37 1
Intake 37 1Intake 37 1
Intake 37 1
 
Thinking In Swift
Thinking In SwiftThinking In Swift
Thinking In Swift
 

Andere mochten auch

Important agriculture extension teaching method defined By Mr Allah Dad Khan ...
Important agriculture extension teaching method defined By Mr Allah Dad Khan ...Important agriculture extension teaching method defined By Mr Allah Dad Khan ...
Important agriculture extension teaching method defined By Mr Allah Dad Khan ...Mr.Allah Dad Khan
 
eXtension for Extension Methods class
eXtension for Extension Methods class eXtension for Extension Methods class
eXtension for Extension Methods class Anne Adrian
 
Common extension teaching methods used by DAE
Common extension teaching methods used by DAECommon extension teaching methods used by DAE
Common extension teaching methods used by DAEsaifur rahman
 
Fundamental of Extension Methods: Tools and Techniques of PRA
Fundamental of Extension Methods:  Tools and Techniques of PRAFundamental of Extension Methods:  Tools and Techniques of PRA
Fundamental of Extension Methods: Tools and Techniques of PRABrajendra Singh Meena
 
Individual contact method in Extension Education
Individual contact method in Extension EducationIndividual contact method in Extension Education
Individual contact method in Extension EducationBalaraj BL
 

Andere mochten auch (6)

c# extension methods
c# extension methodsc# extension methods
c# extension methods
 
Important agriculture extension teaching method defined By Mr Allah Dad Khan ...
Important agriculture extension teaching method defined By Mr Allah Dad Khan ...Important agriculture extension teaching method defined By Mr Allah Dad Khan ...
Important agriculture extension teaching method defined By Mr Allah Dad Khan ...
 
eXtension for Extension Methods class
eXtension for Extension Methods class eXtension for Extension Methods class
eXtension for Extension Methods class
 
Common extension teaching methods used by DAE
Common extension teaching methods used by DAECommon extension teaching methods used by DAE
Common extension teaching methods used by DAE
 
Fundamental of Extension Methods: Tools and Techniques of PRA
Fundamental of Extension Methods:  Tools and Techniques of PRAFundamental of Extension Methods:  Tools and Techniques of PRA
Fundamental of Extension Methods: Tools and Techniques of PRA
 
Individual contact method in Extension Education
Individual contact method in Extension EducationIndividual contact method in Extension Education
Individual contact method in Extension Education
 

Ähnlich wie Extension methods, nulls, namespaces and precedence in c#

PVS-Studio vs Chromium
PVS-Studio vs ChromiumPVS-Studio vs Chromium
PVS-Studio vs ChromiumPVS-Studio
 
PVS-Studio vs Chromium
PVS-Studio vs ChromiumPVS-Studio vs Chromium
PVS-Studio vs ChromiumAndrey Karpov
 
How to avoid bugs using modern C++
How to avoid bugs using modern C++How to avoid bugs using modern C++
How to avoid bugs using modern C++PVS-Studio
 
PVS-Studio: analyzing ReactOS's code
PVS-Studio: analyzing ReactOS's codePVS-Studio: analyzing ReactOS's code
PVS-Studio: analyzing ReactOS's codePVS-Studio
 
How to complement TDD with static analysis
How to complement TDD with static analysisHow to complement TDD with static analysis
How to complement TDD with static analysisPVS-Studio
 
Production Debugging at Code Camp Philly
Production Debugging at Code Camp PhillyProduction Debugging at Code Camp Philly
Production Debugging at Code Camp PhillyBrian Lyttle
 
maXbox Starter 43 Work with Code Metrics ISO Standard
maXbox Starter 43 Work with Code Metrics ISO StandardmaXbox Starter 43 Work with Code Metrics ISO Standard
maXbox Starter 43 Work with Code Metrics ISO StandardMax Kleiner
 
EKON 23 Code_review_checklist
EKON 23 Code_review_checklistEKON 23 Code_review_checklist
EKON 23 Code_review_checklistMax Kleiner
 
Java Programming
Java ProgrammingJava Programming
Java ProgrammingTracy Clark
 
We continue checking Microsoft projects: analysis of PowerShell
We continue checking Microsoft projects: analysis of PowerShellWe continue checking Microsoft projects: analysis of PowerShell
We continue checking Microsoft projects: analysis of PowerShellPVS-Studio
 
Forensic Memory Analysis of Android's Dalvik Virtual Machine
Forensic Memory Analysis of Android's Dalvik Virtual MachineForensic Memory Analysis of Android's Dalvik Virtual Machine
Forensic Memory Analysis of Android's Dalvik Virtual MachineSource Conference
 
100 bugs in Open Source C/C++ projects
100 bugs in Open Source C/C++ projects100 bugs in Open Source C/C++ projects
100 bugs in Open Source C/C++ projectsPVS-Studio
 
No more Three Tier - A path to a better code for Cloud and Azure
No more Three Tier - A path to a better code for Cloud and AzureNo more Three Tier - A path to a better code for Cloud and Azure
No more Three Tier - A path to a better code for Cloud and AzureMarco Parenzan
 
Man in-the-browser-in-depth-report
Man in-the-browser-in-depth-reportMan in-the-browser-in-depth-report
Man in-the-browser-in-depth-reportHai Nguyen
 
Workshop - The Little Pattern That Could.pdf
Workshop - The Little Pattern That Could.pdfWorkshop - The Little Pattern That Could.pdf
Workshop - The Little Pattern That Could.pdfTobiasGoeschel
 
why c++11?
why c++11?why c++11?
why c++11?idrajeev
 

Ähnlich wie Extension methods, nulls, namespaces and precedence in c# (20)

PVS-Studio vs Chromium
PVS-Studio vs ChromiumPVS-Studio vs Chromium
PVS-Studio vs Chromium
 
PVS-Studio vs Chromium
PVS-Studio vs ChromiumPVS-Studio vs Chromium
PVS-Studio vs Chromium
 
How to avoid bugs using modern C++
How to avoid bugs using modern C++How to avoid bugs using modern C++
How to avoid bugs using modern C++
 
PVS-Studio: analyzing ReactOS's code
PVS-Studio: analyzing ReactOS's codePVS-Studio: analyzing ReactOS's code
PVS-Studio: analyzing ReactOS's code
 
Making an Exception
Making an ExceptionMaking an Exception
Making an Exception
 
How to complement TDD with static analysis
How to complement TDD with static analysisHow to complement TDD with static analysis
How to complement TDD with static analysis
 
Production Debugging at Code Camp Philly
Production Debugging at Code Camp PhillyProduction Debugging at Code Camp Philly
Production Debugging at Code Camp Philly
 
maXbox Starter 43 Work with Code Metrics ISO Standard
maXbox Starter 43 Work with Code Metrics ISO StandardmaXbox Starter 43 Work with Code Metrics ISO Standard
maXbox Starter 43 Work with Code Metrics ISO Standard
 
Need 4 Speed FI
Need 4 Speed FINeed 4 Speed FI
Need 4 Speed FI
 
EKON 23 Code_review_checklist
EKON 23 Code_review_checklistEKON 23 Code_review_checklist
EKON 23 Code_review_checklist
 
Java Programming
Java ProgrammingJava Programming
Java Programming
 
We continue checking Microsoft projects: analysis of PowerShell
We continue checking Microsoft projects: analysis of PowerShellWe continue checking Microsoft projects: analysis of PowerShell
We continue checking Microsoft projects: analysis of PowerShell
 
Forensic Memory Analysis of Android's Dalvik Virtual Machine
Forensic Memory Analysis of Android's Dalvik Virtual MachineForensic Memory Analysis of Android's Dalvik Virtual Machine
Forensic Memory Analysis of Android's Dalvik Virtual Machine
 
100 bugs in Open Source C/C++ projects
100 bugs in Open Source C/C++ projects100 bugs in Open Source C/C++ projects
100 bugs in Open Source C/C++ projects
 
midterm_fa07.pdf
midterm_fa07.pdfmidterm_fa07.pdf
midterm_fa07.pdf
 
No more Three Tier - A path to a better code for Cloud and Azure
No more Three Tier - A path to a better code for Cloud and AzureNo more Three Tier - A path to a better code for Cloud and Azure
No more Three Tier - A path to a better code for Cloud and Azure
 
Man in-the-browser-in-depth-report
Man in-the-browser-in-depth-reportMan in-the-browser-in-depth-report
Man in-the-browser-in-depth-report
 
Workshop - The Little Pattern That Could.pdf
Workshop - The Little Pattern That Could.pdfWorkshop - The Little Pattern That Could.pdf
Workshop - The Little Pattern That Could.pdf
 
unit_tests_tutorial
unit_tests_tutorialunit_tests_tutorial
unit_tests_tutorial
 
why c++11?
why c++11?why c++11?
why c++11?
 

Mehr von Paul Houle

Chatbots in 2017 -- Ithaca Talk Dec 6
Chatbots in 2017 -- Ithaca Talk Dec 6Chatbots in 2017 -- Ithaca Talk Dec 6
Chatbots in 2017 -- Ithaca Talk Dec 6Paul Houle
 
Estimating the Software Product Value during the Development Process
Estimating the Software Product Value during the Development ProcessEstimating the Software Product Value during the Development Process
Estimating the Software Product Value during the Development ProcessPaul Houle
 
Universal Standards for LEI and other Corporate Reference Data: Enabling risk...
Universal Standards for LEI and other Corporate Reference Data: Enabling risk...Universal Standards for LEI and other Corporate Reference Data: Enabling risk...
Universal Standards for LEI and other Corporate Reference Data: Enabling risk...Paul Houle
 
Fixing a leaky bucket; Observations on the Global LEI System
Fixing a leaky bucket; Observations on the Global LEI SystemFixing a leaky bucket; Observations on the Global LEI System
Fixing a leaky bucket; Observations on the Global LEI SystemPaul Houle
 
Cisco Fog Strategy For Big and Smart Data
Cisco Fog Strategy For Big and Smart DataCisco Fog Strategy For Big and Smart Data
Cisco Fog Strategy For Big and Smart DataPaul Houle
 
Making the semantic web work
Making the semantic web workMaking the semantic web work
Making the semantic web workPaul Houle
 
Ontology2 platform
Ontology2 platformOntology2 platform
Ontology2 platformPaul Houle
 
Ontology2 Platform Evolution
Ontology2 Platform EvolutionOntology2 Platform Evolution
Ontology2 Platform EvolutionPaul Houle
 
Paul houle the supermen
Paul houle   the supermenPaul houle   the supermen
Paul houle the supermenPaul Houle
 
Paul houle what ails enterprise search
Paul houle   what ails enterprise search Paul houle   what ails enterprise search
Paul houle what ails enterprise search Paul Houle
 
Subjective Importance Smackdown
Subjective Importance SmackdownSubjective Importance Smackdown
Subjective Importance SmackdownPaul Houle
 
Dropping unique constraints in sql server
Dropping unique constraints in sql serverDropping unique constraints in sql server
Dropping unique constraints in sql serverPaul Houle
 
Prefix casting versus as-casting in c#
Prefix casting versus as-casting in c#Prefix casting versus as-casting in c#
Prefix casting versus as-casting in c#Paul Houle
 
Paul houle resume
Paul houle resumePaul houle resume
Paul houle resumePaul Houle
 
Keeping track of state in asynchronous callbacks
Keeping track of state in asynchronous callbacksKeeping track of state in asynchronous callbacks
Keeping track of state in asynchronous callbacksPaul Houle
 
Embrace dynamic PHP
Embrace dynamic PHPEmbrace dynamic PHP
Embrace dynamic PHPPaul Houle
 
Once asynchronous, always asynchronous
Once asynchronous, always asynchronousOnce asynchronous, always asynchronous
Once asynchronous, always asynchronousPaul Houle
 
What do you do when you’ve caught an exception?
What do you do when you’ve caught an exception?What do you do when you’ve caught an exception?
What do you do when you’ve caught an exception?Paul Houle
 
Extension methods, nulls, namespaces and precedence in c#
Extension methods, nulls, namespaces and precedence in c#Extension methods, nulls, namespaces and precedence in c#
Extension methods, nulls, namespaces and precedence in c#Paul Houle
 
Pro align snap 2
Pro align snap 2Pro align snap 2
Pro align snap 2Paul Houle
 

Mehr von Paul Houle (20)

Chatbots in 2017 -- Ithaca Talk Dec 6
Chatbots in 2017 -- Ithaca Talk Dec 6Chatbots in 2017 -- Ithaca Talk Dec 6
Chatbots in 2017 -- Ithaca Talk Dec 6
 
Estimating the Software Product Value during the Development Process
Estimating the Software Product Value during the Development ProcessEstimating the Software Product Value during the Development Process
Estimating the Software Product Value during the Development Process
 
Universal Standards for LEI and other Corporate Reference Data: Enabling risk...
Universal Standards for LEI and other Corporate Reference Data: Enabling risk...Universal Standards for LEI and other Corporate Reference Data: Enabling risk...
Universal Standards for LEI and other Corporate Reference Data: Enabling risk...
 
Fixing a leaky bucket; Observations on the Global LEI System
Fixing a leaky bucket; Observations on the Global LEI SystemFixing a leaky bucket; Observations on the Global LEI System
Fixing a leaky bucket; Observations on the Global LEI System
 
Cisco Fog Strategy For Big and Smart Data
Cisco Fog Strategy For Big and Smart DataCisco Fog Strategy For Big and Smart Data
Cisco Fog Strategy For Big and Smart Data
 
Making the semantic web work
Making the semantic web workMaking the semantic web work
Making the semantic web work
 
Ontology2 platform
Ontology2 platformOntology2 platform
Ontology2 platform
 
Ontology2 Platform Evolution
Ontology2 Platform EvolutionOntology2 Platform Evolution
Ontology2 Platform Evolution
 
Paul houle the supermen
Paul houle   the supermenPaul houle   the supermen
Paul houle the supermen
 
Paul houle what ails enterprise search
Paul houle   what ails enterprise search Paul houle   what ails enterprise search
Paul houle what ails enterprise search
 
Subjective Importance Smackdown
Subjective Importance SmackdownSubjective Importance Smackdown
Subjective Importance Smackdown
 
Dropping unique constraints in sql server
Dropping unique constraints in sql serverDropping unique constraints in sql server
Dropping unique constraints in sql server
 
Prefix casting versus as-casting in c#
Prefix casting versus as-casting in c#Prefix casting versus as-casting in c#
Prefix casting versus as-casting in c#
 
Paul houle resume
Paul houle resumePaul houle resume
Paul houle resume
 
Keeping track of state in asynchronous callbacks
Keeping track of state in asynchronous callbacksKeeping track of state in asynchronous callbacks
Keeping track of state in asynchronous callbacks
 
Embrace dynamic PHP
Embrace dynamic PHPEmbrace dynamic PHP
Embrace dynamic PHP
 
Once asynchronous, always asynchronous
Once asynchronous, always asynchronousOnce asynchronous, always asynchronous
Once asynchronous, always asynchronous
 
What do you do when you’ve caught an exception?
What do you do when you’ve caught an exception?What do you do when you’ve caught an exception?
What do you do when you’ve caught an exception?
 
Extension methods, nulls, namespaces and precedence in c#
Extension methods, nulls, namespaces and precedence in c#Extension methods, nulls, namespaces and precedence in c#
Extension methods, nulls, namespaces and precedence in c#
 
Pro align snap 2
Pro align snap 2Pro align snap 2
Pro align snap 2
 

Kürzlich hochgeladen

Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfSeasiaInfotech2
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 

Kürzlich hochgeladen (20)

Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdf
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 

Extension methods, nulls, namespaces and precedence in c#

  • 1. Generation 5 » Extension Methods, Nulls, Namespaces and Precedence in C#  Subscribe to our RSS Feed | About Us Extension Methods, Nulls, Namespaces and Precedence in C# Introduction Extension methods are the most controversial feature that Microsoft has introduced in C# 3.0.  Introduced to support the LINQ query framework,  extension methods make it possible to define new methods for existing classes. Although extension methods can greatly simplify code that uses them,  many are concerned that they could transform C# into something that programmers find unrecognizable,  or that C#’s namespace mechanisms are inadequate for managing large systems that use extension methods.  Adoption of the LINQ framework,  however,  means that extension methods are here to stay,  and that .net programmers need to understand how to use them effectively,  and,  in particular,  how extension methods are different from regular methods. This article discusses three ways in which extension methods differ from regular methods: 1. Extension methods can be called on null objects without throwing an exception 2. Extension methods cannot be called inside of a subclass without the use of ‘this’ 3. The precedence rules for extension methods The Menace of Null Values The treatment of null values is one of the major weaknesses of today’s generation of languages.  Although C# makes it possible to make nullable versions of value types,  such as int? and guid?,  there’s nothing in C# or Java like the “NOT NULL” declaration in SQL.  As a result,  handling nulls is a significant burden to writing correct code.  Consider the simple case where you want to write [01] someObject.DoSomething(); (where DoSomething is an ordinary instance method)  When I type something like this,  Resharper often highlights the line of code to warn me that someObject might be null.  In some cases I might be confident that it never will,  but if there is any change that it will be null,  I’ll need to write something like [02] if(someObject!=null) { [03]    someObject.DoSomething(); [04] } or maybe [05] if(someObject==null) [06]    return; [07] someObject.DoSomething(); Alternatively I could accepted that an exception could be thrown by the invocation and decide to catch it (or not catch it) elsewhere in the application.  In two cases out of three,  one line of code gets bulked up to three.  Worse than that,  I need to make a decision at that point about what to when there’s an error condition — each decision is a case where somebody can make the wrong decision.  Even if coders make the wrong decision 5% of the time,  that would be 50 time bombs in your code for every 1000 method invocations.  (Oliver Steele works out a particularly outrageous but common case where it takes 10 lines of null-checking code to protect 1 line of working code.) Extension Methods Can Accept Null Values What does this have to do with extension methods? Unlike ordinary instance methods,  extension methods do not automatically throw an http://gen5.info/q/2008/07/03/extension-methods-nulls-namespaces-and-precedence-in-c/[1/16/2014 4:01:00 PM] Search for: Search Archives June 2012 (1) August 2010 (1) May 2010 (1) June 2009 (2) April 2009 (1) March 2009 (1) February 2009 (3) January 2009 (3) November 2008 (1) August 2008 (2) July 2008 (5) June 2008 (5) May 2008 (2) April 2008 (6) March 2008 (8) June 2006 (1) February 2006 (1) Categories AJAX (2) Asynchronous Communications (16) Biology (1) Books (1) Design (1) Distributed (1) Exceptions (2) Functional Programming (1) GIS (1) Ithaca (1) Japan (1) Math (1) Media (3) Nature (1) Semantic Web (3) Tools (28) CRUD (1) Dot Net (17) Freebase (2) GWT (9) Java (7) Linq (2) PHP (6) Server Frameworks (1) Silverlight (12) SQL (5) Uncategorized (1) Web (2) Analytics (1)
  • 2. Generation 5 » Extension Methods, Nulls, Namespaces and Precedence in C# exception if you call them on a null-valued object.  Depending on your point of view,  this can be (i) a gotcha,  or (ii) a useful tool for simplifying your code.  Here’s a little example: [08] namespace ExtensionMethodTest { [09] [10] static class ObjectExtension { [11] static public bool IsNull(this object o) { [12] return o == null; [13] } [14] } [15] [16] class Program { [17] static void Main(string[] args) { [18] String s1 = "hello"; [19] Console.WriteLine(s1.IsNull()); [20] String s2 = null; [21] Console.WriteLine(s2.IsNull()); [22] Console.WriteLine(s2.ToUpper()); [23] } [24] } [25] } This example does something a bit bold:  it attaches an extension method to object ,   adding an extenson method to every object in the system.  This method,  object.IsNull() returns true if object is null and false if it isn’t.  Some people might see this as a nice example of syntactic sugar,  others may see it as reckless.  What’s important is that it works:  if you run this program from the command line,  line [21] will print ‘true’,  while line [22],  which uses an ordinary method,  will throw a NullReferenceException. Events and Extension Methods for Delegates Chris Brandsma works out a practical example of how extension methods can be used to fix a broken and dangerous API.  That is,  the event handling mechanism commonly used in C#: [26] [27] [28] [29] [30] public eventEventHandler<EventArgs> OnLoadData; ... OnLoadData += SomeEventHandler; ... OnLoadData(this, argument); OnLoadData is a MulticastDelegate.  You can attach an unlimited number of real delegates to it.  The sample above works great if you attach at least one delegate,  but it fails with a NullReferenceException if you don’t.  Perhaps this isn’t a problem for you,  because you’re smart and you write [31] if (OnLoadData==null) { [32]     OnLoadData(this,argument) [33] } Unfortunately,  there are two little problems with that.  First,  none of us program in a vacuum,   so many of us will end up having to maintain or use objects where somebody forgot to include a null check.   Secondly,  the example between lines [31] and [33] isn’t thread safe.  It’s possible that a method can be removed from OnLoadData between the time of the null check and the call! It turns out that extension methods can be added to delegates,  so Chris created a really nice extension method called Fire() that encapsulates the error check code between 31-33.   Now you can just write the code you wanted to write: [34] OnLoadData.Fire(this,argument); and be confident that knowledge about threads and quirks of the type system is embedded in an extension method. You must use this to access an extension method inside a subclass Suppose you’re building a Silverlight application and you’d like your team to have an important method that incorporates something tricky on their fingertips.  For instance,  suppose you’re implementing error handling in an event handler that’s responding to a user-initiated event or an async callback.   You can always write [35] if(... something wrong...) { [36]    ... several lines of code to display dialog box ... [37]    return; [38] } But this is something that (i) programmers don’t want to do to begin with,  (ii) that programmers will have to do tens or hundreds of times,  and (iii) isn’t going to be in the main line of testing.  It’s a quality problem waiting to happen.  It’s imperative,  therefore,  to reduce the amount of code to do the right thing as much as possible…  http://gen5.info/q/2008/07/03/extension-methods-nulls-namespaces-and-precedence-in-c/[1/16/2014 4:01:00 PM]
  • 3. Generation 5 » Extension Methods, Nulls, Namespaces and Precedence in C# To make it easier to do the right thing than to do the wrong thing.   It’s tempting to define an extension method like: [39] public static void ErrorDialog(this UserControl c, string message) { [40]    throw new ErrorMessageException(message); [41] } and catch the ErrorMessageException in the global error handler.  (The “method that doesn’t return” is effective,  because it avoids the need to repeat the return,  which occassionaly seems to vanish when people write repetitive error handling code.)  You’d think that this simplifies the code inside the UserControls you write to: [42] if (... something wrong...)  { [43]    ErrorDialog(...); [44] } But it turns out that line [43] doesn’t actually work,  and you need to write [45] if (... something wrong...) { [46]    this.ErrorDialog(...); [47] } in which case you might as well use an ordinary static method on a helper class. What’s wrong with extension methods? I’ve seen two arguments against extension methods:  (i) extension methods could make code hard to understand (and hence maintain) and (ii) extension methods are vulnerable to namespace conflicts.  I think (i) is a specious argument,  but (ii) is serious. I think (i) splits into two directions.  First there’s the practical problem that a programmer is going to see some code like [48] String s="somebody@example.com"; [49] if (s.IsValidEmailAddress()) { [50]     ... do something ... [51] } and wonder where the heck IsValidEmailAddress() comes from,  where it’s documented,  and so forth.  Practically,  Visual Studio understands extension methods well,  so a user that clicks on “Go To Definition” is going to get a quick answer. Going further,  however,  one can imagine that extension methods could transform C# unrecognizably:  I think of a friend of mine who,  in the 1980′s,  liked FORTRAN better than C,  and abused preprocessor macros so he could write C code that looked like FORTRAN.   This is connected with a fear of lambda expressions,  and other features that derive from functional programming.  For instance,  that beginning programmers just won’t get it. We’ll see how it all works out,  but I think that new features in C# are going to help the language evolve in a way more like jquery and prototype have transformed javascript.  Microsoft is bringing concepts that have been locked in the ivory tower for decades into the mainstream:  all programming languages are going to benefit in the long term. Extension methods,  precedence and namespaces Here’s the killer. I can make extension methods available by just adding a namespace to my .cs file with a using directive.  The compiler scans the namespace for extension methods in static classes,  and makes them available.  Pretty easy,  right?  Well,  what happens if two extension methods with the same name get declared in two namespaces which get included in the same file?  What if we define an extension method on class A,  but there’s a conventional method with the same name on class B?  What if file One.cs uses namesspace C,  and Two.cs uses namespace D,   so that ThisExtensionMethod means something different in One.cs and Two.cs? There are real problems in how extension methods interact with namespaces.  These problems aren’t as fatal as namespace conflicts were in C (and C++ before namespaces),  but they are for real. One answer is to avoid the use of extension methods entirely,  but that causes the loss of the benefits.  Anyone who uses extension methods should take a close look at the C# version 3.0 specification and think about how precedence rules effect their work: http://gen5.info/q/2008/07/03/extension-methods-nulls-namespaces-and-precedence-in-c/[1/16/2014 4:01:00 PM]
  • 4. Generation 5 » Extension Methods, Nulls, Namespaces and Precedence in C# (i) Instance methods take precedence over extension methods.  The definition of an instance method makes extension methods with the same name inaccessable.  This happens at the level of methods,  not method groups,  so two methods with the same name but different signatures can be handled by an extension method and instance method respectively. (ii) Once the compiler tries extension methods,  processing works backwards from the closest enclosing namespace declaration outward,  trying extension methods defined in using groups. (iii) The compiler throws an error when there are two or more extension methods that are candidates for a spot. Matt Manela demonstrates an interesting example on the MSDN forums.  With three examples,  he demonstrates that the existence of an instance method (that overrides both extension methods) will suppress the generation of an error message about a conflict between extension methods.  This indicates that potentially conflicting extension methods in two namespaces will only cause an error if an attempt is made to use them. Mitigating Namespace Conflicts Overall,  conflicts between extension methods in different namespaces will not result in catastrophic consequences: 1. The compiler raises an error if there is any ambiguity as to which extension method to apply at a particular invocation — code won’t silently change behavior upon adding a new namespace in a using directive. 2. The compiler does not throw an error if potentially conflicting extension methods are declared in two different namespaces including in distinct using directives if those extension methods are not used — therefore,  conflicting extension methods won’t automatically prevent you from using any namespaces you choose. 3. If there is a conflict,  either between two extension methods or an extension method and an instance methods,  you can always call a specific extension method like an ordinary static example.  For instance,  in the case above: ObjectExtension.IsNull(someObject); You won’t end up in a situation where an extension method becomes unavailable because of a conflict — you’ll just be forced to use an uglier syntax.  I do see two real risks: 1. You can end up using an extension method that you don’t expect if you’re not keeping track of which using directives are in your file,  and 2. An instance method can silently shadow an extension method.  A change in the definition of a method could cause the behavior of a (former) extension method cal to change in a suprising way.  On the other hand,  this could be a useful behavior if you’d like a subclass to override a behavior defined in an extension method. A common bit of advice that I’ve seen circulating is that extension methods should be defined in separate namespaces,  so that it would be possible to include or not include extension methods associated with a namespace to avoid conflicts.  I think this is based on superstition,  for,  as we’ve seen,  conflicting extension methods do not preclude the use of two namespaces;  this advice is certainly not followed in the System.Linq namespace,  which defines a number of valuable extension methods in the System.Linq.Enumerable static class. Conclusion We’re still learning how to use extension methods effectively.  Although extension methods have great promise,  they’re difference from ordinary instance methods in a number of ways.  Some of these,  like the difference in null handling,  are minor,  and could potentially be put to advantage.  Others,  such as the interaction with namespaces in large projects,   are more challenging.  It’s time to start building on our experiences to develop effective patterns for using extension methods. Paul Houle on July 3rd 2008 in Dot Net Comment (1) http://gen5.info/q/2008/07/03/extension-methods-nulls-namespaces-and-precedence-in-c/[1/16/2014 4:01:00 PM] Comments (1) Login
  • 5. Generation 5 » Extension Methods, Nulls, Namespaces and Precedence in C# Sort by: Date Rating Last Activity ecards · 269 weeks ago 0 Nice article. But what I'm not clear on is what's the difference between: 1) adding an extension method foobar for class A 2) adding a method foobar to a partial class A I get the semantic differences like null checking, but there must be a more fundamental difference i'm missing... Reply Post a new comment Enter text right here! Comment as a Guest, or login: Name Email Website (optional) Displayed next to your comments. Not displayed publicly. If you have a website, link to it here. None Subscribe to None Submit Comment Copyright © 2013 Generation 5. WordPress Theme design. http://gen5.info/q/2008/07/03/extension-methods-nulls-namespaces-and-precedence-in-c/[1/16/2014 4:01:00 PM]