c# - elegant method of checking integer values in a list -


i have method returns monster object based on id.

if id in array, in returns definition.

it works! looks kind of hacky. ids might change, or might need new list of ids, , looks bad in opinion way have setup now.

is there way store integers someplace else in code, , this?

if (room.encounter.id.monstertype(dungeonmonstertypes)) 

here's code have now:

public static bool monstertype<t>(this t item, params t[] list) {     return list.contains(item); }  public static monsterdefinition getmonsterdefinition(this rgame room) {     var monster = room.encounter.monsterdefinition;     if (room.encounter.id.monstertype(190, 68, 115, 59, 40, 66, 112, 421))         return new monsterdefinition(monster);     return null; } 

use hashset -

hashset<int> monsterids = new hashset<int>() { 190, 68, 115, 59, 40, 66, 112, 421 }; ... if (monsterids.contains(room.encounter.id))   return new monsterdefinition(monster); 

you use enum instead of pure int's, more readable.


Comments

Popular posts from this blog

php - Embed BBCode within itself not working -

three.js - how to load gltf in three JS -

sql server - T-SQL: Access DB objects in same schema without qualifying? -