oscilatingcretin
oscilatingcretin

Reputation: 10959

C#: How to create "aliases" for classes

I don't even know if it's called an alias, but let me continue anyway.

You know how the System.String type in C# is sorta "aliased" with "string"? In Visual Studio, "string" is in lowercase and blue text. I'd like to do this to some of our lengthy class names. Some are all like "RelocatedPlantProductReleaseItem". I can't change the name of it, but such a long name really makes the code long-winded. I'd like to be able to use "Rppr" instead. So instead of this:

RelocatedPlantProductReleaseItem Item = new RelocatedPlantProductReleaseItem();

I'd go like:

Rppr Item = new Rppr();

I know that I can create a class called Rppr and have it inherit from RelocatedPlantProductReleaseItem, but that seems hacky.

Also, the scenario I gave above is totally fictitious. I just used it to demonstrate the essential functionality I am trying to achieve.

Also, what is it technically called when "string" represents System.String?

I want this to be a global thing. I don't want to have to specify it at the top of every code file. I want to specify it in one place and it work everywhere in the entire application, even in other assemblies that reference its assembly.

Upvotes: 13

Views: 16761

Answers (8)

Johnathan Barclay
Johnathan Barclay

Reputation: 20372

As of C#10, it is now possible to do this globally by using the global modifier.

global using Rppr = Namespace.To.RelocatedPlantProductReleaseItem;

This can be added to any file in the compilation and then applies to all other files, but must be added before any using directives that do not have the global modifier.

Upvotes: 2

JaredPar
JaredPar

Reputation: 755587

Use a using directive

using Rppr = Namespace.To.RelocatedPlantProductReleaseItem;

EDIT Op clarified to ask for a global solution.

There is no typedef style equivalent in C# which can create a global replacement of a type name. You'll need to use a using directive in every file you want to have this abbreviation or go for the long version of the name.

Upvotes: 40

Henk Holterman
Henk Holterman

Reputation: 273854

You can add a using directive at the top of your file:

 using Rppr = MyProjectNs.RelocatedPlantProductReleaseItem;

But it (only) works per file, not for a Project.

Upvotes: 1

Vladimir
Vladimir

Reputation: 3689

You create an alias using:

using alias = LongClassName;

That alias is visible in the namespace where you declared it.

Upvotes: 1

tam
tam

Reputation: 1583

try to add the following with your other usings

using Rppr = YourItemsNamespace.RelocatedPlantProductReleaseItem;

Upvotes: 0

Incognito
Incognito

Reputation: 16597

You can do this :

using Rppr = RelocatedPlantProductReleaseItem;

Upvotes: 2

dlev
dlev

Reputation: 48606

string is a convenience offered by the C# language to refer to the actual CLR type System.String.

You can achieve similar results with a using directive. At the top of the file where you'd like the name shortened, add this line:

using ShortName = Namespace.ReallyLongClassNameGoesHereOhGodWhy;

Then you can say ShortName myVar = new ShortName();

Note that you have to do this on a per file basis. You can't get the same coverage as string with a single declaration.

Upvotes: 7

Bala R
Bala R

Reputation: 109027

Try

using Rppr = SomeNamespace.RelocatedPlantProductReleaseItem;

Reference

Upvotes: 4

Related Questions