# AddKey

> Adds a key and returns its entry.

## Definition

* **Type:** Method
* **Namespace:** [Unity.Localization](/engine/6000.7/script-reference/unity/localization.md)
* **Assembly:** UnityEngine.LocalizationRuntimeModule

## AddKey(string)

Adds a key and returns its entry.

```csharp
public SharedTableData.SharedTableEntry AddKey(string key)
```

### Parameters

**** (\[string]\(https\://learn.microsoft.com/dotnet/api/system.string)): The key to add.

### Returns

| Type                                                                                                       | Description                                                 |
| ---------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------- |
| [SharedTableEntry](/engine/6000.7/script-reference/unity/localization/sharedtabledata/sharedtableentry.md) | The new or existing entry, or `null` when the key is empty. |

### Remarks

When the key already exists, its existing entry is returned rather than a duplicate being created. A new id is drawn from the [SharedTableData.KeyGenerator](/engine/6000.7/script-reference/unity/localization/sharedtabledata/keygenerator.md) and retried until it is unique. Returns `null` when the key is empty or null.

Additional Resources: [SharedTableData.AddKey](/engine/6000.7/script-reference/unity/localization/sharedtabledata/addkey.md), [SharedTableData.RemoveKey](/engine/6000.7/script-reference/unity/localization/sharedtabledata/removekey.md), [SharedTableData.RenameKey](/engine/6000.7/script-reference/unity/localization/sharedtabledata/renamekey.md)

Add a key and observe that adding it again returns the same entry.

### Examples

```csharp
using Unity.Localization;
using UnityEngine;

namespace Unity.Localization.Samples
{
    public class SharedTableDataAddKeyExample
    {
        public void Run()
        {
            var sharedData = ScriptableObject.CreateInstance<SharedTableData>();

            SharedTableData.SharedTableEntry entry = sharedData.AddKey("GREETING");
            SharedTableData.SharedTableEntry again = sharedData.AddKey("GREETING");   // same entry, no duplicate

            Debug.Log(entry.Id == again.Id);   // True
        }
    }
}
```

## AddKey(string, long)

Adds a key with an explicit id and returns its entry.

```csharp
public SharedTableData.SharedTableEntry AddKey(string key, long id)
```

### Parameters

**** (\[string]\(https\://learn.microsoft.com/dotnet/api/system.string)): The key to add.**** (\[long]\(https\://learn.microsoft.com/dotnet/api/system.int64)): The stable id to assign.

### Returns

| Type                                                                                                       | Description                                                             |
| ---------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------- |
| [SharedTableEntry](/engine/6000.7/script-reference/unity/localization/sharedtabledata/sharedtableentry.md) | The new or existing entry, or `null` when the key or id cannot be used. |

### Remarks

Use this to preserve ids when rebuilding from serialized data, for example importing or loading from a file. Returns the existing entry when the key is already present, logging a warning if the requested id differs from the stored one. Returns `null` when the key is empty, the id is `0`, or the id is already used by another key.

Additional Resources: [SharedTableData.AddKey](/engine/6000.7/script-reference/unity/localization/sharedtabledata/addkey.md), [SharedTableData.GetEntry](/engine/6000.7/script-reference/unity/localization/sharedtabledata/getentry.md)

Add a key with an id preserved from serialized data.

### Examples

```csharp
using Unity.Localization;
using UnityEngine;

namespace Unity.Localization.Samples
{
    public class SharedTableDataAddKeyWithIdExample
    {
        public void Run()
        {
            var sharedData = ScriptableObject.CreateInstance<SharedTableData>();

            SharedTableData.SharedTableEntry entry = sharedData.AddKey("GREETING", 1001);

            Debug.Log(entry.Id);   // 1001
        }
    }
}
```
