1

I want to create a collection class that can collect any type of data (string, int, float). So I decided to use a List<object> structure to store any kind of data.

Since using a List structure is safer (managed) than creating an unmanaged array, I want to create a List structure so it can hold any kind of data... but I have some concerns that if I create a List<object> structure and try to hold some strings, there could be memory leaks because of string type..

So do I have to do somethings after using (emptying) the List and deallocate the strings individualy or does .Net already handle that...

Is there a nicer method for creating general collection class?

4
  • Possible answer on memory management using Java: stackoverflow.com/questions/1567979/how-to-free-memory-in-java Commented Jun 19, 2015 at 11:00
  • Try using dynamic list, see if this helps. stackoverflow.com/questions/6600810/… Commented Jun 19, 2015 at 11:03
  • 7
    What makes you think that using a string type will result in a memory leak in .Net? .Net will handle the deallocation and cleanup of memory on your behalf and in this circumstance you do not need to consider it. Commented Jun 19, 2015 at 11:06
  • 6
    You seem to have a little misconception. If you create an array in C# (e.g. string[]), it's not "unmanaged". The garbage collector manages the array. Commented Jun 19, 2015 at 11:07

1 Answer 1

1

You won´t need to GarbageCollect any objects on your own as long as you really need to, but according to your post that´s not the case here, actually this is only necessary in a few cases (you may look here what these cases might be). However .NET frees any memory to which no references exist (indepenedend if you have an int, a string or any custom object), thus if you leave the scope of your array, list or whatever you use the contained elements will be eliminated at some none-deterministic point of time by the GC, but you won´t take care for this.

What you mean by managed and unmanaged is probably the fact, that a List is a bit more dynamic as it can change its size depending on the current number of elements. If this number exceeds a given maximum the list automatically increaes by a given factor. An array is fixed in size however. The term "unmanaged" however relies to C++ e.g., C#-Code is allways managed code (which means there is a garbage-collector e.g.). See Wikipedia on Managed Code

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.