c# Generics problem -


Can anyone tell me why this does not work? I would have thought that the obstacle would validate it.

  public class squares & lt; T & gt; Where T: icdomain {public class () {classbi b = new square b (); IEnumerable & LT; T & gt; Alist = new list & lt; T & gt; B Item = goodbye; // Error: IEnumerable & lt; T & gt; Can not be changed from IEnumerable & lt; ICommon & gt; }} Public Class Classbie {PUBLIC INIMERABLE & lt; Icimon and gt; Item {get; Set;}}}  

This is a tweak with in C # 4 Will work, but not in C # 3. What you are looking for is normal behavior , which is in CLR Supported by NET 2.0, but not in v4 to v4.

Even in C # 4, you need to stop the t as a reference type - too because the value does not work on value types. For example, list & lt; Int & gt; to the IEnumerable & lt; IComparable & gt; , even if int implies IComparable .

To touch your code a bit (effectively there were some typo), it compiled with the C # 4 compiler:

  using System.Collections.Generic; Public interface ICommon {} Note / note the "class" part of the banned Lok class class "A" & lt; T & gt; Where T: Class, IComman {public square () {classbby = new square b (); IEnumerable & LT; T & gt; Alist = new list & lt; T & gt; (); B Item = goodbye; }} Public Class Classbie {PUBLIC INIMERABLE & lt; Icimon and gt; Item {get; If you are stuck in C # 3 and .NET 3.5, then an option  Cast < T & gt; () : 

  b.Items = alist.Cast & lt; ICommon & gt; ();  

Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -