1

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.

msedi
  • 1,437
  • 15
  • 25
  • 1
    I can't imagine that this is possible. It isn't even possible with the most fundamental .Net namespaces, like "System", which needs a using in every source file. Besides that, "implicit usings" are no good idea to me. This puts your namespace on par with the "god namespace" where only the language's own types reside, like "int" or "bool". – Heinz Kessler May 15 '20 at 07:02
  • 3
    "like mscorlib does" - meaning what? even `using System;` isn't implicit - it needs to be per-file; can you clarify with an example of what you mean? but no, in regular C# implicit using directives aren't a thing; you *can* do that in razor (cshtml) – Marc Gravell May 15 '20 at 07:05
  • note: mscorlib is effectively "forwarded" to System.Private.CoreLib these days, and: internally it uses regular per-file using directives; example chosen purely at random: https://github.com/dotnet/runtime/blob/b1b407a9bfb0d4960d958246ab328711057858d1/src/libraries/System.Private.CoreLib/src/System/Activator.cs; there is no external magic for people *consuming* mscorlib – Marc Gravell May 15 '20 at 07:10
  • 1
    @Sinatr that's a using directive *alias*; different scenario, although the answer would still be "no, it is per file/namespace" – Marc Gravell May 15 '20 at 07:16
  • A solution: [switch to VB.NET](https://stackoverflow.com/a/2480329/11683)... – GSerg May 15 '20 at 07:19
  • I have posted some clarification of why I need it, maybe there's a better approach I don't see. – msedi May 15 '20 at 07:20
  • 1
    @GSerg a language where the official position is ["Going forward, we do not plan to evolve Visual Basic as a language."](https://devblogs.microsoft.com/vbteam/visual-basic-support-planned-for-net-5-0/) – Marc Gravell May 15 '20 at 07:21
  • 1
    @msedi re the edit: consider that **LINQ** - a first level language feature (if you use the `from x in foo` syntax, etc) - which is usually based on extension methods: still requires you to have the correct `using` directives. This seems like a documentation problem, more than anything else... – Marc Gravell May 15 '20 at 07:23
  • @MarcGravell: In the end that's true, but Intellisense is a good way to train people. They can directly see what they have available. For extension methods it's a bit more complicated. You need to know them. – msedi May 15 '20 at 07:26

1 Answers1

1

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?

K, there's 3 different things we need to tease apart here, but ultimately I believe the answer is simply "no", but...

  1. "in each assembly" - using directives are at their widest per-file (they can also be per-namespace scope inside a file) - not per assembly (or even per type, thanks to partial); it cannot be applied globally
  2. at the assembly level, you would need a reference, not a using directive - presumably a package reference; this is something that can be applied globally, by putting the <PackageReference> (or similar) in a Directory.Build.props file that is in a higher directory in the build tree (note: having the reference does not do anything to change the need for a per-file using directive; it is "necessary but not sufficient")
  3. "like mscorlib does?" - mscorlib does no such thing
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900