vendredi 1 septembre 2017

Simplify c# if/else statement with different List<>

Is there any way to simplify the following code? supportList is an object which is getting from constructor:

 public void FillCollection(object supportList)

Code here:

 if (supportList is List<Bank>)
        {
            foreach (var temp in supportList as List<Bank>)
            {
              //code here

            }
        }
        else if (supportList is List<Currency>)
        {
            foreach (var temp in supportList as List<Currency>)
            {
                //code here

            }
        }
        else if (supportList is List<Amount>)
        {
            foreach (var temp in supportList as List<Amount>)
            {
                //code here

            }
        }

EDIT 1: All foreach parts do the same code like

foreach (var temp in supportList as List<Bank>)
            {
                string yesno;
                if (temp.Id == CurrentId)
                {
                    yesno = "" + yes;
                }
                else
                {
                    yesno = "" + no;
                }

                CollectionSupport.Add(new SupportModel { Id = temp.Id, Name = temp.Name, Code = temp.Code, YesNo = "" + yesno });

            }

Aucun commentaire:

Enregistrer un commentaire