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

  • SEARCH
  • Home
  • 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 862165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:05:50+00:00 2026-05-15T09:05:50+00:00

In the .NET framework, many of the System.Collection classes have Clear methods on them.

  • 0

In the .NET framework, many of the System.Collection classes have Clear methods on them. Is there a clear advantage on using this versus replacing the reference with a new object?

Thanks.

  • 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-15T09:05:50+00:00Added an answer on May 15, 2026 at 9:05 am

    You’d want to use Clear if you have other references to the same object, and you want to keep them all pointing to the same object.

    For example maybe you have a worker queue where you store tasks to do. And in one or more threads you take work items out of this queue (of course you use locking to make sure you access the queue with at most one thread at a time). If at some point you want to empty the queue, then you can use Clear and all threads will still point to the same object.

    As seen here when you use Clear all items will be removed, and the Count will be 0, but the Capacity will remain unchanged. Usually the Capacity being unchanged is a good thing (for efficiency), but there could be some extreme case that you had a ton of items and you want that memory to be eventually freed.

    The MSDN link above also mentions that Clear is an O(n) operation. Whereas simply replacing the reference will be an O(1) operation and then eventually it will be garbage collected but possibly not right away. But replacing the reference also means that the memory that makes up the capacity will need to be re-allocated.

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

Sidebar

Related Questions

I have seen many examples of using .NET Framework namespaces in c++/cli like the
Microsoft claims that the .Net 3.5 framework has many speed improvements over 2.0. Is
After downloading all .NET framework symbols and sources using NetMassDownloader , is it possible
I'm using Entity Framework in Visual Studio 2010 Beta 2 (.NET framework 4.0 Beta
I understand and appreciate the usefulness of the System.WeakReference class in the .NET framework,
I'm trying to debug some code in the .NET Framework. Unfortunately, many of the
Using Visual Studio 2008, and I'm trying to use some of the .net Framework
I am developing a web application using asp.net framework 4.0. I am using so
I'm going to write an ftp client using .NET Framework. I want to upload
.NET Framework: 2.0 Preferred Language: C# I am new to TDD (Test Driven Development).

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.