Passing arguments by reference with "out" keyword

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

Microsoft .NET allows you to create methods that expect arguments to be passed by reference.
Passing by reference is introduced by explicitly setting ref or out keywords before argument type within the method definition. For example, void MethodWithRefArg(ref int arg).

Note: Passing arguments by reference should not be confused with passing reference-type arguments described in the previous section. If you need just to pass another .NET object in method arguments, please see the previous section.

Javonet allows you to pass by reference both .NET objects (NObject) or primitive types (String, Integer, Float, Boolean etc..). You can also pass arrays of these types like NObject[] or String[].

While passing variables by reference, the value of that variable might be modified within the method body. Read more about .NET ref and out keywords here:

Javonet allows you to invoke methods expecting out arguments by providing dedicated type: NOut. To pass the variable by reference, you must wrap the variable with the NOut class. For NObject and NObject[], you can wrap them with NOut directly. However because Java does not support passing by reference primitive types, both variables (like String, Integer, Boolean etc.) or arrays of them should be wrapped with the "AtomicReference<?>" class, and then wrapped with NOut object.
Example .NET class with expecting out argument method:

namespace TestNamespace
	class Item
		public string ItemName { get; set; }
	class PopulateItems
		public void Populate(out Item[] items)
			items = new Item[5];
			for (int i = 0; i < 5; i++)
				items[i] = new Item();
				items[i].ItemName = "Item " + i.ToString();

Example of a out argument method from Java using Javonet

I code in:
// Todo: activate Javonet and add reference to .NET library

NObject populator = Javonet.New("TestNamespace.PopulateItems");

//Wrap null Java array in atomic reference to pass the reference as out argument
AtomicReference<NObject[]> items = new AtomicReference<NObject[]>(null);

populator.invoke("Populate", new NOut(items, "TestNamespace.Item[]"));

// write response to console
for (NObject element : items.get()) {
    System.out.println((String) element.get("ItemName"));
//expected output:
//Item 0
//Item 1
//Item 2
//Item 3
//Item 4

Example of a out argument method from .NET standard library which can be invoked using Javonet:

I code in:
// Todo: activate Javonet

String strNumber = "4";
AtomicReference<Integer> myInt = new AtomicReference<Integer>();

//NOut constructor with argument type is used because myInt has NULL value. Without specifying explicitly
//argument type .NET would not be able to locate proper method to execute.
if (Javonet.getType("Int32").invoke("TryParse", strNumber, new NOut(myInt, "System.Int32"))) {

See Live Example!