Main Content

.NET Generic Classes

Generics in .NET allow you to define classes and methods with type parameters (placeholders for one or more types). This lets you design classes that operate on a generic type, with the actual type specified at run time. A common use for generic classes is to work with collections. For information about generic methods, see Call .NET Generic Methods.

Create .NET Generic Classes in MATLAB

The NET.createGeneric function creates an instance of a specialized generic class. It requires:

  • The fully qualified name of the generic class definition

  • A list of fully qualified type names for specializing the generic type parameters

  • A variable list of constructor arguments

When a generic type parameter itself is another parameterized (generic) class, use instances of the NET.GenericClass helper class in the type list. This allows for multiple levels of generic type nesting.

To create a generic class with nested parameterization, construct NET.GenericClass instances using the fully qualified generic type name and a list of type parameters (which can themselves be NET.GenericClass objects).

Create .NET Collections

This example demonstrates how to create and manipulate a generic collection using System.Collections.Generic.List<System.String> in MATLAB®.

  1. Create string arrays:

    d1 = NET.createArray("System.String",3);
    d1(1) = "Brachiosaurus";
    d1(2) = "Shunosaurus";
    d1(3) = "Allosaurus";
    
    d2 = NET.createArray("System.String",4);
    d2(1) = "Tyrannosaurus";
    d2(2) = "Spinosaurus";
    d2(3) = "Velociraptor";
    d2(4) = "Triceratops";
    
  2. Create a generic list. The System.Collections.Generic.List class is in the mscorlib assembly, which MATLAB loads automatically. The List object list has a Capacity of three, but currently is empty (Count = 0).

    list = NET.createGeneric("System.Collections.Generic.List",{"System.String"},3)
    
    list = 
    
      List<System*String> with properties:
    
        Capacity: 3
           Count: 0
  3. Use the AddRange method to add the contents of d1 to the list:

    AddRange(list,d1);
    
  4. Now, list contains three items. To display them:

    for i = 1:list.Count
      disp(list.Item(i-1))
    end
    Brachiosaurus
    Shunosaurus
    Allosaurus
    
  5. Another way to add values is to use the InsertRange method. Insert the contents of d2 starting at index 1:

    InsertRange(list,1,d2);
    
  6. The list now contains sevens. Display all values:

  7. for i = 1:list.Count
      disp(list.Item(i-1))
    end
    
    Brachiosaurus
    Tyrannosaurus
    Spinosaurus
    Velociraptor
    Triceratops
    Shunosaurus
    Allosaurus

  8. To verify that ('Tyrannosaurus') is at index 1, choose one of these statements:

    System.String.Compare(d2(1),list.Item(1))
    System.String.Equals(d2(1),list.Item(1))
    d2(1) == list.Item(1)

    A result of 0 means the values are equal.

Convert .NET Collections to MATLAB Arrays

Use the ToArray method of the System.Collections.Generic.List class to convert a collection to a .NET array. For example:

dog = NET.createArray("System.String",3);
list = NET.createGeneric("System.Collections.Generic.List",{"System.String"},3);
Add(list,"poodle");
Add(list,"spaniel");
Add(list,"Irish setter");
arr = list.ToArray;
dogs = string(arr)
dogs = 
    "poodle"    "spaniel"    "Irish setter"

You can now use dogs in MATLAB functions:

sort(dogs)'
ans = 
    "Irish setter"
    "poodle"
    "spaniel"

Access Items in .NET Collections

Use the Item property of the List class to get or set an element at a specified index. In MATLAB, since Item is an indexed property, it maps to getter and setter methods.

The syntax to get a value for a list object at position index:

value = list.Item(index)

The syntax to set a newValue value in a list object at position index:

list.Item(index,newValue)

Method signatures:

Return TypeNameArguments
System.String RetValItem(System.Collections.Generic.
List<System*String> this,
int32 scalar index)
noneItem(System.Collections.Generic.
List<System*String> this,
int32 scalar index,
System.String value)

See Also

Functions

Classes

Topics