Open & Close a Local Realm - .NET SDK

When opening a local (non-synced) realm, pass a RealmConfiguration object to either GetInstanceAsync() or GetInstance(). The following example creates a RealmConfiguration object with a local file path, sets the IsReadOnly property to true, and then opens a local realm with that configuration information:

var config = new RealmConfiguration(pathToDb + "/my.realm")
IsReadOnly = true,
var localRealm = Realm.GetInstance(config);

With an InMemoryConfiguration object, you can create a realm that runs entirely in memory (that is, without the data written to disk.) The following example shows how to do this:

var config = new InMemoryConfiguration("some-identifier");
var realm = Realm.GetInstance(config);

In-memory realms might still use disk space if memory is running low, but all files created by an in-memory realm will be deleted when the realm is closed. When creating an in-memory realm, the identifier must be unique to all realms, including both in-memory and persisted realms.


When an in-memory realm is disposed or garbage-collected, the data is lost. To keep an in-memory realm “alive” throughout your app’s execution, be sure to hold a reference to realm.

The realm instance implements IDisposable to ensure native resources are freed up. You should dispose of a realm object immediately after use, especially on background threads. The simplest way to do this is by declaring the realm object with a using statement, or wrapping the code that interacts with a realm in a using (...) statement:

config = new SyncConfiguration("myPart", user);
using (var realm = await Realm.GetInstanceAsync(config))
var allTasks = realm.All<Task>();

If you require a realm object to be shared outside of a single method, be sure to manage its state by calling the Dispose() method:


As a general rule, you should dispose of the realm only on background threads, because disposing of a realm invalidates all objects associated with that instance. If you are data binding the realm objects on the main thread, for example, you should not call Dispose().

By default, all RealmObject classes are stored in a realm. In some scenarios, you may want to limit the classes that get stored, which you can do with the ObjectClasses property of the RealmConfiguration object. The following code demonstrates how you specify two classes you want stored in the realm:

var config = new RealmConfiguration()
ObjectClasses = new Type[]
Give Feedback