You can create a generic extension method to check if an array is null or empty.
Given the following code:
public class Foo
{
private anyClass[] anyClassField;
public anyClass[] car
{
get
{
return this.anyClassField;
}
set
{
this.anyClassField = value;
}
}
}
public class anyClass
{
// add properties here ....
}
You can create an extension method like this:
public static class CollectionExtensions
{
public static bool IsNullOrEmptyCollection<T>(this T[] collection)
{
if (collection == null)
return true;
return collection.Length == 0;
}
}
Using the code (don't forget to include the namespace of the CollectionExtensions
class):
var foo = new Foo();
// returns true
bool isEmpty = foo.car.IsNullOrEmptyCollection();
// add 1 element to the array....
foo.car = new [] { new anyClass() };
// returns false
isEmpty = foo.car.IsNullOrEmptyCollection();