Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to specify which extension method to use for extension methods of the same name in different namespaces? [duplicate]

A simple example using the code snippet below:

using System.Data;
using CustomerNameSpace;
...
...
CDataSet.CustomerDataTable dtCustomer = GetCustomer();

var customersWithName = dtCustomer.AsEnumerable()
    .Where(x => x.Name != null)
    .CopyToDataTable();

For some reason, my coworker has created an extension method CopyToDataTable() within the CustomerNameSpace.

The program in this case is using both namespaces System.Data and CustomerNameSpace.

Both now contains the extension method CopyToDataTable().

In the sample snippet below, is there a way to specify which extension methods from these two namespaces to use?

like image 564
IvanJazz Avatar asked Sep 03 '25 03:09

IvanJazz


1 Answers

If you absolutely need both namespaces in your code, the only way to differentiate is to call the method as a 'normal' static method instead of an extension method:

var customers = dtCustomer.AsEnumerable()
    .Where(x => x.Name != null);
CustomerNameSpace.MyExtensionsClass.CopyToDataTable(customers);
like image 118
jeroenh Avatar answered Sep 05 '25 00:09

jeroenh