C# 定义类型别名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9257989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 06:34:53  来源:igfitidea点击:

Defining type aliases

c#types

提问by haughtonomous

One feature of Pascal I found very useful was the ability to name a data type, eg

我发现 Pascal 的一个非常有用的功能是能够命名数据类型,例如

type
 person: record
             name: string;
             age: int;
         end;

var
 me: person;
 you: person;

etc

Can you do something similar in C#? I want to be able to do something like

你能在 C# 中做类似的事情吗?我希望能够做类似的事情

using complexList = List<Tuple<int,string,int>>;

complexList peopleList;
anotherList otherList;

So that if I have to change the definition of the datatype, I can do it in one place.

这样如果我必须更改数据类型的定义,我可以在一个地方完成。

Does C# support a way to achieve this?

C# 是否支持实现这一目标的方法?

采纳答案by Mithrandir

It's not excatly what you do in Pascal, but you can use the using-directive. Have a look here on how to use it

这不是你在 Pascal 中所做的,但你可以使用using-directive。看看这里如何使用它

Example:

例子:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyList = Dummy2.CompleXList;

namespace Dummy2
{
    public class Person 
    {
    }

    public class CompleXList : List<Person> 
    { 
    }


    class Program
    {
        static void Main(string[] args)
        {
            MyList l1 = new MyList();
        }
    }
}

回答by ken2k

Yes, it's possible. You can write:

是的,这是可能的。你可以写:

using System;
using System.Collections.Generic;

namespace ConsoleApplication12
{
    using MyAlias = List<Tuple<int, string, int>>;
}

or, if declared outside the namespace:

或者,如果在命名空间之外声明:

using System;
using System.Collections.Generic;

using MyAlias = System.Collections.Generic.List<System.Tuple<int, string, int>>;

namespace ConsoleApplication12
{
}

then use it as a type:

然后将其用作类型:

MyAlias test = new MyAlias();

回答by flq

Yes you can do that, however you need to specify the full types, i.e. the definition becomes:

是的,您可以这样做,但是您需要指定完整类型,即定义变为:

using ComplexList = System.Collections.Generic.List<System.Tuple<int,string,int>>;

This is specified per file, much like the using directives for namespaces.

这是按文件指定的,很像命名空间的 using 指令。

nitpick: Conventionally, a type in .NET is PascalCased.

挑剔:通常,.NET 中的类型是 PascalCased。

回答by Jodrell

What about inheritance?

继承呢?

class ComplexList : List<Tuple<int, string, int>> {}

var complexList = new ComplexList();

This seems like a similar concept but there is no direct equivalent of a type based alias in C#, only namespace aliases.

这似乎是一个类似的概念,但在 C# 中没有基于类型的别名的直接等价物,只有namespace aliases

If you want to avoid type name clashing in your code, namespace aliasing is the way to go.

如果您想避免代码中的类型名称冲突,命名空间别名是一种可行的方法。

If you want to make a new type which "is a" instance of another type, inheritance is one option.

如果你想创建一个“是”另一种类型的实例的新类型,继承是一种选择。

回答by Serge Wautier

You can create a type:

您可以创建一个类型:

class ComplexList : List<Tuple<int,string,int>> { }

This is not strictly the same as an alias but in most cases, you shouldn't see any differences.

这与别名并不完全相同,但在大多数情况下,您应该看不到任何差异。

回答by BrainSlugs83

From the syntax shown in the initial question, it looks like you're really just asking how to make a class in C#, and not how to alias a type.

从初始问题中显示的语法来看,您似乎只是在问如何在 C# 中创建类,而不是如何为类型设置别名。

If you want a simpler name to type than List<Tuple<int,string,int>>, and you want it to be "global" (i.e. not per-file), I would create a new class that inherited said class and declared no additional members. Like this:

如果您想要一个比 更简单的名称List<Tuple<int,string,int>>,并且您希望它是“全局的”(即不是每个文件),我将创建一个新类,该类继承了该类并且不声明其他成员。像这样:

public class MyTrinaryTupleList: List<Tuple<int,string,int>> { }

That gives one single location of management, and no need for additional using statements.

这提供了一个单一的管理位置,并且不需要额外的 using 语句。

However, I would also take it a step further, and venture that you probably don't want a Tuple, but rather another class, such as:

但是,我也会更进一步,并冒险说您可能不想要元组,而是另一个类,例如:

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    public int FavoriteNumber { get; set; }

    public Person() { }

    public Person(string name, int age, int favoriteNumber) 
    { 
        this.Name = name; 
        this.Age = age; 
        this.FavoriteNumber = favoriteNumber; 
    }
}

And then for your list type you can do the following:

然后对于您的列表类型,您可以执行以下操作:

public class PersonList: List<Person> { }

In addition, if you need other list specific helper properties or methods you can add them to the PersonList class as well.

此外,如果您需要其他特定于列表的帮助器属性或方法,您也可以将它们添加到 PersonList 类。

回答by juFo

Just a simple using would do:

只需一个简单的使用就可以:

using Foo = System.UInt16;

public class Test {
  public Foo Bar { get;set; }
}