# HasKey(string)

> Returns true if key exists in the preferences file.

## Definition

* **Type:** Method
* **Namespace:** [UnityEditor](/engine/6000.5/script-reference/unityeditor.md)
* **Assembly:** UnityEditor.CoreModule

```csharp
public static bool HasKey(string key)
```

### Parameters

**** (\[string]\(https\://learn.microsoft.com/dotnet/api/system.string)): Name of key to check for.

### Returns

| Type                                                          | Description                      |
| ------------------------------------------------------------- | -------------------------------- |
| [bool](https://learn.microsoft.com/dotnet/api/system.boolean) | The existence or not of the key. |

### Remarks

The preferences file is examined to identify whether the specified key exists.  True or false is returned.  In the following example a key and value can be written into the preference file, or deleted.  The existence of the key is checked with the [EditorPrefs.HasKey](/engine/6000.5/script-reference/unityeditor/editorprefs/haskey.md) function and a message displayed.

![EditorPrefsHasKey (HasKey(string))](/api/media?file=/engine/6000.5/media/images/EditorPrefsHasKey.png)

*Use save, delete, and HasKey preference check.*

### Examples

```csharp
// Small example where the XyZ key can be saved or deleted from the Preferences file.
// The existence of the key is checked using the HasKey() function.

using UnityEngine;
using UnityEditor;

public class HasKeyExample : EditorWindow
{
    private string keyName = "XyZ";

    [MenuItem("Examples/HasKey Example")]
    static void Init()
    {
        HasKeyExample window = (HasKeyExample)EditorWindow.GetWindowWithRect(
            typeof(HasKeyExample), new Rect(0, 0, 250, 80));
        window.Show();
    }

    void OnGUI()
    {
        EditorGUILayout.BeginHorizontal();

        if (GUILayout.Button("Save '" + keyName + "' as Key"))
            EditorPrefs.SetString(keyName, "abc123");

        if (GUILayout.Button("Delete Key '" + keyName + "'"))
            EditorPrefs.DeleteKey(keyName);

        EditorGUILayout.EndHorizontal();

        GUILayout.Label(keyName + " key exists: " + EditorPrefs.HasKey(keyName));

        if (GUILayout.Button("Close"))
            this.Close();
    }

    // delete the key each time the demo starts
    void OnFocus()
    {
        EditorPrefs.DeleteKey(keyName);
    }
}
```
