在C#中输入别名 [英] Type alias in C#

查看:60
本文介绍了在C#中输入别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许我很愚蠢或者我错过了明显的,但是我找不到一种方法

在C#中为原始类型定义类型别名(类和接口我可以

继承,没问题)。我想要声明一个别名的类型

,比方说,int。从概念上讲,这就是我想要做的事情:


public MyIntType = int; //不会编译


任何人都知道怎么做?


-

问候, Peter

解决方案

在基于类文件的基础上,您可以使用(在顶部)


使用MyIntType = System.Int32;


即我想声明一个类型,例如,int


嗯,你要声明哪个:类型还是别名?它们是相互独立的。但是,你不能对结构进行子类化。


Marc


那么,你要宣告哪一种:一种类型还是别名?它们是


exclusive。但是,你不能将结构子类化。



C#有什么区别?在Delphi(我最有经验的地方),

您通过以下方式创建别名:


类型

MyIntType =整数;


和另一种类型的新类型:


类型

MyIntType =类型整数;


我很感兴趣知道C中提供的不同选项


-

问候,彼得


好吧,在类中例如,两个语法演示将是


使用SomeAlias = Full.Namespace.To.BaseClass;


vs.


类SomeClass:BaseClass {}

答:第一个(别名)纯粹是单个来源中的编译器技巧

文件*,允许两件事:

1 :(更常见)避免文件中的名称冲突,而不必使用完全限定名称,例如
,例如你有一个名为(奇怪)的自定义类

XmlDocument,你正在使用System.Xml,所以你可能别名两个

(或两者)中的一个,例如

使用MyXmlDocument = My.Namespace.XmlDocument;


现在这意味着在你的类文件中你可以使用

MyXmlDocument doc =新的MyXmlDocument();

等没有冲突的风险,*但*编译的IL只知道

My.Namespace.XmlDocument。

2 :(不常见)允许程序员更改支持类型。

有时泛型可以提供帮助,但请注意泛型不尊重

算术(或其他)运营商。所以,如果你不确定你是否需要很长的

或int,你可以使用

使用SomeAlias = System.Int32;

和然后

SomeAlias值= 0;

值+ = 5;



如果你改变主意以后你可以只将别名更改为Int64。

*如果*仍支持所有操作,那么它将编译并运行,

(但可能会破坏你的界面) 。


B:第二个(继承)定义程序集中继承的新类型

。这将允许仅在一个方向上自动投射,并且可能*不会显着改变行为,但不是一个好主意。

除了存在之外凌乱,它不能与界面,静态,密封,

struct等工作。


Marc


Maybe I''m stupid or maybe I am missing the obvious, but I can''t find a way
to define a type alias in C# for primitive types (class and interfaces I can
inherit from, no problem). I.e I want to declare a type that is an alias
for, say, int. Conceptually, this is what I want to do:

public MyIntType = int; // won''t compile

Anyone knows how to do this?

--
Regards, Peter

解决方案

On a class-file -by- class-file basis you can use (at the top)

using MyIntType = System.Int32;

I.e I want to declare a type that is an alias for, say, int

Well, which do you want to declare: a type or an alias? They are mutually
exclusive. You can''t subclass a struct, however.

Marc


Well, which do you want to declare: a type or an alias? They are mutually

exclusive. You can''t subclass a struct, however.

What is the difference in C#? In Delphi (where I have the most experience),
you create an alias by doing:

type
MyIntType = integer;

and a new type deriving from another type with:

type
MyIntType = type integer;

I''d be interested knowing the different options available in C#

--
Regards, Peter


Well, in the "class" case, the two syntax demos would be

using SomeAlias = Full.Namespace.To.BaseClass;

vs.

class SomeClass : BaseClass {}

A: The first (alias) is purely a compiler trick *within a single source
file*, to allow two things:
1: (more commonly) to avoid name conflicts within a file without having to
use fully qualified names, e.g. you have a custom class called (bizarrely)
XmlDocument, and you are using System.Xml, so you might alias one of the two
(or both), e.g.
using MyXmlDocument = My.Namespace.XmlDocument;

this now means that in your class file you can use
MyXmlDocument doc = new MyXmlDocument();
etc without the risk of conflicts, *but* the compiled IL only knows about
My.Namespace.XmlDocument.

2: (less common) to allow the programmer to change the backing type.
Sometimes generics can help, but note that generics do not respect
arithmetic (or other) operators. So if you weren''t sure if you needed long
or int, you could use
using SomeAlias = System.Int32;
and then
SomeAlias value = 0;
value += 5;
etc
And if you change your mind later you can change just the alias to Int64.
*If* all the operations are still supported, then it will compile and work,
(but may break your interfaces).

B: the second (inheritance) defines a new type in your assembly inherited
from the old. This will allow automatic casting in one direction only, and
*possibly* won''t change the behaviour significantly, but is not a good idea.
As well as being messy, it won''t work with interface, static, sealed,
struct, etc.

Marc


这篇关于在C#中输入别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆