c# - Switch statement using string.contains -


i have following method

public list<availablefile> getavailablefiles(string rootfolder) {       if (directory.exists(rootfolder))      {           try          {              foreach (string f in directory.getfiles(rootfolder))              {                 if (f.tostring().contains("test"))                 {                       files = createfilelist(f);                 }             }         }         catch (system.exception excpt)         {            // log stuff         }    }    return files; } 

what i'm wanting refactor out if statement inside foreach loop, making using of switch statement need check against variety of different words

however, i'm struggling put switch statement in due use .contains() , fact need check each of file names may or may not have particular set of characters i'm looking for.

is there way in can make use of switch statement or stuck using variety of if statements?

edit maybe unclear in wanting do. , seem how go opinion based. below, quick mock of how i'm picturing this

public list<availablefile> getavailablefiles(string rootfolder) {     if (directory.exists(rootfolder))     {         log.info("checking folder: " + rootfolder + " files");         try         {             foreach (string f in directory.getfiles(rootfolder))             {                 // if statement removed method createfilelist                 files = createfilelist(f);                              }         }         catch (system.exception excpt)         {             log.fatal("getavailablefiles failed: " + excpt.message);         }     }     return files; }   // mock  private static list<availablefile> createfilelist(string entity) {     list<availablefile> filelist = new list<availablefile>();      // how can     if(entity.contains("test"))     {         run process 1     }     else if (entity.contains("run"))     {         run process 2     }      // way i'd prefer have     switch(entity)     {         case "test":             run process 1         break;         case "run":             run process 2         break;     }      return filelist; } 

if need use multiple if statements, shall, i'd prefer use switch. i'm trying possible c#?

switch(entity) {     case "test": 

only equal comparison

you need use

else if (entity.contains("run")) 

you might able create method returns function


Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -