{"id":74131,"date":"2022-08-29T08:00:07","date_gmt":"2022-08-29T06:00:07","guid":{"rendered":"https:\/\/code-maze.com\/?p=74131"},"modified":"2022-08-29T08:46:11","modified_gmt":"2022-08-29T06:46:11","slug":"using-generics-in-csharp","status":"publish","type":"post","link":"https:\/\/code-maze.com\/using-generics-in-csharp\/","title":{"rendered":"Using Generics in C#"},"content":{"rendered":"<p>Generic programming is a powerful tool that C# offers to developers. In this article, we&#8217;ll take a look at how generics work in C# and some of the benefits they provide. We&#8217;ll also see how to use generics with classes, methods, and delegates. Finally, we&#8217;ll look at some of the drawbacks of using generics and ways to work around them.\u00a0<\/p>\n<p>While we go in-depth in this article with generics, we do have an introductory article on <a href=\"https:\/\/code-maze.com\/csharp-generics\/\" target=\"_blank\" rel=\"noopener\">Generics<\/a> if this is a new topic to you.<\/p>\n<div style=\"padding: 20px; border-left: 5px #dc2323 solid; display: block; margin-bottom: 20px; box-shadow: 1px 1px 5px 0px lightgrey;\">To download the source code for this article, you can visit our <a href=\"https:\/\/github.com\/CodeMazeBlog\/CodeMazeGuides\/tree\/main\/csharp-advanced-topics\/GenericsInCSharp\" target=\"_blank\" rel=\"nofollow noopener\">GitHub repository<\/a>.<\/div>\n<p>Let&#8217;s start!<\/p>\n<h2><a id=\"what-is-a-generic-class\"><\/a>What Are Generics in C#?<\/h2>\n<p>In C#, Generics is a concept that makes it possible for programmers to implement classes and methods that defer the specification of their types until the classes and methods are instantiated by the client code.<\/p>\n<p>For example, a generic class might be created for handling customer data, with specific methods for adding, removing, and updating customer records. In this case, the compiler generates the code to handle the type it encounters as we instantiate the class.\u00a0<\/p>\n<h2><a id=\"why-use-generics\"><\/a>Why Use Generics?<\/h2>\n<p>Generic classes and methods are essential as they make it possible for programmers to enjoy benefits such as <strong>reusability, type safety, and efficiency<\/strong>, unlike their non-generic counterparts.\u00a0<\/p>\n<p>This makes them ideal for use with collections and methods which utilize them to implement business logic.\u00a0\u00a0<\/p>\n<p>In C#, the <code>System.Collections.Generic<\/code> namespace contains generic-based collection classes. However, we can also create generic custom types to implement our generalized solutions and design patterns that enjoy the benefits of <strong>type safety<\/strong> and <strong>efficiency<\/strong>.\u00a0<\/p>\n<h2><a id=\"how-do-generics-work\"><\/a>How Do Generics Work in C#?<\/h2>\n<p>To understand how generics work in C#, we are going to implement some generic classes and methods.<\/p>\n<p>First, let&#8217;s start by creating a simple generic class to help us manipulate generic array objects:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public class GenericArray&lt;T&gt;\r\n{\r\n    private T[] _arrayObj;\r\n\r\n    public GenericArray(int size)\r\n    {\r\n        _arrayObj = new T[size + 1];\r\n    }\r\n\r\n    public T RetrieveValue(int index)\r\n    {\r\n        return _arrayObj[index];\r\n    }\r\n\r\n    public void InsertValue(int index, T value)\r\n    {\r\n        _arrayObj[index] = value;\r\n    }\r\n}<\/pre>\n<p>Here, we define a generic class <code>GenericArray<\/code> of type <code>T<\/code>, which helps us define custom methods to help us manipulate array objects. We denote the generic type as <code>T<\/code> since we use it as a placeholder for the type that the compiler encounters at runtime. However, we need to specify the type when instantiating the generic class.<\/p>\n<p>In the class, we define a generic type variable <code>_arrayObj<\/code> that holds the array values and a constructor to instantiate the class. Next, we define two public methods to insert and retrieve array elements by accepting the index of the array and the generic value as inputs.<\/p>\n<p>We can then proceed to verify that we can generate arrays of different types:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">var intArray = new GenericArray&lt;int&gt;(5);\r\nvar guidArray = new GenericArray&lt;Guid&gt;(5);\r\nvar rand = new Random();\r\n\r\nfor (int i = 0; i &lt; 5; i++) \r\n{\r\n    intArray.InsertValue(i, rand.Next());\r\n}\r\n\r\nfor (int i = 0; i &lt; 5; i++)\r\n{\r\n    guidArray.InsertValue(i, Guid.NewGuid());\r\n}\r\n\r\nAssert.IsInstanceOfType(intArray.RetrieveValue(1), typeof(int));\r\nAssert.IsInstanceOfType(guidArray.RetrieveValue(1), typeof(Guid));<\/pre>\n<p>Here, we can see that we can use the <code>GenericArray<\/code> class to instantiate array objects of different types, which the compiler determines at runtime. Generics provide compile-time type safety as the solution would not build successfully when we attempt to <strong>insert a value of a different type <\/strong>such as a <code>string<\/code> into the <code>intArray<\/code> object.\u00a0<\/p>\n<h2><a id=\"generic-delegates\"><\/a>Generic Methods<\/h2>\n<p>Next, let&#8217;s create generic methods with type parameters to understand how they work. To keep things simple, let&#8217;s write a method to swap elements given two generic type variables:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public void SwapElements(ref T left, ref T right)\r\n{\r\n    T tempVar;\r\n    tempVar = left;\r\n    left = right;\r\n    right = tempVar;\r\n}<\/pre>\n<p>This method accepts two generic type variables we pass by reference and swaps them by using a <code>tempVar<\/code> variable to facilitate the swap process.<\/p>\n<p>We can verify that the <code>SwapElements()<\/code> method works with different data types at runtime using this unit test:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">var firstInt = 1;\r\nvar lastInt = 9;\r\nvar firstChar = 'a';\r\nvar lastChar = 'z';\r\nvar integerObj = new GenericMethods&lt;int&gt;();\r\nvar charObj = new GenericMethods&lt;char&gt;();\r\n\r\nintegerObj.SwapElements(ref firstInt, ref lastInt);\r\ncharObj.SwapElements(ref firstChar, ref lastChar);\r\n\r\nAssert.AreEqual(firstInt, 9);\r\nAssert.AreEqual(lastInt, 1);\r\nAssert.AreEqual(firstChar, 'z');\r\nAssert.AreEqual(lastChar, 'a');<\/pre>\n<h2><a id=\"generic-delegates\"><\/a>Generic Delegates<\/h2>\n<p>We can use generics when we want to utilize <a href=\"https:\/\/code-maze.com\/delegates-charp\/\" target=\"_blank\" rel=\"noopener\">delegates<\/a> in C#. We are aware that delegates help us hold references to methods and access them without using their actual names.\u00a0<\/p>\n<p>However, we also know that when we have many methods and need to encapsulate them using delegates, we need to define a delegate for each method. Creating many delegates may end up making our codebases difficult to maintain and reducing application performance.\u00a0<\/p>\n<p><strong>Generic delegates come in handy as we can use the same delegate to hold references to multiple methods.<\/strong> To make this concept easy to understand, let&#8217;s define two methods to add and multiply two numbers and an additional method to print the results:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public int AdditionFunc(int num1, int num2)\r\n{\r\n    return num1 + num2;\r\n}\r\n\r\npublic int MultiplicationFunc(int num1, int num2 )\r\n{\r\n    return num2 * num1;\r\n}\r\n\r\npublic void PrintString(string stringVal) \r\n{\r\n    Console.WriteLine(stringVal);\r\n}<\/pre>\n<p>Both <code>AdditionFunc<\/code> and <code>MultiplicationFunc<\/code> take two integer values and return their sum and product respectively.<\/p>\n<p>Next, we are going to\u00a0define a generic delegate in the <code>Main()<\/code> method to test these methods:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">delegate T ArithmeticDelegates&lt;T&gt;(T num1, T num2);\r\n\r\nstatic void Main(string[] args)\r\n{\r\n    var delegateObj = new GenericDelegates();\r\n    var addition = new ArithmeticDelegates&lt;int&gt;(delegateObj.AdditionFunc);\r\n    var multiplication = new ArithmeticDelegates&lt;int&gt;(delegateObj.MultiplicationFunc);\r\n\r\n    var num1 = 5;\r\n    var num2 = 10;\r\n\r\n    var printOutput = $\"The sum of {num1} and {num2} is {addition(num1, num2)}\";\r\n    delegateObj.PrintString(printOutput);\r\n\r\n    printOutput = $\"The multiplication of {num1} and {num2} is {multiplication(num1, num2)}\";\r\n    delegateObj.PrintString(printOutput);\r\n}\r\n<\/pre>\n<p>Let&#8217;s understand how the code works.\u00a0<\/p>\n<p>First, we define a generic delegate <code>ArithmeticDelegates<\/code> that accepts two generic type input parameters and returns one generic output type parameter.<\/p>\n<p>In the next step, we create the <code>delegateObj<\/code> object of <code>GenericDelegates<\/code> type and pass the <code>AdditionFunc()<\/code> and <code>MultiplicationFunc()<\/code> methods to our delegate.<\/p>\n<p>Finally, to verify that all the methods work as expected, we create two number variables and print the results of both the Addition and Multiplication methods.<\/p>\n<h2><a id=\"features-of-generics\"><\/a>Features of Generics<\/h2>\n<p>After implementing generic classes and methods in our example, we learn that generics enrich our programs in multiple ways.<\/p>\n<p>First, it helps us maximize the principles of code reusability, type safety, and performance.\u00a0<\/p>\n<p>Besides that, the technique allows us to create generic collection classes, which are available in the <code>System.Collections.Generic<\/code> namespace.\u00a0<\/p>\n<p>The technique also helps us define generic interfaces, classes, methods, events, and delegates.<\/p>\n<p>Another way generics enrich our programs is their ability to help us enable access to methods that require specific data types.\u00a0<\/p>\n<p>Finally, the generics help us get information on the types used in a generic data type at compile-time, through the <a href=\"https:\/\/code-maze.com\/csharp-reflection\/\" target=\"_blank\" rel=\"noopener\">reflection<\/a> technique.<\/p>\n<h2><a id=\"advantages-of-generics\"><\/a>Advantages of Generics<\/h2>\n<p>From our <a href=\"#how-do-generics-work\">examples<\/a>, we can see that we can <strong>reuse<\/strong> our code snippets without modifying them thanks to generics. This makes our code base simple to understand and modify when the need arises.\u00a0<\/p>\n<p>Besides reusability, we learn that we benefit from <strong>type safety<\/strong> as we need to specify the types of objects we intend to pass to our generic methods and classes. Passing objects of different types to the same generic object results in a compilation error.<\/p>\n<p>Finally, generic types have been known to <strong>perform<\/strong> better than their standard type system counterparts since the former types don&#8217;t require boxing, unboxing, and type casting.\u00a0<\/p>\n<h2><a id=\"disadvantages-of-generics\"><\/a>Disadvantages of Generics<\/h2>\n<p>First, we cannot use generics with lightweight dynamic methods as generics are determined at compile time while dynamics are resolved at runtime.<\/p>\n<p>Besides that, we cannot use generic type parameters with enumerations. Generic enumerations can only occur in special circumstances such as nesting it in a generic type.\u00a0<\/p>\n<p>On top of that, .NET does not support context-bound generic types. It means we can derive generic types from <code>ContextBoundObject<\/code>, but trying to create an instance of that type causes a <code>TypeLoadException<\/code>.<\/p>\n<p>Finally, we can&#8217;t instantiate nested types that are enclosed in generic types unless we assign them to the type parameters of all enclosing types.\u00a0<\/p>\n<h2><a id=\"conclusion\"><\/a>Conclusion<\/h2>\n<p>In this article, we have learned how generics work in C#. They come in handy when we want to maximize code reuse, type safety, and performance. They make it possible for us to define our own generic interfaces, classes, methods, events, and delegates.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Generic programming is a powerful tool that C# offers to developers. In this article, we&#8217;ll take a look at how generics work in C# and some of the benefits they provide. We&#8217;ll also see how to use generics with classes, methods, and delegates. Finally, we&#8217;ll look at some of the drawbacks of using generics and [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":62189,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[12],"tags":[956,951,367,366],"class_list":["post-74131","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-csharp","tag-generic","tag-generic-delegates","tag-generic-methods","tag-generics","et-has-post-format-content","et_post_format-et-post-format-standard"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Using Generics in C# - Code Maze<\/title>\n<meta name=\"description\" content=\"In this article, we are going to learn about Generics in C#, covering what they are, why we should use them, along with some examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/code-maze.com\/using-generics-in-csharp\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Using Generics in C# - Code Maze\" \/>\n<meta property=\"og:description\" content=\"In this article, we are going to learn about Generics in C#, covering what they are, why we should use them, along with some examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/code-maze.com\/using-generics-in-csharp\/\" \/>\n<meta property=\"og:site_name\" content=\"Code Maze\" \/>\n<meta property=\"article:published_time\" content=\"2022-08-29T06:00:07+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-08-29T06:46:11+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1100\" \/>\n\t<meta property=\"og:image:height\" content=\"620\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Code Maze\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/CodeMazeBlog\" \/>\n<meta name=\"twitter:site\" content=\"@CodeMazeBlog\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Code Maze\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":[\"Article\",\"BlogPosting\"],\"@id\":\"https:\/\/code-maze.com\/using-generics-in-csharp\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/using-generics-in-csharp\/\"},\"author\":{\"name\":\"Code Maze\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04\"},\"headline\":\"Using Generics in C#\",\"datePublished\":\"2022-08-29T06:00:07+00:00\",\"dateModified\":\"2022-08-29T06:46:11+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/code-maze.com\/using-generics-in-csharp\/\"},\"wordCount\":1134,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/using-generics-in-csharp\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"keywords\":[\"generic\",\"Generic Delegates\",\"generic methods\",\"generics\"],\"articleSection\":[\"C#\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/code-maze.com\/using-generics-in-csharp\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/code-maze.com\/using-generics-in-csharp\/\",\"url\":\"https:\/\/code-maze.com\/using-generics-in-csharp\/\",\"name\":\"Using Generics in C# - Code Maze\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/code-maze.com\/using-generics-in-csharp\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/using-generics-in-csharp\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"datePublished\":\"2022-08-29T06:00:07+00:00\",\"dateModified\":\"2022-08-29T06:46:11+00:00\",\"description\":\"In this article, we are going to learn about Generics in C#, covering what they are, why we should use them, along with some examples.\",\"breadcrumb\":{\"@id\":\"https:\/\/code-maze.com\/using-generics-in-csharp\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/code-maze.com\/using-generics-in-csharp\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/using-generics-in-csharp\/#primaryimage\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"width\":1100,\"height\":620,\"caption\":\"C# Development\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/code-maze.com\/using-generics-in-csharp\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/code-maze.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Using Generics in C#\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/code-maze.com\/#website\",\"url\":\"https:\/\/code-maze.com\/\",\"name\":\"Code Maze\",\"description\":\"Learn. Code. Succeed.\",\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/code-maze.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/code-maze.com\/#organization\",\"name\":\"Code Maze\",\"url\":\"https:\/\/code-maze.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"width\":3511,\"height\":3510,\"caption\":\"Code Maze\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/CodeMazeBlog\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04\",\"name\":\"Code Maze\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez-150x150.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez-150x150.png\",\"caption\":\"Code Maze\"},\"description\":\"This is the standard author on the site. Most articles are published by individual authors, with their profiles, but when several authors have contributed, we publish collectively as a part of this profile.\",\"sameAs\":[\"https:\/\/www.linkedin.com\/company\/codemaze\/\",\"https:\/\/x.com\/https:\/\/twitter.com\/CodeMazeBlog\"],\"url\":\"https:\/\/code-maze.com\/author\/codemazecontributor\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Using Generics in C# - Code Maze","description":"In this article, we are going to learn about Generics in C#, covering what they are, why we should use them, along with some examples.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/code-maze.com\/using-generics-in-csharp\/","og_locale":"en_US","og_type":"article","og_title":"Using Generics in C# - Code Maze","og_description":"In this article, we are going to learn about Generics in C#, covering what they are, why we should use them, along with some examples.","og_url":"https:\/\/code-maze.com\/using-generics-in-csharp\/","og_site_name":"Code Maze","article_published_time":"2022-08-29T06:00:07+00:00","article_modified_time":"2022-08-29T06:46:11+00:00","og_image":[{"width":1100,"height":620,"url":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","type":"image\/png"}],"author":"Code Maze","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/CodeMazeBlog","twitter_site":"@CodeMazeBlog","twitter_misc":{"Written by":"Code Maze","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["Article","BlogPosting"],"@id":"https:\/\/code-maze.com\/using-generics-in-csharp\/#article","isPartOf":{"@id":"https:\/\/code-maze.com\/using-generics-in-csharp\/"},"author":{"name":"Code Maze","@id":"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04"},"headline":"Using Generics in C#","datePublished":"2022-08-29T06:00:07+00:00","dateModified":"2022-08-29T06:46:11+00:00","mainEntityOfPage":{"@id":"https:\/\/code-maze.com\/using-generics-in-csharp\/"},"wordCount":1134,"commentCount":0,"publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"image":{"@id":"https:\/\/code-maze.com\/using-generics-in-csharp\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","keywords":["generic","Generic Delegates","generic methods","generics"],"articleSection":["C#"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/code-maze.com\/using-generics-in-csharp\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/code-maze.com\/using-generics-in-csharp\/","url":"https:\/\/code-maze.com\/using-generics-in-csharp\/","name":"Using Generics in C# - Code Maze","isPartOf":{"@id":"https:\/\/code-maze.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/code-maze.com\/using-generics-in-csharp\/#primaryimage"},"image":{"@id":"https:\/\/code-maze.com\/using-generics-in-csharp\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","datePublished":"2022-08-29T06:00:07+00:00","dateModified":"2022-08-29T06:46:11+00:00","description":"In this article, we are going to learn about Generics in C#, covering what they are, why we should use them, along with some examples.","breadcrumb":{"@id":"https:\/\/code-maze.com\/using-generics-in-csharp\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/code-maze.com\/using-generics-in-csharp\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/using-generics-in-csharp\/#primaryimage","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","width":1100,"height":620,"caption":"C# Development"},{"@type":"BreadcrumbList","@id":"https:\/\/code-maze.com\/using-generics-in-csharp\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/code-maze.com\/"},{"@type":"ListItem","position":2,"name":"Using Generics in C#"}]},{"@type":"WebSite","@id":"https:\/\/code-maze.com\/#website","url":"https:\/\/code-maze.com\/","name":"Code Maze","description":"Learn. Code. Succeed.","publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/code-maze.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/code-maze.com\/#organization","name":"Code Maze","url":"https:\/\/code-maze.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","width":3511,"height":3510,"caption":"Code Maze"},"image":{"@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/CodeMazeBlog"]},{"@type":"Person","@id":"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04","name":"Code Maze","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/person\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez-150x150.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez-150x150.png","caption":"Code Maze"},"description":"This is the standard author on the site. Most articles are published by individual authors, with their profiles, but when several authors have contributed, we publish collectively as a part of this profile.","sameAs":["https:\/\/www.linkedin.com\/company\/codemaze\/","https:\/\/x.com\/https:\/\/twitter.com\/CodeMazeBlog"],"url":"https:\/\/code-maze.com\/author\/codemazecontributor\/"}]}},"_links":{"self":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/74131","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/comments?post=74131"}],"version-history":[{"count":4,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/74131\/revisions"}],"predecessor-version":[{"id":74159,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/74131\/revisions\/74159"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media\/62189"}],"wp:attachment":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media?parent=74131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/categories?post=74131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/tags?post=74131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}