C#:如何创建“别名”;上课

编程入门 行业动态 更新时间:2024-10-22 23:43:59
本文介绍了C#:如何创建“别名”;上课的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我什至不知道它是否被称为别名,但无论如何让我继续。

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

您知道C#中的System.String类型是如何别名的与字符串?在Visual Studio中,字符串以小写和蓝色文本表示。我想对一些冗长的班级名称进行此操作。有些都像 RelocatedPlantProductReleaseItem。我无法更改其名称,但是这么长的名称确实会使代码冗长。我希望可以改用 Rppr。所以代替这个:

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();

我会这样:

Rppr Item = new Rppr();

我知道我可以创建一个名为Rppr的类,并将其从RelocatedPlantProductReleaseItem继承,但这似乎很麻烦。

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.

此外,当字符串表示System.String时,它在技术上叫什么?

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

更新:谢谢大家的回答,伙计们,但是我想我应该指定我希望这是一个全球性的事情。我不想在每个代码文件的顶部都指定它。我想在一个地方指定它,并且它可以在整个应用程序中的任何地方使用,甚至在引用其程序集的其他程序集中也可以。

Update: Thanks for all the answers, guys, but I guess I should have specified that 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.

推荐答案

使用using指令

using Rppr = Namespace.To.RelocatedPlantProductReleaseItem;

编辑明确要求全球解决方案。

EDIT Op clarified to ask for a global solution.

C#中没有 typedef 样式等效项,它可以创建类型名称的全局替换。您需要在每个要使用此缩写的文件中使用using指令,或使用长名称。

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.

更多推荐

C#:如何创建“别名”;上课

本文发布于:2023-10-26 18:59:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1531062.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:别名

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!