Skip to content

Commit

Permalink
Pass type of default value (#2466)
Browse files Browse the repository at this point in the history
* Specify the type of the default value.

* Edit.
  • Loading branch information
davidbritch committed Aug 27, 2024
1 parent aca4d7b commit a507e66
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions docs/platform-integration/storage/preferences.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: "Preferences"
description: "Learn how to read and write application preferences, in .NET MAUI. The IPreferences interface can save and load application preferences in a key/value store."
ms.date: 09/02/2022
ms.date: 08/27/2024
no-loc: ["Microsoft.Maui", "Microsoft.Maui.Storage", "Preferences"]
---

Expand Down Expand Up @@ -40,10 +40,16 @@ Preferences are set by calling the `Preferences.Set` method, providing the key a

## Get preferences

To retrieve a value from preferences, you pass the key of the preference, followed by the default value when the key doesn't exist:
To retrieve a value from preferences you pass the key of the preference, followed by the default value when the key doesn't exist:

:::code language="csharp" source="../snippets/shared_1/Storage.cs" id="prefs_defaults":::

In some scenarios you may need to pass the key of the preference, followed by the default value and its type:

```csharp
long value = Preferences.Get("master_date", (long)0);
```

## Check for a key

It may be useful to check if a key exists in the preferences or not. Even though `Get` has you set a default value when the key doesn't exist, there may be cases where the key existed, but the value of the key matched the default value. So you can't rely on the default value as an indicator that the key doesn't exist. Use the `ContainsKey` method to determine if a key exists:
Expand Down

0 comments on commit a507e66

Please sign in to comment.