Sign Up

Sign Up to our social questions and Answers to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In


Have an account? Sign In Now

Sign In

Login to our social questions & Answers to ask questions, answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Sorry, you do not have permission to add post.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Oraask Logo Oraask Logo
Sign InSign Up

Oraask

  • Write
    • Add A New Post
    • Ask A Question

Oraask Navigation

Search
Ask A Question

Mobile menu

Close
  • Categories
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Dev Tools
    • Online Compiler
    • Base64 Converter
  • Wiki
    • SQL Tutorials
    • Java Tutorials
    • Python Tutorials
    • JavaScript Tutorials
Home/ Questions/Q 74878
Next
In Process

Oraask Latest Questions

Question
TerryBittar
  • 0
  • 0
TerryBittarExplorer
Asked: September 8, 20212021-09-08T17:20:21+03:00 2021-09-08T17:20:21+03:00In: C#

How to check if List empty or null in C#

  • 0
  • 0

I have a list of items and it is maybe empty or null. I need to check if the list contains any List-item or is empty and if not then add an object to the List in C#?

// I have a list, but sometimes it doesn’t have any data added to it

var myList = new List<object>();
 
var myList = new List<object>();

// Expression is always false

if (myList == null)
Console.WriteLine(“List is never null”);
if (myList[0] == null)
myList.Add(“new item”);

//Errors encountered:  Index was out of range. Must be non-negative and less than the size of the collection.

// Inner Exception says “null”

c#
1
  • 1 1 Answer
  • 3k Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook

    Related Questions

    • Are lists mutable in C#
    • What is the use of FirstOrDefault in C#
    • How do you use isEmpty in C#
    • How do I know if IEnumerable has an item in C#
    • What is enumerable empty in C#

    1 Answer

    • Voted
    • Oldest
    • Recent
    1. Hassan AbdElrahman
      Hassan AbdElrahman Master Oracle ACE Pro ♠ | Oracle Senior ERP Technical Consultant
      2022-07-16T08:18:14+03:00Added an answer on July 16, 2022 at 8:18 am
      This answer was edited.

      Check if the list is empty in C#

      You can check whether the list is empty or not in many ways.

      • The first approach is to check for null, and if the count is greater than zero

       

      Example:

      if (strList != null && strList.Count > 0)
      {
      Console.WriteLine("The List has " + strList.Count + " records"); 
      }

      Output:

      The List has 10 records

       

      • The second approach is to use the LINQ method called “.any()”

       

      Example:

      if ((strList != null) && (!strList .Any()))
      {
      Console.WriteLine("The List has more than one record"); 
      }

      Output:

      The List has more than one record

      Do you find it helpful? Share to Spread Knowledge

        • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp

    Leave an answer
    Cancel reply

    You must login to add an answer.

    Forgot Password?

    Need An Account, Sign Up Here

    Sidebar

    Adv 250x250

    Explore

    • Categories
    • Questions
      • New Questions
      • Trending Questions
      • Must read Questions
      • Hot Questions
    • Dev Tools
      • Online Compiler
      • Base64 Converter
    • Wiki
      • SQL Tutorials
      • Java Tutorials
      • Python Tutorials
      • JavaScript Tutorials

    Footer

    Oraask

    About

    Oraask is a website for developers and software engineers who want to learn new skills, share their knowledge, and solve their coding problems. Oraask provides free content on various programming languages and topics, such as Oracle, Python, Java, etc. Oraask also allows users to ask questions and get answers from other members of the community.

    About Us

    • About Us
    • Contact Us

    Legal Stuff

    • Privacy Policy
    • Terms & Conditions

    Follow

    Oraask is licensed under CC BY-NC-SA 4.0Oraask CopyrightOraask CopyrightOraask CopyrightOraask Copyright

    © 2019 Oraask. All Rights Reserved
    With Love by Oraask.

    Insert/edit link

    Enter the destination URL

    Or link to existing content

      No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.