# GrainExtensions Methods

[Type overview](/orleans/docs/api/csharp/microsoft.orleans.core.abstractions/orleans.grainextensions/)

## AsReference(IAddressable) {#asreference-1-this-orleans-runtime-iaddressable-2abb36bf}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.core.abstractions/orleans.grainextensions/methods/asreference-1-this-orleans-runtime-iaddressable-2abb36bf/)

[Source](https://github.com/dotnet/orleans/blob/e4e752cedecc2976811db49802c7a999b1f1c704/src/Orleans.Core.Abstractions/Core/GrainExtensions.cs)

```csharp
public static TGrainInterface AsReference<TGrainInterface>(this IAddressable grain)
```

Returns a typed reference to the provided grain.

### Parameters

- `grain` (`IAddressable`): The grain to convert.

### Returns

A strongly typed reference to the provided grain which implements `TGrainInterface`.

## AsReference(IAddressable, Type) {#asreference-this-orleans-runtime-iaddressable-system-type-63cfbb41}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.core.abstractions/orleans.grainextensions/methods/asreference-this-orleans-runtime-iaddressable-system-type-63cfbb41/)

[Source](https://github.com/dotnet/orleans/blob/e4e752cedecc2976811db49802c7a999b1f1c704/src/Orleans.Core.Abstractions/Core/GrainExtensions.cs#L90)

```csharp
public static object AsReference(this IAddressable grain, Type interfaceType)
```

Returns a typed reference to the provided grain.

### Parameters

- `grain` (`IAddressable`): The grain to convert.
- `interfaceType` (`Type`): The type of the grain interface.

### Returns

A strongly typed reference to the provided grain which implements `interfaceType`.

## Cast(IAddressable) {#cast-1-this-orleans-runtime-iaddressable-2a4597cb}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.core.abstractions/orleans.grainextensions/methods/cast-1-this-orleans-runtime-iaddressable-2a4597cb/)

[Source](https://github.com/dotnet/orleans/blob/e4e752cedecc2976811db49802c7a999b1f1c704/src/Orleans.Core.Abstractions/Core/GrainExtensions.cs#L78)

```csharp
public static TGrainInterface Cast<TGrainInterface>(this IAddressable grain)
```

Returns a typed reference to the provided grain.

### Parameters

- `grain` (`IAddressable`): The grain to convert.

### Returns

A strongly typed reference to the provided grain which implements `TGrainInterface`.

## Cast(IAddressable, Type) {#cast-this-orleans-runtime-iaddressable-system-type-36ff1bc3}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.core.abstractions/orleans.grainextensions/methods/cast-this-orleans-runtime-iaddressable-system-type-36ff1bc3/)

[Source](https://github.com/dotnet/orleans/blob/e4e752cedecc2976811db49802c7a999b1f1c704/src/Orleans.Core.Abstractions/Core/GrainExtensions.cs#L103)

```csharp
public static object Cast(this IAddressable grain, Type interfaceType)
```

Returns a typed reference to the provided grain.

### Parameters

- `grain` (`IAddressable`): The grain to convert.
- `interfaceType` (`Type`): The type of the grain interface.

### Returns

A strongly typed reference to the provided grain which implements `interfaceType`.

## GetGrainId(IAddressable) {#getgrainid-this-orleans-runtime-iaddressable-50186970}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.core.abstractions/orleans.grainextensions/methods/getgrainid-this-orleans-runtime-iaddressable-50186970/)

[Source](https://github.com/dotnet/orleans/blob/e4e752cedecc2976811db49802c7a999b1f1c704/src/Orleans.Core.Abstractions/Core/GrainExtensions.cs#L113-L127)

```csharp
public static GrainId GetGrainId(this IAddressable grain)
```

Returns the grain id corresponding to the provided grain.

### Parameters

- `grain` (`IAddressable`): The grain

### Returns

The grain id corresponding to the provided grain.

### Exceptions

- `System.ArgumentException`: The provided value has the wrong type or has no id.

## GetPrimaryKey(IAddressable) {#getprimarykey-this-orleans-runtime-iaddressable-1d7c2ee2}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.core.abstractions/orleans.grainextensions/methods/getprimarykey-this-orleans-runtime-iaddressable-1d7c2ee2/)

[Source](https://github.com/dotnet/orleans/blob/e4e752cedecc2976811db49802c7a999b1f1c704/src/Orleans.Core.Abstractions/Core/GrainExtensions.cs#L244-L257)

```csharp
public static Guid GetPrimaryKey(this IAddressable grain)
```

Returns the `System.Guid` representation of a grain primary key.

### Parameters

- `grain` (`IAddressable`): The grain to find the primary key for.

### Returns

A `System.Guid` representing the primary key for this grain.

### Exceptions

- `System.InvalidOperationException`: The provided grain does not have a `System.Guid`-based key.

## GetPrimaryKey(IAddressable, string?) {#getprimarykey-this-orleans-runtime-iaddressable-out-string-nullable-821f81ba}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.core.abstractions/orleans.grainextensions/methods/getprimarykey-this-orleans-runtime-iaddressable-out-string-nullable-821f81ba/)

[Source](https://github.com/dotnet/orleans/blob/e4e752cedecc2976811db49802c7a999b1f1c704/src/Orleans.Core.Abstractions/Core/GrainExtensions.cs#L216-L233)

```csharp
public static Guid GetPrimaryKey(this IAddressable grain, out string? keyExt)
```

Returns the `System.Guid` representation of a grain primary key.

### Parameters

- `grain` (`IAddressable`): The grain to find the primary key for.
- `keyExt` (`string?`): The output parameter to return the extended key part of the grain primary key, if extended primary key was provided for that grain.

### Returns

A `System.Guid` representing the primary key for this grain.

### Exceptions

- `System.InvalidOperationException`: The provided grain does not have a `System.Guid`-based key.

## GetPrimaryKeyLong(IAddressable) {#getprimarykeylong-this-orleans-runtime-iaddressable-ac3a602d}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.core.abstractions/orleans.grainextensions/methods/getprimarykeylong-this-orleans-runtime-iaddressable-ac3a602d/)

[Source](https://github.com/dotnet/orleans/blob/e4e752cedecc2976811db49802c7a999b1f1c704/src/Orleans.Core.Abstractions/Core/GrainExtensions.cs#L193-L204)

```csharp
public static long GetPrimaryKeyLong(this IAddressable grain)
```

Returns the `long` representation of a grain primary key.

### Parameters

- `grain` (`IAddressable`): The grain to find the primary key for.

### Returns

A `long` representing the primary key for this grain.

### Exceptions

- `System.InvalidOperationException`: The provided grain does not have a `System.Int64`-based key.

## GetPrimaryKeyLong(IAddressable, string?) {#getprimarykeylong-this-orleans-runtime-iaddressable-out-string-nullable-3599616d}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.core.abstractions/orleans.grainextensions/methods/getprimarykeylong-this-orleans-runtime-iaddressable-out-string-nullable-3599616d/)

[Source](https://github.com/dotnet/orleans/blob/e4e752cedecc2976811db49802c7a999b1f1c704/src/Orleans.Core.Abstractions/Core/GrainExtensions.cs#L171-L182)

```csharp
public static long GetPrimaryKeyLong(this IAddressable grain, out string? keyExt)
```

Returns the `long` representation of a grain primary key.

### Parameters

- `grain` (`IAddressable`): The grain to find the primary key for.
- `keyExt` (`string?`): The output parameter to return the extended key part of the grain primary key, if extended primary key was provided for that grain.

### Returns

A `long` representing the primary key for this grain.

### Exceptions

- `System.InvalidOperationException`: The provided grain does not have a `System.Int64`-based key.

## GetPrimaryKeyString(IAddressable) {#getprimarykeystring-this-orleans-runtime-iaddressable-41a1138d}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.core.abstractions/orleans.grainextensions/methods/getprimarykeystring-this-orleans-runtime-iaddressable-41a1138d/)

[Source](https://github.com/dotnet/orleans/blob/e4e752cedecc2976811db49802c7a999b1f1c704/src/Orleans.Core.Abstractions/Core/GrainExtensions.cs#L267-L273)

```csharp
public static string GetPrimaryKeyString(this IAddressable grain)
```

Returns the `string` primary key of the grain.

### Parameters

- `grain` (`IAddressable`): The grain to find the primary key for.

### Returns

A `string` representing the primary key for this grain.

## IsPrimaryKeyBasedOnLong(IAddressable) {#isprimarykeybasedonlong-this-orleans-runtime-iaddressable-92fbb78b}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.core.abstractions/orleans.grainextensions/methods/isprimarykeybasedonlong-this-orleans-runtime-iaddressable-92fbb78b/)

[Source](https://github.com/dotnet/orleans/blob/e4e752cedecc2976811db49802c7a999b1f1c704/src/Orleans.Core.Abstractions/Core/GrainExtensions.cs#L148-L159)

```csharp
public static bool IsPrimaryKeyBasedOnLong(this IAddressable grain)
```

Returns whether part of the primary key is of type `long`.

### Parameters

- `grain` (`IAddressable`): The target grain.

### Exceptions

- `System.InvalidOperationException`: The provided grain does not have a `System.Int64`-based key.
