Search code examples
c#extension-methodsusing

Is it possible to specify an assembly scoped using directive?


I have a couple of string extension methods in an assembly A. In assembly B, i'm not able to use these extension methods unless I add an using A directive in each of the .cs files in assembly B. Is there a way, in the B assembly project configuration to specify a using for the entire assembly? similar to c++ stdafx.h

Thanks


Solution

  • using includes a namespace, not an assembly. It's not necessary for a namespace to have the same name as its assembly (even if it is a common convention).

    If it's natural for you to want to include your string extensions across all of your assemblies because they represent common functionality, then just put them in a top-level namespace, e.g. MyCompanyName.MyApplication. These are then in scope from any other class in the same or a child namespace.

    (This does assume that all your namespaces follow the common practice of descending from a common root e.g. MyCompanyName....)