c# - Why does this code work for different types? -


I'm working on some class room examples. This code works but I do not think why it works because I know that there is a normal type and this class applies but the item is just another class.

I'm sure this is what to do with normal, but I'm not really sure why.

Question: Why does this code allow two different types of permissions in the same list?

class definitions:

  public class items & lt; T & gt; : Item {} public square item {}   

code:

  static zero main (string [] args) {var list = new list & lt; Item & gt; (); List Add (new item & lt; int & gt; ()); List Add (new item & lt; double & gt; ()); The deceptive thing in your example is that both labels in the base and derived classes label " Item "However, with some name changes, your code is equivalent:  
  public square pet {} public square dog: pet {} public square cat: pet {} static zero main (string [ ] Args) {var list = new list & lt; Pet & gt; (); // Base type item list. Add (new dog); List Add (new cat); }   

Although two different types are being submitted in the list, they are obtained from the same base class. The key to understanding the code is that the list & lt; & Gt; There is a container for the base type, in this case "pet" - and in your example the base type is "item".

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -