c# - Why do we need generics if all classes we used usually inherit from some common class? -


good day. generics used this:

class mylist<t> {     public t data;     public mylist<t> nextelement; } 

why not instead use follow:

class mylist {     public object data;     public mylist nextelement; } 

or even:

class mystructure<t> t : somecommonclass {     public t data;     public mystructure<t> nextelement;     public mystructure<t> prevelement; } 

and instead:

class mystructure {     public somecommonclass data;     public mystructure nextelement;     public mystructure prevelement; } 

update:

well, afraid not understood me correctly, , downgraded question. example without generics, work correctly:

public partial class form1 : form {     public form1()     {         initializecomponent();     }      private void form1_load(object sender, eventargs e)     {          mynode nodebutton = new mynode("nodebutton", new button());         mynode nodetextbox = new mynode("nodetextbox", new textbox());         mynode nodecheckbox = new mynode("nodecheckbox", new checkbox());         mylist mylist = new mylist() { nodebutton, nodetextbox, nodecheckbox };         (int = 0; < mylist.count;i++)         {             this.controls.add(mylist[i].data);             mylist[i].data.left = 100 * i;         }     } }  public class mynode {     public mynode(string name, control data)     {         data = data;         name = name;     }     public string name { get; private set; }     public control data { get; private set; } }  public class mylist : collection<mynode> {     protected override void insertitem(int index, mynode item)     {         base.insertitem(index, item);         item.data.mouseclick += new mouseeventhandler((sender, e) => { messagebox.show(item.name); });     } } 

and same example generics, generates error @ compile time:

public partial class form1 : form {     public form1()     {         initializecomponent();     }      private void form1_load(object sender, eventargs e)     {          mynode<button> nodebutton = new mynode<button>("nodebutton", new button());         mynode<textbox> nodetextbox = new mynode<textbox>("nodetextbox", new textbox());         mynode<checkbox> nodecheckbox = new mynode<checkbox>("nodecheckbox", new checkbox());         mylist mylist = new mylist() { (mynode<control>)nodebutton, (mynode<control>)nodetextbox, (mynode<control>)nodecheckbox };         (int = 0; < mylist.count;i++)         {             this.controls.add(mylist[i].data);             mylist[i].data.left = 100 * i;         }     } }  public class mynode<t> t : control {     public mynode(string name, t data)     {         data = data;         name = name;     }     public string name { get; private set; }     public t data { get; private set; } }  public class mylist : collection<mynode<control>> {     protected override void insertitem(int index, mynode<control> item)     {         base.insertitem(index, item);         item.data.mouseclick += new mouseeventhandler((sender, e) => { messagebox.show(item.name); });     } } 

according you, first version bad practice, because type safety broken, second version not allow type casting!

in both cases, can lose lot of information object, , it, lot of type safety. casting not pleasant. lose ability sure list only contains objects of type.

there’s significant performance loss when using value types; have boxed , unboxed out of object, slow, , again, not safe.

in fact, suggested did (well, still does) exist! called arraylist , horrible.


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -