{"id":621,"date":"2022-05-06T16:32:12","date_gmt":"2022-05-06T09:32:12","guid":{"rendered":"https:\/\/csharptutorial.net\/?page_id=621"},"modified":"2023-04-04T13:15:44","modified_gmt":"2023-04-04T06:15:44","slug":"csharp-generic-constraints","status":"publish","type":"page","link":"https:\/\/www.csharptutorial.net\/csharp-tutorial\/csharp-generic-constraints\/","title":{"rendered":"C# Generic Constraints"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about C# generic constraints that allow you to specify what kinds of types are acceptable as arguments.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to C# generic constraints<\/h2>\n\n\n\n<p>The following example defines a <a href=\"https:\/\/csharptutorial.net\/csharp-tutorial\/csharp-generic-classes\/\">generic class<\/a> <code>Result&lt;T&gt;<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Result<\/span>&lt;<span class=\"hljs-title\">T<\/span>&gt;\n{\n    T Data { <span class=\"hljs-keyword\">get<\/span>; <span class=\"hljs-keyword\">set<\/span>; }\n    <span class=\"hljs-keyword\">int<\/span> Status { <span class=\"hljs-keyword\">get<\/span>; <span class=\"hljs-keyword\">set<\/span>; }\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-title\">Result<\/span>(<span class=\"hljs-params\">T data, <span class=\"hljs-keyword\">int<\/span> status<\/span>)<\/span>\n    {\n        Data = data;\n        Status = status;\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, the <code>Result&lt;T&gt;<\/code> only assigns <code>data<\/code> argument to <code>Data<\/code> member. It doesn&#8217;t do anything else that requires the operations of the type <code>T<\/code>. <\/p>\n\n\n\n<p>The reason is that the <code>Result&lt;T&gt;<\/code> doesn&#8217;t know the data of the type <code>T<\/code> it will store. Therefore, it can&#8217;t know the members of the type <code>T<\/code>. <\/p>\n\n\n\n<p>Since all objects are derived from the <code>object<\/code> class including <code>T<\/code>, the <code>Result&lt;T&gt;<\/code> class only knows the members of the <code>object<\/code> class such as <code>ToString()<\/code>, <code>Equals()<\/code>, and <code>GetType()<\/code>.<\/p>\n\n\n\n<p>As long as the <code>Result&lt;T&gt;<\/code> class does not access the member of the type <code>T<\/code> other than members of the <code>object<\/code> class, the <code>Result&lt;T&gt;<\/code> class can handle any type. This type parameter is called the <strong>unbounded type parameter<\/strong>.<\/p>\n\n\n\n<p>However, if you try to use any other members, the compiler will produce an error message. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Result<\/span>&lt;<span class=\"hljs-title\">T<\/span>&gt;\n{\n    T Data { <span class=\"hljs-keyword\">get<\/span>; <span class=\"hljs-keyword\">set<\/span>; }\n    <span class=\"hljs-keyword\">int<\/span> Status { <span class=\"hljs-keyword\">get<\/span>; <span class=\"hljs-keyword\">set<\/span>; }\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-title\">Result<\/span>(<span class=\"hljs-params\">T data, <span class=\"hljs-keyword\">int<\/span> status<\/span>)<\/span>\n    {\n        Data = data;\n        Status = status;\n    }\n\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">string<\/span> <span class=\"hljs-title\">JSON<\/span>(<span class=\"hljs-params\"><\/span>)<\/span> \n    {\n        <span class=\"hljs-keyword\">return<\/span> Data?.ToJSON(); <span class=\"hljs-comment\">\/\/ ERROR<\/span>\n    }\n\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This example causes an error because the <code>JSON()<\/code> method of the <code>Result&lt;T&gt;<\/code> class attempts to call the <code>ToJSON()<\/code> method of the <code>Data<\/code> member.<\/p>\n\n\n\n<p>To make the <code>Result&lt;T><\/code> class more useful, you can specify the information about the kind of types that is acceptable as arguments. This additional information is called <strong>generic constraints<\/strong>.<\/p>\n\n\n\n<p>To specify a generic constraint, you use the <code>where<\/code> clause after the closing angle bracket of the type parameter list:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">MyClass<\/span>&lt;<span class=\"hljs-title\">T<\/span>&gt; <span class=\"hljs-title\">where<\/span> <span class=\"hljs-title\">TypeParam<\/span>: <span class=\"hljs-title\">constraint<\/span>, <span class=\"hljs-title\">constraint<\/span>,<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>For example, the following specifies that the type T must be a type that implements the <code>IJSON<\/code> interface:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\">\n<span class=\"hljs-keyword\">interface<\/span> <span class=\"hljs-title\">IJSON<\/span>\n{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">string<\/span> <span class=\"hljs-title\">ToJSON<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>;\n}\n\n\n<span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Result<\/span>&lt;<span class=\"hljs-title\">T<\/span>&gt; \n       <span class=\"hljs-title\">where<\/span> <span class=\"hljs-title\">T<\/span> : <span class=\"hljs-title\">IJSON<\/span>\n{\n    T Data { <span class=\"hljs-keyword\">get<\/span>; <span class=\"hljs-keyword\">set<\/span>; }\n    <span class=\"hljs-keyword\">int<\/span> Status { <span class=\"hljs-keyword\">get<\/span>; <span class=\"hljs-keyword\">set<\/span>; }\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-title\">Result<\/span>(<span class=\"hljs-params\">T data, <span class=\"hljs-keyword\">int<\/span> status<\/span>)<\/span>\n    {\n        Data = data;\n        Status = status;\n    }\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">string<\/span> <span class=\"hljs-title\">JSON<\/span>(<span class=\"hljs-params\"><\/span>)<\/span> \n    {\n        <span class=\"hljs-keyword\">return<\/span> Data?.ToJSON();\n    }\n\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">C# Generic constraint types and order<\/h2>\n\n\n\n<p>C# provides you with five types of generic constraints:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Constraint Type<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td>ClassName<\/td><td>The type argument is the class of this type or classes derived from it.<\/td><\/tr><tr><td>class<\/td><td>The type argument can be any reference type including classes, arrays, delegates, and interfaces.<\/td><\/tr><tr><td>struct<\/td><td>The type argument can be any value type<\/td><\/tr><tr><td>InterfaceName<\/td><td>The type argument can be the interface or types that implement this interface<\/td><\/tr><tr><td>new()<\/td><td>The type argument with the parameterless public constructor.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>A generic type can have multiple constraints. In other words, it can have multiple <code>where<\/code> clauses like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">MyClass<\/span>&lt;<span class=\"hljs-title\">T1<\/span>, <span class=\"hljs-title\">T2<\/span>, <span class=\"hljs-title\">T3<\/span>&gt;\n    <span class=\"hljs-title\">where<\/span> <span class=\"hljs-title\">T2<\/span>: <span class=\"hljs-title\">MyClass2<\/span>\n    <span class=\"hljs-title\">where<\/span> <span class=\"hljs-title\">T3<\/span>: <span class=\"hljs-title\">IMyInterface<\/span>\n{\n   <span class=\"hljs-comment\">\/\/...<\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The order of the <code>where<\/code> clauses is not important. However, the constraints in a <code>where<\/code> clause matters. The following shows the order of the constraints:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Primary (0 or 1)<\/th><th>Secondary (0 or more)<\/th><th>Constructor (0 or 1)<\/th><\/tr><\/thead><tbody><tr><td>ClassName<br>class<br>struct<\/td><td>InterfaceName<\/td><td>new()<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Generic constraints specify constraints on the type used as arguments for type parameters in generic type.<\/li>\n\n\n\n<li>Use the <code>where<\/code> clauses to specify generic constraints.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"621\"\n\t\t\t\tdata-post-url=\"https:\/\/www.csharptutorial.net\/csharp-tutorial\/csharp-generic-constraints\/\"\n\t\t\t\tdata-post-title=\"C# Generic Constraints\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"621\"\n\t\t\t\tdata-post-url=\"https:\/\/www.csharptutorial.net\/csharp-tutorial\/csharp-generic-constraints\/\"\n\t\t\t\tdata-post-title=\"C# Generic Constraints\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn about C# generic constraints that allow you to specify what kinds of types are acceptable as arguments.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":7,"menu_order":66,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-621","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/621","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/comments?post=621"}],"version-history":[{"count":5,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/621\/revisions"}],"predecessor-version":[{"id":1385,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/621\/revisions\/1385"}],"up":[{"embeddable":true,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/7"}],"wp:attachment":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/media?parent=621"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}