> For the complete documentation index, see [llms.txt](https://docs.wisej.com/ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.wisej.com/ai/components/api/smartsession/wisej.ai.smartsession.messagecollection.md).

# SmartSession.MessageCollection

Namespace: **Wisej.AI**

Assembly: **Wisej.AI** (3.5.0.0)

Represents a collection of messages in a session.

{% tabs %}
{% tab title="C#" %}

```csharp
public class MessageCollection : ObservableCollection<>, ICloneable
```

{% endtab %}

{% tab title="VB.NET" %}

```visual-basic
Public Class MessageCollection
    Inherits ObservableCollection(Of )
    Implements ICloneable
```

{% endtab %}
{% endtabs %}

## Constructors

### ![](/files/ptrKjmmRoQB76pvrIqh0) MessageCollection()

Initializes a new instance of [MessageCollection](/ai/components/api/smartsession/wisej.ai.smartsession.messagecollection.md).

## Methods

### ![](/files/ptrKjmmRoQB76pvrIqh0) Add(role, content)

Adds a message with the specified role and content.

| Parameter   | Type                                                                                | Description                 |
| ----------- | ----------------------------------------------------------------------------------- | --------------------------- |
| **role**    | [MessageRole](/ai/components/api/smartsession/wisej.ai.smartsession.messagerole.md) | The role of the message.    |
| **content** | [String](https://docs.microsoft.com/dotnet/api/system.string)                       | The content of the message. |

**Returns:** [MessageCollection](/ai/components/api/smartsession/wisej.ai.smartsession.messagecollection.md). The current [MessageCollection](/ai/components/api/smartsession/wisej.ai.smartsession.messagecollection.md) instance.

**Throws:**

* [ArgumentNullException](https://docs.microsoft.com/dotnet/api/system.argumentnullexception)\
  Thrown when the content is null.

### ![](/files/ptrKjmmRoQB76pvrIqh0) AddRange(messages)

Adds a range of messages to the collection.

| Parameter    | Type                                                                                        | Description          |
| ------------ | ------------------------------------------------------------------------------------------- | -------------------- |
| **messages** | [IList\<Message>](https://docs.microsoft.com/dotnet/api/system.collections.generic.ilist-1) | The messages to add. |

**Returns:** [MessageCollection](/ai/components/api/smartsession/wisej.ai.smartsession.messagecollection.md). The current [MessageCollection](/ai/components/api/smartsession/wisej.ai.smartsession.messagecollection.md) instance.

### ![](/files/ptrKjmmRoQB76pvrIqh0) Clone()

Creates a shallow clone of the collection.

**Returns:** [MessageCollection](/ai/components/api/smartsession/wisej.ai.smartsession.messagecollection.md). A shallow clone of the [MessageCollection](/ai/components/api/smartsession/wisej.ai.smartsession.messagecollection.md).

### ![](/files/KL0Ik37djZRr8a07Wopd) InsertItem(index, item)

Inserts a message at the specified index.

| Parameter | Type                                                                        | Description                                                   |
| --------- | --------------------------------------------------------------------------- | ------------------------------------------------------------- |
| **index** | [Int32](https://docs.microsoft.com/dotnet/api/system.int32)                 | The zero-based index at which the message should be inserted. |
| **item**  | [Message](/ai/components/api/smartsession/wisej.ai.smartsession.message.md) | The message to insert.                                        |

### ![](/files/KL0Ik37djZRr8a07Wopd) MoveItem(oldIndex, newIndex)

Moves a message from one index to another.

| Parameter    | Type                                                        | Description                                                |
| ------------ | ----------------------------------------------------------- | ---------------------------------------------------------- |
| **oldIndex** | [Int32](https://docs.microsoft.com/dotnet/api/system.int32) | The zero-based index specifying the location to move from. |
| **newIndex** | [Int32](https://docs.microsoft.com/dotnet/api/system.int32) | The zero-based index specifying the location to move to.   |

### ![](/files/ptrKjmmRoQB76pvrIqh0) RemoveAll(predicate)

Removes all messages that match the specified predicate.

| Parameter     | Type                                                                            | Description                      |
| ------------- | ------------------------------------------------------------------------------- | -------------------------------- |
| **predicate** | [Predicate\<Message>](https://docs.microsoft.com/dotnet/api/system.predicate-1) | The predicate to match messages. |

**Returns:** [MessageCollection](/ai/components/api/smartsession/wisej.ai.smartsession.messagecollection.md). The current [MessageCollection](/ai/components/api/smartsession/wisej.ai.smartsession.messagecollection.md) instance.

### ![](/files/ptrKjmmRoQB76pvrIqh0) RemoveLast(count)

Removes the last message(s) from the collection.

| Parameter                                  | Type                                                        | Description                                     |
| ------------------------------------------ | ----------------------------------------------------------- | ----------------------------------------------- |
| **count** ![](/files/52BqKf9SltWQ97FbQSBF) | [Int32](https://docs.microsoft.com/dotnet/api/system.int32) | The number of messages to remove. Default is 1. |

**Returns:** [MessageCollection](/ai/components/api/smartsession/wisej.ai.smartsession.messagecollection.md). The current [MessageCollection](/ai/components/api/smartsession/wisej.ai.smartsession.messagecollection.md) instance.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.wisej.com/ai/components/api/smartsession/wisej.ai.smartsession.messagecollection.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
