Project
ReSharper
Priority
Normal
Type
Feature
Fix versions
Mirabile Futurum
State
Open
Assignee
Evgeny Pasynkov
Subsystem
Psi
Affected versions
No Affected versions
Fixed in build
No Fixed in build
  • Created by   Michael D. Norman
    5 years ago (05 Aug 2006 00:39)
  • Updated by   Michael D. Norman
    3 years ago (27 Nov 2008 00:09)
  • Jira: RSRP-3932
    (history, comments)
 
RSRP-3932 Optimize Usings shouldn't remove aliases
2
Issue is visible to: All Users
  The issue is visible to the selected user group only
Because I've been burned so much in the Java world, I'm a big fan of using aliases instead of the using entire namespaces. For example:

using Array = System.Array;
using Hashtable = System.Collections.Hashtable; using ICloneable = 
System.ICloneable; using ICollection = System.Collections.ICollection; 
using IEnumerable = System.Collections.IEnumerable; using IEnumerator = System.Collections.IEnumerator; using SerializableAttribute = 
System.SerializableAttribute; 

rather than

using System;
using System.Collections;


The reason for this is so that I know EXACTLY what I'm using, especially when I'm only using a couple of classes in a namespace. I don't think that optimize usings should remove these aliases and replace them with the namespace.

I also hope that more and more C# developers will start using this method. Unfortunately, most sample code (as was the old days of Java) uses namespaces rather than classes. This would be made easier by automatically adding using alias statements for individual classes how IDEA does it.
Comments (1)
 
History
 
Linked Issues (?)
 
TeamCity Changes (0)
 
Nikolay Shustov
  Nikolay Shustov
27 Nov 2008 00:09
3 years ago
The type aliases are extermely useful in C# too in the case the fully (or partially) qualified type name has to be used to to resolve the type specification.
If there are more than one element of the qualified name has to be specified then the replacement of the aliased types dramatically reduces the readablity of the code.

Example:

using MyEnum = MyNamespaceOne.MyNamespaceTwo.MyAnotherType.MyNestedType.MyEnum;
namespace MyNamespaceOne.MyNamespaceTwo
{
class MyType
{
class MyNestedType
{
void Foo()
{
MyEnum a = MyEnum.Member;
}
}
}
}

Would be replaced with:

namespace MyNamespaceOne.MyNamespaceTwo
{
class MyType
{
class MyNestedType
{
private enum MyEnum { ... };
void Foo()
{
MyAnotherType.MyNestedType.MyEnum a = MyAnotherType.MyNestedType.MyEnum.Member;
}
}
}
}

The workaround is to use the different name for the alias and uncheck "Do not remove using alias directives if alias name differs from teh imported type name" in ReSharper > Options > C# > Namespaces Imports :

using ImportedEnum = MyNamespaceOne.MyNamespaceTwo.MyAnotherType.MyNestedType.MyEnum;
namespace MyNamespaceOne.MyNamespaceTwo
{
class MyType
{
class MyNestedType
{
void Foo()
{
ImportedEnum a = ImportedEnum.Member;
}
}
}
}

Although the latter won't be changed by refactoring but it reduces the readablity of the code too - so I voted for that ReSharper feature.