0

I'm writing a class where I have a collection of objects of class A and I'm writing a function similar to Dictionary<TKey,TValue>.TryGetValue(TKey,TValue)

It looks pretty similar to this:

public class MyCollection
{
   public List<A> Collection {get; set;} = new List<A>();

   public bool TryGetPropertyAByPropertyB(string pPropertyB, out string pPropertyA)
   { … }
}

I'm just wondering if I replaced List for IList, would it be more efficient (talking about memory)?

public class MyCollection
{
   public IList<A> Collection {get; set;} = new List<A>();

   public bool TryGetPropertyAByPropertyB(string pPropertyB, out string pPropertyA)
   { … }
}
Carlos
  • 786
  • 7
  • 13
  • 4
    No - it's going to be the same `List` instance whether you call it a`List` or an `IList` – Wai Ha Lee Jan 08 '20 at 16:37
  • 2
    `IList` is only an interface and doesn't have any implementation. You're comparing apples and volkswagons. –  Jan 08 '20 at 16:37
  • Does this answer your question? [List or IList](https://stackoverflow.com/questions/400135/listt-or-ilistt) – Shahid Manzoor Bhat Jan 08 '20 at 16:37
  • There *may* be a small memory difference if there is code which currently uses a method on `List`, and there's no equivalent method on `IList` (such as `BinrarySearch`), and this code then needs to buffer the elements in the list. But there's no difference in the memory usage of the actual `List`. – canton7 Jan 08 '20 at 16:38
  • No. DI will just make any IList a List at runtime –  Jan 08 '20 at 16:38
  • Unless you change the actual data structure (e.g. changing `new List();` to some other custom data structure), there will be no difference. – Arthur Attout Jan 08 '20 at 16:39
  • 2
    You need to go all the way back and ask yourself 1) if you need to care about memory and 2) if you do, how you plan to identify actual problems. Picking at random bits of code and wondering if option A or option B might use more memory is a counterproductive waste of time -- even if the answer is "yes", you still have no idea of the actual impact. – Jeroen Mostert Jan 08 '20 at 16:39
  • 3
    perhaps interestingly (who knows), there *will* be a difference if you `foreach` over the data, due to how `foreach` is implemented (duck-typing, custom iterators, etc); but most of the time: no difference – Marc Gravell Jan 08 '20 at 16:56

1 Answers1

4

No, a reference of type List<T> and a reference of type IList<T> are of the exact same size, usually 32 or 64 bits depending on the target platform's architecture.

In both cases you're creating a new List<T>. The type of the actual object that lives on the heap does not depend on the type of the reference to which it is assigned. You can say object x = new List<T>() and it's still a fully functional instance of a List<T> created "somewhere", even though you're only holding a reference of type object.

It'd be interesting to hear how you got the idea that it could make a difference? Maybe you need to read up on the basics of C#/.NET, starting with reference and value types?

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/reference-types https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/value-types http://www.albahari.com/valuevsreftypes.aspx

V0ldek
  • 9,623
  • 1
  • 26
  • 57
  • It said that *The reference itself is basically a pointer. 32 bits on a 32 bit OS, 64 bits on a 64 bit OS. The size of the object that's referenced is more complicated.* I agree that the reference is the same but doing the change to IList, couldn't the size of the object be more lightweight? – Carlos Jan 08 '20 at 17:19
  • Thank you, it sounds reasonable, although, if you do object x = new List() you lose some features of List, couldn't this loss be associated to memory release? – Carlos Jan 08 '20 at 18:00
  • @CarlosPozos You can always recast it back to a `List` with `(List) x`. – V0ldek Jan 08 '20 at 18:22
  • @CarlosPozos Referring to a `List` as an object or as an interface has *no effect* on the runtime type, or the runtime type's memory usage. It's still a list. The only difference it would make memory-wise is the reference itself. –  Jan 08 '20 at 19:40