Create a user-defined type (UDT) (C#)

Creates a user-defined type (UDT) to use in a table.

Ready to write code? See the examples for this method to get started. If you are new to the Data API, check out the quickstart.

Result

Creates a user-defined type with the specified definition.

Does not return anything.

Parameters

Use the CreateTypeAsync method, which belongs to the Database class. You can also use CreateType, which is the synchronous version of the method.

Method signature
public Task CreateTypeAsync<T>(CreateTypeOptions options = null);
public Task CreateTypeAsync<T>(
  string typeName, CreateTypeOptions options = null
);
public Task CreateTypeAsync(
  string typeName,
  UserDefinedTypeDefinition definition,
  CreateTypeOptions options = null
);
Name Type Summary

typeName

string

The name of the new user-defined type.

The name must be unique within the keyspace.

If you use a custom user-defined type class and do not specify this parameter, the client attempts to extract it from first from the [UserDefinedType("TYPE_NAME")] attribute, then from the name of the class. For more information and examples, see Custom typing for collections.

definition

UserDefinedTypeDefinition

The definition for the new user-defined type. The definition describes the name and data type of each field in the user-defined type. You can use any supported data type, except for maps, lists, sets, or other user-defined types.

See the examples for usage.

options

CreateTypeOptions

Optional. Options for this operation. For more information and examples for general options such as timeout, see Customize API interaction. For options specific to this method, see Method-specific properties of the CreateTypeOptions class.

Method-specific properties of the CreateTypeOptions class
Name Type Summary

IfNotExists

bool

Optional. Whether the command should silently succeed even if a user-defined type with the given name already exists in the keyspace and no new type was created.

This option only checks type names. It does not check type definitions.

Default: false

Examples

The following examples demonstrate how to create a user-defined type.

Create a user-defined type

  • Typed

  • Untyped

using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Tables;

namespace Examples;

// Define the user-defined type
[UserDefinedType("member")]
public class Member
{
  [ColumnName("name")]
  public string? Name { get; set; }

  [ColumnName("is_active")]
  public bool? IsActive { get; set; }

  [ColumnName("date_joined")]
  public DateOnly? DateJoined { get; set; }
};

public class Program
{
  static async Task Main()
  {
    // Get an existing table
    var client = new DataAPIClient();
    var database = client.GetDatabase(
      "API_ENDPOINT",
      "APPLICATION_TOKEN"
    );

    // Create a user-defined type
    await database.CreateTypeAsync<Member>();
  }
}
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Tables;
using DataStax.AstraDB.DataApi.Utils;

namespace Examples;

public class Program
{
  static async Task Main()
  {
    // Get an existing table
    var client = new DataAPIClient();
    var database = client.GetDatabase(
      "API_ENDPOINT",
      "APPLICATION_TOKEN"
    );

    // Create a user-defined type
    await database.CreateTypeAsync(
      "member",
      new UserDefinedTypeDefinition
      {
        Fields = new Dictionary<string, DataAPIType>
        {
          ["name"] = DataAPIType.Text(),
          ["is_active"] = DataAPIType.Boolean(),
          ["date_joined"] = DataAPIType.Date(),
        },
      }
    );
  }
}

Create a user-defined type only if the type does not exist

Use this option to silently do nothing if a user-defined type with the specified name already exists.

This option only checks type names. It doesn’t check the definition of any existing types.

  • Typed

  • Untyped

using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.Tables;

namespace Examples;

// Define the user-defined type
[UserDefinedType("member")]
public class Member
{
  [ColumnName("name")]
  public string? Name { get; set; }

  [ColumnName("is_active")]
  public bool? IsActive { get; set; }

  [ColumnName("date_joined")]
  public DateOnly? DateJoined { get; set; }
};

public class Program
{
  static async Task Main()
  {
    // Get an existing table
    var client = new DataAPIClient();
    var database = client.GetDatabase(
      "API_ENDPOINT",
      "APPLICATION_TOKEN"
    );

    // Create a user-defined type
    await database.CreateTypeAsync<Member>(
      new CreateTypeOptions() { IfNotExists = true }
    );
  }
}
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.Tables;
using DataStax.AstraDB.DataApi.Utils;

namespace Examples;

public class Program
{
  static async Task Main()
  {
    // Get an existing table
    var client = new DataAPIClient();
    var database = client.GetDatabase(
      "API_ENDPOINT",
      "APPLICATION_TOKEN"
    );

    // Create a user-defined type
    await database.CreateTypeAsync(
      "member",
      new UserDefinedTypeDefinition
      {
        Fields = new Dictionary<string, DataAPIType>
        {
          ["name"] = DataAPIType.Text(),
          ["is_active"] = DataAPIType.Boolean(),
          ["date_joined"] = DataAPIType.Date(),
        },
      },
      new CreateTypeOptions() { IfNotExists = true }
    );
  }
}

Client reference

For more information, see the client reference.

Was this helpful?

Give Feedback

How can we improve the documentation?

© Copyright IBM Corporation 2026 | Privacy policy | Terms of use Manage Privacy Choices

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries. Kubernetes is the registered trademark of the Linux Foundation.

General Inquiries: Contact IBM