.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®.
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";
Create a generic list. The
System.Collections.Generic.Listclass is in themscorlibassembly, which MATLAB loads automatically. TheListobjectlisthas aCapacityof 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: 0Use the
AddRangemethod to add the contents ofd1to the list:AddRange(list,d1);
Now,
listcontains three items. To display them:for i = 1:list.Count disp(list.Item(i-1)) end
Brachiosaurus Shunosaurus Allosaurus
Another way to add values is to use the
InsertRangemethod. Insert the contents ofd2starting at index1:InsertRange(list,1,d2);
The list now contains sevens. Display all values:
for i = 1:list.Count disp(list.Item(i-1)) end
Brachiosaurus Tyrannosaurus Spinosaurus Velociraptor Triceratops Shunosaurus Allosaurus
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
0means 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 Type | Name | Arguments |
|---|---|---|
System.String RetVal | Item | (System.Collections.Generic. |
| none | Item | (System.Collections.Generic. |