Dispose initialized entity collection?

Posts   
 
    
morten71
User
Posts: 80
Joined: 13-Jan-2009
# Posted on: 30-Jul-2019 12:14:07   

I've upgraded to VS2019 16.2 and begin to see messages like these:

IDE0068 C# Use recommended dispose pattern to ensure that object created by is disposed on all paths: using statement/declaration or try/finally

The referred code look like this: var tagsCollection = new TagCollection(); foreach (var tag in tags) { tagsCollection.Add(new TagEntity { FileId = fileId, Tag = tag }); } tagsCollection.SaveMulti();

if I add tagsCollection.Displose(); the recommendation disappears.

Should I always call Dispose in code like the above?

Thanks in advance.

Best regards Morten

Walaa avatar
Walaa
Support Team
Posts: 14946
Joined: 21-Aug-2005
# Posted on: 31-Jul-2019 05:00:38   

Generally it's not needed. Unless you keep a reference to one or more of the entities inside the collection. This will keep the Collection in memory. In that case Dispose makes sure, the entity is terminated.

morten71
User
Posts: 80
Joined: 13-Jan-2009
# Posted on: 31-Jul-2019 12:13:28   

Thanks for you reply. I'll leave the code as it is

Walaa wrote:

Generally it's not needed. Unless you keep a reference to one or more of the entities inside the collection. This will keep the Collection in memory. In that case Dispose makes sure, the entity is terminated.