Xamarin.Essentials Preferences

Pre-release NuGet

The Preferences class helps to store application preferences in a key/value store.

Using Secure Storage

Add a reference to Xamarin.Essentials in your class:

using Xamarin.Essentials;

To save a value for a given key in preferences:

Preferences.Set("my_key", "my_value");

To retrieve a value from preferences or a default if not set:

var myValue = Preferences.Get("my_key", "default_value");

To remove the key from preferences:

Preferences.Remove("my_key");

To remove all preferences:

Preferences.Clear();

In addition to these methods each take in an optional sharedName that can be used to create additional containers for preference. Read the platform implementation specifics below.

Supported Data Types

The following data types are supported in Preferences:

  • bool
  • double
  • int
  • float
  • long
  • string

Platform Implementation Specifics

All data is stored into Shared Preferences. If no sharedName is specified the default shared preferences are used, else the name is used to get a private shared preferences with the specified name.

Limitations

When storing a string, this API is intended to store small amounts of text. Performance may be subpar if you try to use it to store large amounts of text.

API