C#泛型的typedef [英] C# typedef generics

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

问题描述

我使用一类的简写版,它看起来像这样:

 使用NodeSteps =元组LT;节点,INT> ;; 



节点是自己定义的类。这工作通常是罚款,但这里的问题是,在该节点是一个通用的,需要一个结构



我的问题如下:



1。
如何这些所谓的typedef在C#中。我知道他们是不完全类型定义,但它是我能想到的最类似的事情。



2。
我怎样才能仿制

 使用NodeSteps< T> =元组LT;节点< T> ;, INT取代; 



我注意到这是不是这样做的方式。我也想指定T是一个结构。


解决方案

  1. 他们被称为别名


  2. 没有,这是不可能的。 C#语言规范:





使用别名可以命名一个封闭构造类型,但是不能命名。绑定的泛型类型声明没有提供类型参数




因此,用x< T> =列表< T> 或类似的东西是不可能的。



您可以使用一个类(见其他答案(S)),而不是


I am using a shorthand version for a class, which looks like this:

using NodeSteps = Tuple<Node, int>;

Node is a class defined by myself. This works fine usually, but the problem here is, is that Node is a generic requiring a struct.

My questions are as follows:

1. How are these typedefs called in C#. I know they are not exactly typedefs, but it was the most similar thing I could think of.

2. How can I make a generic version?

using NodeSteps<T> = Tuple<Node<T>, int>;

I noticed this is not the way to do it. I also would like to specify T is a struct.

解决方案

  1. They are called aliases.

  2. No, this isn't possible. C# Language spec:

Using aliases can name a closed constructed type, but cannot name an unbound generic type declaration without supplying type arguments.

Therefore, using x<T> = List<T> or something similar isn't possible.

You may use a class (see the other answers(s)) instead.

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

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