# ProfilerMarker.AutoScope

> Helper IDisposable struct for use with ProfilerMarker.Auto.

## Definition

* **Type:** Struct
* **Namespace:** [Unity.Profiling](/engine/6000.6/script-reference/unity/profiling.md)
* **Assembly:** UnityEngine.CoreModule
* **Implements:** [IDisposable](https://learn.microsoft.com/dotnet/api/system.idisposable)

```csharp
public struct ProfilerMarker.AutoScope : IDisposable
```

## Remarks

Use [ProfilerMarker.Auto](/engine/6000.6/script-reference/unity/profiling/profilermarker/auto.md) to enclose a piece of code you want to profile in *using* statement. Constructor of *AutoScope* calls [ProfilerMarker.Begin](/engine/6000.6/script-reference/unity/profiling/profilermarker/begin.md) and *Dispose* method - [ProfilerMarker.End](/engine/6000.6/script-reference/unity/profiling/profilermarker/end.md).

The *contextUnityObject* parameter associates the profiling sample with a specific Unity object, making it easier to identify which objects contribute to performance issues in the Profiler window.

The *metadata* parameter attaches additional contextual information to help distinguish different execution paths or parameter values without creating separate markers.

## Examples

```csharp
using Unity.Profiling;
using UnityEngine;

public class MySystemClass : MonoBehaviour
{
    ProfilerMarker simulatePerfMarker = new ProfilerMarker("MySystem.Simulate");
    ProfilerMarker processItemPerfMarker = new ProfilerMarker("MySystem.ProcessItem");

    public void UpdateLogic()
    {
        // Basic usage
        using (simulatePerfMarker.Auto())
        {
            // ...
        }

        // With object context
        using (simulatePerfMarker.Auto(this))
        {
            // ...
        }
    }

    public void ProcessItem(string itemType)
    {
        // With metadata
        using (processItemPerfMarker.Auto(itemType))
        {
            // ...
        }
    }
}
```
