# GetComponentRefOrNullRef<T>()

> Returns a reference to the component of type T attached to this element, or a null reference when the component is not attached. This method never adds a component.

## Definition

* **Type:** Method
* **Namespace:** [UnityEngine.UIElements](/engine/6000.7/script-reference/unityengine/uielements.md)
* **Assembly:** UnityEngine.UIElementsModule

```csharp
public ref T GetComponentRefOrNullRef<T>() where T : struct, IVisualElementComponent
```

### Returns

| Type | Description                                                                                                                                                                               |
| ---- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| T    | A reference to the live component data, or a null reference when the component is not attached. A valid reference stays valid until a component is added to or removed from this element. |

### Remarks

Always check the returned reference with `System.Runtime.CompilerServices.Unsafe.IsNullRef` before using it. Reading or writing through a null reference throws NullReferenceException. When the component is attached, this method behaves exactly like [VisualElement.GetComponent](/engine/6000.7/script-reference/unityengine/uielements/visualelement/getcomponent.md).
