Bug 38805 - Null Propagation: Extension methods with the same name as a non-extension methods are problematic for null propagation in Mono
Summary: Null Propagation: Extension methods with the same name as a non-extension met...
Alias: None
Product: Compilers
Classification: Mono
Component: C# ()
Version: unspecified
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Marek Safar
Depends on:
Reported: 2016-02-16 16:05 UTC by Norbert Virth
Modified: 2016-03-17 22:43 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

Code for reproduction (788 bytes, text/plain)
2016-02-16 16:05 UTC, Norbert Virth
Versio info provided by Xamarin Studio (974 bytes, text/plain)
2016-02-16 16:07 UTC, Norbert Virth

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Norbert Virth 2016-02-16 16:05:38 UTC
Created attachment 15059 [details]
Code for reproduction

It seems extension methods with the same name as a non-extension methods are problematic
for null propagation in Mono.
In the below code, IDictionary has a method named TryGetValue, and I add +1 extension method
with the same name. Null propagation won't work, the method will be called also
with null dictionary.
If running the code below in Xamarin Studio on a Mac, the extension method will be called.
If running it in Visual Studion on a Windows, it won't be called.

public static class MyHelpers
	public static TValue TryGetValue<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue = default(TValue))
		TValue value;
		return dictionary.TryGetValue(key, out value) ? value : defaultValue;

static class MyMainClass
	static void Main(string[] args)
		Dictionary<string, string> dictionary = null;
		var aa = dictionary?.TryGetValue("dfg"); // TryGetValue will be called!
Comment 1 Norbert Virth 2016-02-16 16:07:21 UTC
Created attachment 15060 [details]
Versio info provided by Xamarin Studio
Comment 2 Marek Safar 2016-02-16 20:16:53 UTC
Already fixed in Mono 4.3
Comment 3 Kevin B 2016-03-17 22:43:42 UTC
If it helps others in the future. I also ran into this issue with the following code:

public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
    if (source == null) throw new ArgumentNullException(nameof(source));

object[] foo = null;
foo?.ForEach(x => x.GetHashCode());

A notable difference is the method naming conflict is with the static ForEach method on System.Array rather than an instance method.

Upgrading from Mono 4.2.3 to Mono 4.4.0 does indeed resolve the problem. 
Renaming the extension method also works.