[英]What does “Generic Type” mean? in .net : x.GetType().IsGenericType
I have found explanations for java Generic Types, but I do understand there are major differences between Java and .net's generic Types 我已经找到了Java通用类型的解释,但是我确实理解Java和.net的通用类型之间存在主要差异
Eg I run the following , but I cannot find out the true meaning of it. 例如,我执行以下操作,但我无法找出其真正含义。
List<int> list = new List<int>();
Console.WriteLine( list.GetType().IsGenericType);
The MSDN link simply states "Gets a value indicating whether the current type is a generic type.", but it does not state what generic type means MSDN链接仅声明“获取一个值,该值指示当前类型是否为通用类型。”,但未说明通用类型的含义。
Does Generic Type mean it is inherited from the Object class ? 泛型类型是否意味着它是从Object类继承的?
Does Generic Type have to do with Abstract classes? 泛型类型与Abstract类有关吗?
What is the definition of a .net Generic Type - how can I determine whether it is a Generic Type or not, from logic by myself ( in order to understand the concept) .net泛型的定义是什么-我如何根据自己的逻辑确定它是否为泛型(以了解概念)
To answer your two questions first: 首先要回答您的两个问题:
Object
. .NET中的所有类型,泛型和其他类型都源自Object
。 To determine whether a type is generic or not, just look a the class definition. 要确定类型是否为通用类型,只需查看类定义即可。 Queue
is NOT a generic type, while Queue<T>
is a generic type, because you can get different types by replacing the generic type parameter T
with any other type. Queue
不是通用类型,而Queue<T>
是通用类型,因为您可以通过将通用类型参数 T
替换为任何其他类型来获得不同的类型。 For example Queue<int>
, Queue<string>
, Queue<Object>
etc. all use the same code defined once in Queue<T>
class. 例如, Queue<int>
, Queue<string>
, Queue<Object>
等都使用在Queue<T>
类中一次定义的相同代码。 Notice that they are also generic types themselves. 请注意,它们本身也是通用类型。 Also nested types, which are nested in a generic type are considered to be generic types. 嵌套在通用类型中的嵌套类型也被视为通用类型。 The "base type" Queue<T>
is called a generic type definition . “基本类型” Queue<T>
被称为通用类型定义 。
public abstract class MyListBase { }
public abstract class MyListBase<T> : MyListBase { }
public class MyList<T> : MyListBase<T>
{
public class Nested { }
}
public class MyStringList : MyList<string> { }
... ...
var isGenericType0 = typeof(MyListBase).IsGenericType; //False
var isGenericType1 = typeof(MyListBase<>).IsGenericType; //True
var isGenericType2 = typeof(MyListBase<>)
.MakeGenericType(typeof(char)).IsGenericType; //True
var myIntegerList = new MyList<int>();
var isGenericType3 = myIntegerList.GetType().IsGenericType; //True
var myNested1 = new MyList<int>.Nested();
var isGenericType4 = myNested1.GetType().IsGenericType; //True
var myStringList = new MyStringList();
var isGenericType5 = myStringList.GetType().IsGenericType; //False
Hope you can wrap you head around generic types now. 希望您现在可以绕过泛型类型。
Generics let you tailor a method, class, structure, or interface to the precise data type it acts upon. 泛型使您可以根据其作用的确切数据类型定制方法,类,结构或接口。
A generic type definition is a class, structure, or interface declaration that functions as a template, with placeholders for the types that it can contain or use. 泛型类型定义是用作模板的类,结构或接口声明,带有可包含或使用的类型的占位符。 For example, the System.Collections.Generic.Dictionary class can contain two types: keys and values. 例如,System.Collections.Generic.Dictionary类可以包含两种类型:键和值。 Because a generic type definition is only a template, you cannot create instances of a class, structure, or interface that is a generic type definition. 因为通用类型定义只是模板,所以不能创建作为通用类型定义的类,结构或接口的实例。
public class Generic<T>
{
public T Field;
}
It's hard to describe what is Generic in .NET at answer. 在答案中很难描述什么是.NET中的泛型。 It's better to read documentation. 最好阅读文档。
In short, it's a templates in С++, Generic in Java, .NET, Delphi. 简而言之,它是С++中的模板,Java中的泛型,.NET,Delphi。 Common description here 此处常见说明
https://en.wikipedia.org/wiki/Generic_programming https://zh.wikipedia.org/wiki/Generic_programming
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.