1

Iam using a library which uses a class named "Transaction". The problem is that i want to define a class in my project with the same name too.

I know about aliases, but the problem is that it looks really ugly. Is there a way to use aliases only for a specific type? So i dont have to use Alias.Class for every class in the library, but only when i use Transaction?

Example (Class1,Class2 and Transaction are in the same library directive):

Class1 obj1 = new Class1();
Class2 obj2 = new Class2();
Library.Transaction obj3 = new Library.Transaction();
Loading
  • 1,098
  • 1
  • 12
  • 25
  • Do you use both types withion the same source-code-file? If not a usual `using MyNamespace` would be enough to reference your own `Transaction`-class. – MakePeaceGreatAgain May 14 '18 at 13:34
  • the problem was that i use Library.Transaction nowhere, but Library.Class1 pretty much everywhere. Now i wanted to use my own Transaction class somewhere but i got ambiguity between Library.Transaction and my own. Using the marked solution is preventing this. – Loading May 14 '18 at 13:44

3 Answers3

2

Write aliases in the top of your code (using part):

using ExternalTransaction = Library.Transaction;

or maybe something less confusing:

using Library_Transaction = Library.Transaction;
//Or using TransactionAlias = Library.Transaction;

and then use it like:

ExternalTransaction et = new ExternalTransaction();
//Library_Transaction lt = new Library_Transaction();
//TransactionAlias ta = new TransactionAlias();

DotNetPerl's example

SᴇM
  • 7,024
  • 3
  • 24
  • 41
  • Then you are actually reducing the redability all-together. Moreover, hitting F12 (goto definition) would take you to namespace alias rather than the actuall class file – Rahul May 14 '18 at 13:26
  • @Rahul: I disagree. It's more readable to use two differently named classes (when correctly and aptly named, of course); compared to using two classes with the same name (but a different namespace). Also, the F12 argument is not an argument. The same can be said for using interfaces instead of direct classes (F12 takes you to the interface, not the class), but that's no argument against using interfaces. – Flater May 14 '18 at 13:28
  • @Rahul: It is either changing the Class Names, using fully qualified names or Type Aliases (for the fully qualified names). Considering we have 3-4 "Timer" classes in .NET, it is not even a uncommon problem. – Christopher May 14 '18 at 13:28
  • Well yes, it can sometimes reduce readability if use it heavily and can be extremely confusing, but you can name aliases less confusing: for example `Library_Transaction` or `TransactionAlias` to be more readable. – SᴇM May 14 '18 at 13:34
1

If Class1,Class2 and Transaction are in same namespace then no, you can't since you alias the entire namespace. You can then either fully qualify the type saying Namespace.Class or define a separate namespace for the type

Rahul
  • 76,197
  • 13
  • 71
  • 125
1

This is not a uncommon problem. Indeed we have those issues in particular with the different "Timer" classes. Of wich there are at least 4 in the .NET Framework. It is all dandy, unless you use one of the other times Namespaces by accident (wich can happen easily).

For those cases there are 3 options:

  1. Rename the classes so their partial name is unambigious
  2. Stop using both (or all) using directives. Use fully qualified names insteand
  3. Option 2, just use type Aliases to prevent you from having to write the fully qualified name at every singe point. It also allows you to swap out the types in a single place.
Christopher
  • 9,634
  • 2
  • 17
  • 31