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 8467405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:38:06+00:00 2026-06-10T15:38:06+00:00

if (!someList.Contains(new listItem(arg1, args2))) { // Do some stuff } For the code segment

  • 0
if (!someList.Contains(new listItem(arg1, args2)))
{
    // Do some stuff
}

For the code segment above, someList is a list of structs and listItem is the struct.

Does using the new operator in this context result in a memory leak? Or is this an example of bad practice in general? I have searched around on SO and Google, but wasn’t able to find a question that specifically addressed this.

  • 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-06-10T15:38:07+00:00Added an answer on June 10, 2026 at 3:38 pm

    You have to work really hard to create a memory leak in C#. It’s possible, but there are not very many cases where it happens accidentally (other than when inter-oping with other languages that don’t have such straightforward memory management). I won’t go into the edge cases here though.

    Here is what the memory will look like for that object:

    • a local variable will be created in whatever method holds the code you’ve shown. Since you didn’t set the new structure to a local variable explicitly, it will have some name that can’t be referenced in code.
    • It will be passed, via the stack, to the method that you call. (It will be passed by value, so there will now be two variables that have the same structure at this point).
    • When Contains returns the parameters on it’s call stack will be “freed”, so we’re now back to just having one variable.
    • When the method you’re currently in finishes (or possibly a bit earlier, if the compiler wants to optimize it knowing it’s no longer used) the stack space of the structure will be “freed”

    Even though the new keyword is used, the structure is not created on the heap. It’s still created on the stack. In C# new is used when calling any constructor explicitly (barring reflection) and does not indicate an allocation on the heap (the way it does in, say, C++).

    It’s also worth mentioning that even if this were a class, and not a structure, it wouldn’t result in any memory leak. The object would end up on the heap, but the garbage collector will take care of cleaning it up at some point after it’s no longer needed. You’re not doing anything here that would result in holding onto a reference to the object once it’s not needed (and I know that Contains isn’t going to do any shenanigans that would prevent it from being collected; if it was some unknown function it’s possible but unlikely that they’d do something mean).

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

Sidebar

Related Questions

I've the following code: var e = someList.GetEnumerator(); var a = new List<Foo>(); var
Assuming I have an object A containing // ... private List<double> someList = new
I have a dropdown, that contains some list of StudentCode... when user select any
On my html page I have a dropdown list: <select name=somelist> <option value=234234234239393>Some Text</option>
Consider: List<String> someList = new ArrayList<>(); // add "monkey", "donkey", "skeleton key" to someList
My application contains some list of *.apk files. I know only the application(example.apk) name.
My code will be more clear I think- someList = list() foo = {'a':'b'}
Is it safe to do the following? std::list<Something> someList; ... someList.push_back(Something(8)); Something* something =
I have dynamically created some list of RadioButtons with some values. pro grammatically I
I have an activity which shows some List entries. When I click on a

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.