Using Value-Type and Reference-Type Arrays

You are browsing legacy Javonet 1.5 (Java<>.NET bridge for Windows) documentation. Use the left side menu or click here to switch to latest Javonet 2.0 documentation. Javonet 2.0 allows you to use any module from JVM, CLR, Netcore, Python, Ruby, Perl, NodeJS on Windows, Linux and MacOs from any application created in Java, Clojure, Groovy, Kotlin, C#, F#, J#, VB.NET, Python, Perl, Ruby, JavaScript, TypeScript, C++ and GoLang

With Javonet, you can use any value-type or reference-type arrays. Arrays can be retrieved from field or property values, returned as method invocation results, or passed as arguments or set to fields and properties. All array operations can be performed both on instance and static elements.

Using arrays is very simple. Value-typed arrays are translated into regular arrays of corresponding any language types. Reference-typed arrays are represented as array of NObject/JObject objects.

Assuming we have a custom .NET Framework DLL with the following class inside:

using System;

namespace TestNamespace
{
	public class MyItem
	{
		public MyItem(string name) { ItemName = name; }
		public string ItemName { get; set; }
	}
	public class ArrayTestClass
	{
		public string[] GetItems()
		{
			return new string[] { "item1", "item2", "item3" };
		}
		public MyItem[] GetRefItems()
		{
			return new MyItem[] { new MyItem("Item1"), new MyItem("Item2"), new MyItem("Item3") };
		}
		public void DisplayArray(string[] items)
		{
			Console.Out.WriteLine(".NET: Displaying value-typed array");
			foreach (var item in items)
				Console.Out.WriteLine(item);
		}
		public void DisplayArray(MyItem[] items)
		{
			Console.Out.WriteLine(".NET: Displaying ref-typed array");
			foreach (var item in items)
				Console.Out.WriteLine(item.ItemName);
		}
	}
}

To use method from this class in any language:

I code in:

Select language you code in:

Java

Starting with version 1.4hf34 you can also retrieve a mixed arrays of value and reference types. In such case you will received on Java side an Object[] which will consist of NObject items and Java value types like (float, double, int, etc..).

See Live Example!