我发现Pascal的一个非常有用的特性是命名数据类型的能力,例如
type
person: record
name: string;
age: int;
end;
var
me: person;
you: person;
etc你能在C#中做类似的事情吗?我希望能够做一些像这样的事情
using complexList = List<Tuple<int,string,int>>;
complexList peopleList;
anotherList otherList;因此,如果我必须更改数据类型的定义,我可以在一个地方完成。
C#是否支持实现此目标的方法?
发布于 2012-02-13 17:26:02
这并不是你在Pascal中所做的,但是你可以使用using-directive。请在how to use it上查看此处
示例:
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();
}
}
}发布于 2012-02-13 17:22:44
是的,这是可能的。你可以这样写:
using System;
using System.Collections.Generic;
namespace ConsoleApplication12
{
using MyAlias = List<Tuple<int, string, int>>;
}或者,如果在命名空间之外声明:
using System;
using System.Collections.Generic;
using MyAlias = System.Collections.Generic.List<System.Tuple<int, string, int>>;
namespace ConsoleApplication12
{
}然后使用它作为一个类型:
MyAlias test = new MyAlias();发布于 2012-02-13 17:25:50
您可以创建一个类型:
class ComplexList : List<Tuple<int,string,int>> { }严格来说,这与别名并不相同,但在大多数情况下,您应该看不到任何区别。
https://stackoverflow.com/questions/9257989
复制相似问题