---
title: "How to dispose the context variable in c#?"  
description: "How to dispose the context variable in c#?"  
author: "Anonymous User"  
published: 2014-01-22  
updated: 2014-01-23  
canonical: https://www.mindstick.com/forum/1858/how-to-dispose-the-context-variable-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 1 minute  

---

# How to dispose the context variable in c#?

I want to dispose the devDB [context](https://www.mindstick.com/forum/23245/what-is-context-in-android) object in the below code.

```
private static void InitContainer(){     var devDB = new TestContext(constr);                  Container                                    .RegisterInstance<TestContext>(devDB)                    .RegisterInstance<IRepository<User>>(new Repository<User>(devDB))}private static void CleanUp(){    if (Container != null)    {        Container.Dispose();    }}
```

I tried a using statement, but the I receive the following [error](https://yourviews.mindstick.com/view/88527/fixing-quickbooks-error-4120-reinstalling-vs-repairing):

The [ObjectContext](https://www.mindstick.com/interview/34244/explain-the-difference-between-dbcontext-and-objectcontext) [instance](https://www.mindstick.com/forum/155658/testing-connection-to-cloud-sql-instance-with-a-mysql-client-from-vm-instance) has been disposed and can no longer be used for [operations](https://www.mindstick.com/blog/304985/how-does-devops-bridge-the-gap-between-development-and-operations-teams-like-git) that require a connection.

How can I dispose devDB correctly?

## Replies

### Reply by Pravesh Singh

Hi Tanuj,

Use using statements:

```
using(var devDB = new TestContext(constr)){    // rest of your code}
```

It will automatically Dispose your variable if it's Disposable.


---

Original Source: https://www.mindstick.com/forum/1858/how-to-dispose-the-context-variable-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
