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

html - Difficulties with background-image property -

visual studio code - What does the isShellCommand property actually do and how should you use it? -

ios - Segue not passing data between ViewControllers -