idisposable

Do I need to force a Dispose after a LINQ query?

谁说胖子不能爱 提交于 2021-02-18 04:46:51
问题 My DBA says that there are way too many connection open and he thinks it is my code in .net that is leaving them open. I am using LINQ querys and EF code first. Example Method: public List<Stuff> GetStuff() { var db = new DBContext(); var results = db.stuff.toList(); return results; } Do I need to dispose the db var once I am done? My understanding is that I didn't need to in EF and LINQ. Please point me to a Microsoft documentation about managing connection in code or best practices for LINQ

Disposing SqlCommand

笑着哭i 提交于 2021-02-08 02:17:28
问题 Because SqlCommand implements IDisposable , I would normally approach an ADO query as follows. using (SqlConnection connection = new SqlConnection(connectionString)) using (SqlCommand command = new SqlCommand(query, connection)) { // Execute command, etc. here } However, what if I need to execute multiple commands during a single connection? Do I really need a new using block for each command? The examples I found from Microsoft don't use a using block for SqlCommand s (or even call Dispose()

Does “using” also dispose objects created in the constructor? [duplicate]

℡╲_俬逩灬. 提交于 2021-01-29 05:23:59
问题 This question already has answers here : Nesting 'IDisposable's in a single 'using' statement (3 answers) Closed 4 years ago . When creating an element that implements IDisposable , Dispose() is called at the end of the using block also if an exception is thrown, if I'm correct. However, when creating a new element of ClassB within the constructor of a disposable element, will the object of ClassB also be disposed if IDisposable is implemented? using (ClassA a = new ClassA(new ClassB())) { }

Multiple using block, is this code safe?

99封情书 提交于 2021-01-29 03:11:36
问题 Code snippet is as follows public static string ToCompressedBase64(this string text) { using (var memoryStream = new MemoryStream()) { using (var gZipOutputStream = new GZipStream(memoryStream, CompressionMode.Compress)) { using (var streamWriter = new StreamWriter(gZipOutputStream)) { streamWriter.Write(text); } } return Convert.ToBase64String(memoryStream.ToArray()); } } As far as I know, if class contains field which is IDisposable then It should implement IDisposable itself and take care

Subscribe and immediately unsubscribe after first action

吃可爱长大的小学妹 提交于 2021-01-27 03:57:46
问题 I want to subscribe on an IObservable<T> and unsubscribe (dipose) the subscription right after receiving the first element of type T , i.e. I only want to call the action on the very first element I get after subscribing. This is the approach I came up with: public static class Extensions { public static void SubscribeOnce<T>(this IObservable<T> observable, Action<T> action) { IDisposable subscription = null; subscription = observable.Subscribe(t => { action(t); subscription.Dispose(); }); }

Dispose class vb.net

泄露秘密 提交于 2020-12-06 04:34:12
问题 I see alot of "Tutorial" on how to dispose a class but I can't understand plus all of them are explain in c# not in vb.net it's quite similar I know but it seems I can,t get it and I don't know why So my question : How I can implement a IDisposable in this class Public Class Container Private _sItemName As String Private _sPrice As Single Private _sPriceTot As Single Private _iNumber As Integer Sub New(ByVal _sItemName As String, ByVal _sPrice As Single, ByVal _sPriceTot As Single, ByVal

Dispose class vb.net

孤街醉人 提交于 2020-12-06 04:33:48
问题 I see alot of "Tutorial" on how to dispose a class but I can't understand plus all of them are explain in c# not in vb.net it's quite similar I know but it seems I can,t get it and I don't know why So my question : How I can implement a IDisposable in this class Public Class Container Private _sItemName As String Private _sPrice As Single Private _sPriceTot As Single Private _iNumber As Integer Sub New(ByVal _sItemName As String, ByVal _sPrice As Single, ByVal _sPriceTot As Single, ByVal

Dispose class vb.net

吃可爱长大的小学妹 提交于 2020-12-06 04:33:21
问题 I see alot of "Tutorial" on how to dispose a class but I can't understand plus all of them are explain in c# not in vb.net it's quite similar I know but it seems I can,t get it and I don't know why So my question : How I can implement a IDisposable in this class Public Class Container Private _sItemName As String Private _sPrice As Single Private _sPriceTot As Single Private _iNumber As Integer Sub New(ByVal _sItemName As String, ByVal _sPrice As Single, ByVal _sPriceTot As Single, ByVal

Is PdfStamper disposing output stream? (iTextSharp)

亡梦爱人 提交于 2020-08-09 02:37:27
问题 I am using iTextSharp to add page numbers to a PDF with C#. While running code analysis the MemoryStream for the output is suspected to be disposed more than once. See this warning generated by Visual Studio. Is this an API problem? Should the second parameter of PdfStamper be marked as out ? Is there a way for me to fix this warning? MemoryStream mem = null; PdfReader reader = null; PdfStamper stamper = null; try { mem = new MemoryStream(); reader = new PdfReader(m_pdf); stamper = new

Should Class Implement Both IAsyncDisposable and IDisposable?

妖精的绣舞 提交于 2020-08-08 06:38:07
问题 For example I have this class: public class UnitOfWork : IUnitOfWork { private readonly ApplicationDbContext _context; public IProductRepository Products { get; private set; } public ICategoryRepository Categories { get; private set; } public IPhotoRepository Photos { get; private set; } public UnitOfWork(ApplicationDbContext context, IProductRepository productRepository, ICategoryRepository categoryRepository, IPhotoRepository photoRepository) { _context = context; Products =