c# - Determine if an object is an array of any type, and access the array length -


i have overridden method has object parameter. determining if array, , want determine length:

public override bool isvalid(object value) {     type type = value.gettype();     if (type.isarray)     {         return ((object[]) value).length > 0;     }     else     {         return false;     } } 

the problem if value int[], errors when try cast object[]. there way handle cast work type of array?

cast value base system.array class:

return ((array) value).length > 0 

by using as operator, can simplify code further:

public static bool isvalid(object value) {     array array = value array;     return array != null && array.length > 0; } 

note: return true multidimensional arrays such new int[10, 10]. if want return false in case, add check array.rank == 1.


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -