c# - Extension Method on a null List with inner instantiation -


can use extension method on list may null, , if case instanciate inside extension method??

instanciate list inside extension method applies on it... sounds when try add or remove item list your iterating foreach loop.

public static void addorupdate(this list<blabla> i, person p) { = ?? new list<blabla>(); //is ok no instantiate inside? i.removeall(t => t.id == p.id && t.role == roleenum.admin); i.add(p); //since reference type, dont need return (even "this" parameter) right? } 

and use this:

//list<blabla> teamwork comes anywhere else, instantiated or not teamwork.addorupdate(apersona); teamwork.addorupdate(apersonb); dosomething(teamwork); 

no, not work, parameter should passed ref , can't use ref combined this keyword.

but can likle this:

public static list<blabla> addorupdate(this list<blabla> i, person p) {     = ?? new list<blabla>();     i.removeall(t => t.id == p.id && t.role == roleenum.admin);     i.add(p);     return i; }  teamwork = teamwork.addorupdate(apersona); teamwork = teamwork.addorupdate(apersonb); dosomething(teamwork); 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -