简体   繁体   中英

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

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

Does Generic Type mean it is inherited from the Object class ?

Does Generic Type have to do with Abstract classes?

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)

To answer your two questions first:

  1. All types in .NET, generic types and others derive from Object .
  2. A generic type may be abstract just like any other type but it doesn't need to be. If you make an abstract generic type you need to derive a non-abstract type from it to be able to instantiate an object from it.

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. For example Queue<int> , Queue<string> , Queue<Object> etc. all use the same code defined once in Queue<T> class. 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 .

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. 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. It's better to read documentation.

In short, it's a templates in С++, Generic in Java, .NET, Delphi. Common description here

https://en.wikipedia.org/wiki/Generic_programming

Documentation from MSDN

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM