Sign Up

Sign Up to our social questions and Answers Engine 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 Engine 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

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3841592
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T15:40:28+00:00 2026-05-19T15:40:28+00:00

According to the MSDN documentation on the List<T>.Clear method : This method is an

  • 0

According to the MSDN documentation on the List<T>.Clear method:

This method is an O(n) operation,
where n is Count.

Why O(n)? I ask because I would assume that clearing a List<T> could be accomplished simply by allocating a new T[] array internally. No other class could have a reference to this array, so I fail to see the harm in this approach.

Now, maybe this is a stupid question… is allocating a T[] array itself O(n)? For some reason I would not have thought so; but maybe it is (is my lack of a C.S. degree showing right now?). If so, I suppose that would explain it since, according to the same documentation quoted above, the capacity of the list remains unchanged, which means an array of equal size would need to be constructed.

(Then again, this doesn’t seem like it could be the correct explanation, as then the documentation should have said “where n is Capacity“—not Count*).

I just suspect that this method, rather than allocating a new array, zeroes out all elements of the current one; and I’m curious to know why this would be.

*Hans Passant pointed out in a comment to LukeH’s answer that the docs are correct. Clear only zeroes out the elements that have been set in the List<T>; it does not need to “re-zero” all the elements past those.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T15:40:28+00:00Added an answer on May 19, 2026 at 3:40 pm

    As far as I’m aware the current implementation just calls Array.Clear on its internal T[] array, and Array.Clear is an O(n) process. (As Hans points out in the comments, the MSDN docs are correct that the n in this case is the list’s Count, not its Capacity.)

    But, even if it did just allocate a new T[] array internally, that would still be an O(n) process because allocating an array of size n involves initialising all n elements to their zero/null/default state.

    Of course, there’s nothing to stop some sort of internal trickery where the array could be initialised with length 0 or 42 or whatever and then auto-expanded again on-the-fly as required, amortising the overall O(n) cost.

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

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

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.