Click here to Skip to main content
15,887,875 members
Articles / Programming Languages / C#

IsNullOrEmpty for Generic Collections in C#

,
Rate me:
Please Sign up or sign in to vote.
3.43/5 (18 votes)
12 Aug 2015CPOL 33.6K   11   22
IsNullOrEmpty for Generic Collections in C#

In this code snippet, we will see the creation and implementation of IsNullOrEmpty for collections.

Can I Implement IsNullOrEmpty for my Generic Collections/Lists?

It's out of C#. Unfortunately, there is only one method which is of a String method String.IsNullOrEmpty() available, framework does not provide us such things.

What is the Solution for this?

A solution is only an Extension Method, we just need to create an Extension Method which will provide the same facility: Here is the extension method:

C#
public static class CollectioncExtensionMethods  
    {  
        public static bool IsNullOrEmpty<T>(this IEnumerable<T> genericEnumerable)  
        {  
            return ((genericEnumerable == null) || (!genericEnumerable.Any()));  
        }  
  
        public static bool IsNullOrEmpty<T>(this ICollection<T> genericCollection)  
        {  
            if (genericCollection == null)  
            {  
                return true;  
            }  
            return genericCollection.Count < 1;  
        }  
    }

Enjoy the benefit of these extension methods. :)

C#
var serverDataList = new List<ServerData>();  

//It will use IsNullOrEmpty<T>(this ICollection<T> gnericCollection)
var result = serverDataList.IsNullOrEmpty();  
 
var serverData = new ServerDataRepository().GetAll();
              
// It will use IsNullOrEmpty<T>(this IEnumerable<T> genericEnumerable)  
var result = serverData.IsNullOrEmpty();

Can You Think What It Will Give When I Call serverDataList.Count and When I Call serverData.Count?

Hint: One will give me O(n) while other O(1), enjoy! :)

If still confused, view this video on .NET collection by legend author, Mr. Shivprasad Koirala.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer
India India
Learning never ends.

Written By
South Africa South Africa
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
D V L23-Aug-15 19:55
professionalD V L23-Aug-15 19:55 
AnswerRe: My vote of 5 Pin
Gaurav Aroraa23-Aug-15 21:56
professionalGaurav Aroraa23-Aug-15 21:56 
QuestionNot really a good idea Pin
William E. Kempf13-Aug-15 6:01
William E. Kempf13-Aug-15 6:01 
AnswerRe: Not really a good idea Pin
mtiede13-Aug-15 10:50
mtiede13-Aug-15 10:50 
GeneralRe: Not really a good idea Pin
codefabricator18-Aug-15 5:55
codefabricator18-Aug-15 5:55 
QuestionNice trick Pin
DotNetForAll13-Aug-15 3:57
DotNetForAll13-Aug-15 3:57 
AnswerRe: Nice trick Pin
Gaurav Aroraa13-Aug-15 4:00
professionalGaurav Aroraa13-Aug-15 4:00 
QuestionGreat Pin
irneb13-Aug-15 2:13
irneb13-Aug-15 2:13 
AnswerRe: Great Pin
Gaurav Aroraa13-Aug-15 3:53
professionalGaurav Aroraa13-Aug-15 3:53 
GeneralRe: Great Pin
irneb13-Aug-15 19:59
irneb13-Aug-15 19:59 
GeneralRe: Great Pin
Gaurav Aroraa13-Aug-15 20:11
professionalGaurav Aroraa13-Aug-15 20:11 
GeneralRe: Great Pin
irneb14-Aug-15 22:55
irneb14-Aug-15 22:55 
AnswerRe: Great Pin
Gaurav Aroraa15-Aug-15 22:49
professionalGaurav Aroraa15-Aug-15 22:49 
GeneralRe: Great Pin
irneb16-Aug-15 23:46
irneb16-Aug-15 23:46 
AnswerRe: Great Pin
Gaurav Aroraa17-Aug-15 1:18
professionalGaurav Aroraa17-Aug-15 1:18 
GeneralRe: Great Pin
irneb17-Aug-15 4:48
irneb17-Aug-15 4:48 
Could someone please test it on their systems? Is there really such a discrepancy between Mono and DotNet? If so it actually seems as if Mono performs better on average - perhaps I'm just doing something wrong here.

I've just redone this test on my Windows machine (W8.1 with VS 2013 CE):

Name                               Iterations             Seconds            Relative</h1>

ColIntNullAny                       536870912         1.413856600         1.000000000
ArrayIntNullLength                  536870912         1.413859000         1.000001697
EnumIntNullAny                      536870912         1.414000600         1.000101849
ArrayIntNullAny                     536870912         1.414018700         1.000114651
ColIntNullCount                     536870912         1.418061500         1.002974064
ArrayIntLength                      536870912         1.555343400         1.100071535
ArrayEx                             536870912         1.555374400         1.100093461
ColEx                               268435456         1.282051600         1.813552520
ColIntCount                         268435456         1.285510400         1.818445237
ColIntAny                           268435456         1.289521800         1.824119646
YieldIntAny                          67108864         1.397846600         7.909410898
ArrayIntAny                          67108864         1.550962800         8.775785607
EnumEx                               33554432         1.058709000        11.980949129
ColEnEx                              33554432         1.059109800        11.985484808
EnumIntAny                           33554432         1.060733900        12.003864041
ArEnEx                               33554432         1.768205000        20.010006673


AnswerRe: Great Pin
Gaurav Aroraa17-Aug-15 22:37
professionalGaurav Aroraa17-Aug-15 22:37 
GeneralRe: Great Pin
Gaurav Aroraa24-Aug-15 5:29
professionalGaurav Aroraa24-Aug-15 5:29 
GeneralMy vote 5 Pin
Shuby Arora12-Aug-15 10:56
Shuby Arora12-Aug-15 10:56 
GeneralRe: My vote 5 Pin
Gaurav Aroraa13-Aug-15 3:53
professionalGaurav Aroraa13-Aug-15 3:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.