Extension Method Precedence

From the Visual Basic Programming Guide on Extension Methods (applicable to C# as well), an important blurb on extension method precedence:

When
two extension methods that have identical signatures are in scope and
accessible, the one with
higher precedence will be invoked. An
extension method’s precedence is based on the mechanism
used to bring
the method into scope. The following list shows the precedence
hierarchy, from
highest to lowest.

  1. Extension methods defined inside the current module.

  2. Extension
    methods defined inside data types in the current namespace or any one
    of its
    parents, with child namespaces having higher precedence than
    parent namespaces.

  3. Extension methods defined inside any type imports in the current file.

  4. Extension methods defined inside any namespace imports in the current file.

  5. Extension methods defined inside any project-level type imports.

  6. Extension methods defined inside any project-level namespace imports.

If
precedence does not resolve the ambiguity, you can use the fully
qualified name to specify
the method that you are calling. If the Print method in the earlier example is defined in a module
named StringExtensions, the fully qualified name is StringExtensions.Print(example) instead
of example.Print().

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s