--- title: "Consuming Generics (C++/CLI) | Microsoft Docs" ms.custom: "" ms.date: "11/04/2016" ms.reviewer: "" ms.suite: "" ms.technology: - "cpp-windows" ms.tgt_pltfrm: "" ms.topic: "language-reference" dev_langs: - "C++" helpviewer_keywords: - "generics [C++], consuming from .NET languages" ms.assetid: e6330ef5-e907-432e-b527-7a22f5899639 caps.latest.revision: 14 author: "mikeblome" ms.author: "mblome" manager: "ghogen" translation.priority.ht: - "cs-cz" - "de-de" - "es-es" - "fr-fr" - "it-it" - "ja-jp" - "ko-kr" - "pl-pl" - "pt-br" - "ru-ru" - "tr-tr" - "zh-cn" - "zh-tw" --- # Consuming Generics (C++/CLI) Generics authored in one .NET language may be used in other .NET languages. Unlike templates, a generic in a compiled assembly still remains generic. Thus, one may instantiate the generic type in a different assembly and even in a different language than the assembly in which the generic type was defined. ## Example ### Description This example shows a generic class defined in C#. ### Code ``` // consuming_generics_from_other_NET_languages.cs // compile with: /target:library // a C# program public class CircularList { class ListNode { public ItemType m_item; public ListNode next; public ListNode(ItemType item) { m_item = item; } } ListNode first, last; public CircularList() {} public void Add(ItemType item) { ListNode newnode = new ListNode(item); if (first == null) { first = last = newnode; first.next = newnode; last.next = first; } else { newnode.next = first; first = newnode; last.next = first; } } public void Remove(ItemType item) { ListNode iter = first; if (first.m_item.Equals( item )) { first = last.next = first.next; } for ( ; iter != last ; iter = iter.next ) if (iter.next.m_item.Equals( item )) { if (iter.next == last) last = iter; iter.next = iter.next.next; return; } } public void PrintAll() { ListNode iter = first; do { System.Console.WriteLine( iter.m_item ); iter = iter.next; } while (iter != last); } } ``` ## Example ### Description This example consumes the assembly authored in C#. ### Code ``` // consuming_generics_from_other_NET_languages_2.cpp // compile with: /clr #using using namespace System; class NativeClass {}; ref class MgdClass {}; int main() { CircularList^ circ1 = gcnew CircularList(); CircularList^ circ2 = gcnew CircularList(); for (int i = 0 ; i < 100 ; i += 10) circ1->Add(i); circ1->Remove(50); circ1->PrintAll(); } ``` ### Output ``` 90 80 70 60 40 30 20 10 ``` ## See Also [Generics](../windows/generics-cpp-component-extensions.md)