{"id":3817,"date":"2014-03-30T05:52:32","date_gmt":"2014-03-30T05:52:32","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/generic-type-parameter-in-netc-whose-value-can-by-one-of-specified-types-collection-of-common-programming-errors\/"},"modified":"2014-03-30T05:52:32","modified_gmt":"2014-03-30T05:52:32","slug":"generic-type-parameter-in-netc-whose-value-can-by-one-of-specified-types-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/generic-type-parameter-in-netc-whose-value-can-by-one-of-specified-types-collection-of-common-programming-errors\/","title":{"rendered":"Generic type parameter in .net(C#) whose value can by one of specified types?-Collection of common programming errors"},"content":{"rendered":"<p>Lets say I have classes <code>ClassA<\/code>, <code>ClassB<\/code>, <code>ClassC<\/code>. All of them inherit from <code>object<\/code> and non of them implements any interface.<\/p>\n<p>Is it possible in C# to declare generic interface that will take generic argument <code>T<\/code> and that <code>T<\/code> may be either <code>ClassA<\/code> or <code>ClassB<\/code> or <code>ClassC<\/code> ?<\/p>\n<p>So I would have something like<\/p>\n<pre><code>public interface MyInterface where T: ClassA | ClassB | ClassC {\n    ...\n}\n<\/code><\/pre>\n<p>If it is possible what&#8217;s the syntax?<\/p>\n<p><em>Note:<\/em> I know I could have those classes <code>ClassA<\/code>, <code>ClassB<\/code>, <code>ClassC<\/code> to implement some interface and than use it as constrain of <code>T<\/code> but before I create an interface that have no methods I want to know that there is no better way.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Lets say I have classes ClassA, ClassB, ClassC. All of them inherit from object and non of them implements any interface. Is it possible in C# to declare generic interface that will take generic argument T and that T may be either ClassA or ClassB or ClassC ? So I would have something like public [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3817","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3817","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=3817"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3817\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3817"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3817"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3817"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}