We have a core library (CoreLibrary) where it really makes sense to be included in every assembly. There are currently a lot of extension methods where in each file I have to included the namespace via
using CoreLibrary;
Is there a possbility to implicitly include the using CoreLibrary
in each assembly so that I don't have to write it my own, like mscorlib does? The reason is not only to save typing it's also for users that are not aware that the extension methods exist.
The reason is following. I had an
interface IArray
{
int Size { get;}
// Returns data with a newly created array.
T[] GetData<T>();
// Returns data in an already existing array.
void GetData<T>(T[] data);
}
Before each class that implemented IArray had to implement both methods (and I have a few of them...)
class Array1D : IArray
{
public int Size { get;}
// Returns data with a newly created array.
T[] GetData<T>()
{
var data = new T[Size];
GetData(data);
return data;
}
// Returns data in an already existing array.
void GetData<T>(T[] data)
{
// Produce data
}
}
Now I thought that I could use default implementations in interfaces, so I could get rid of each completely identical implementation of GetData for each class that implements IArray.
interface IArray
{
int Size { get;}
// Returns data with a newly created array.
T[] GetData<T>()
{
var data = new T[Size];
GetData(data);
return data;
}
// Returns data in an already existing array.
void GetData<T>(T[] data);
}
So far so good, but know I always need to cast to IArray to get access to the default implementation which is not doable and it seems with default implementations there is now other way around. So the idea came up to use extension methods
public static class ArrayExtensions : IArray
{
public int Size { get;}
// Returns data with a newly created array.
public static T[] GetData<T>(this IArray array)
{
var data = new T[array.Size];
array.GetData(data);
return data;
}
}
Which works quite well, but now with the effect that I have to include the namespace, which is primarily OK, but before you could directly see via Intellisense what method you have, now you don't know about the extension method if you haven't used it before.