# How to compile a native array for c# target

**URL:** https://community.haxe.org/t/how-to-compile-a-native-array-for-c-target/4092
**Category:** Haxe
**Tags:** haxe-cs
**Created:** [November 26, 2023, 10:24pm UTC](https://community.haxe.org/t/how-to-compile-a-native-array-for-c-target/4092 "2023-11-26T22:24:39Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![witcher](https://community.haxe.org/user_avatar/community.haxe.org/witcher/32/2252_2.png) [@witcher](https://community.haxe.org/u/witcher)
#### Post date: [November 26, 2023, 10:24pm UTC](https://community.haxe.org/t/how-to-compile-a-native-array-for-c-target/4092/1 "2023-11-26T22:24:39Z")

</div>

Hello community. I am stuck trying to build a native Array type for c# target. The doc suggests using **cs.NativeArray** instead of Array I didn’t find any other solution. But I want to keep single code base, so for now my code doesn’t match Haxe concept. If there is another solution? macros for example? I tried adding nativeGen etc - it doesn’t work. NativeArray is also not compatible with Lambda, which is not very convenient.

```haxe
@:nativeGen
@:strict(SerializableAttribute)
class ProfileData {
#if cs
public var Cards:NativeArray<CardData>;
public var Items:NativeArray<ItemData>;
public var Events:NativeArray<GameRequest>;
....
#elseif js
public var Cards:Array<CardData>;
public var Items:Array<ItemData>;
public var Events:Array<GameRequest>;
...
#end
}

```

---

<div class="post-metadata">

### Author: ![haxecoder](https://community.haxe.org/user_avatar/community.haxe.org/haxecoder/32/2239_2.png) [@haxecoder](https://community.haxe.org/u/haxecoder)
#### Post date: [November 27, 2023, 8:48am UTC](https://community.haxe.org/t/how-to-compile-a-native-array-for-c-target/4092/2 "2023-11-27T08:48:51Z")

</div>

So the problem is the conditional compilation flag and you are trying to avoid that?

---

<div class="post-metadata">

### Author: ![ibilon](https://community.haxe.org/user_avatar/community.haxe.org/ibilon/32/13_2.png) [@ibilon](https://community.haxe.org/u/ibilon)
#### Post date: [November 27, 2023, 9:18am UTC](https://community.haxe.org/t/how-to-compile-a-native-array-for-c-target/4092/3 "2023-11-27T09:18:23Z")

</div>

You can use a typedef:

```haxe
#if cs
import cs.NativeArray;
#else
typedef NativeArray = Array;
#end

```

and then you can use the name `NativeArray` in all targets.

What’s your use case? A native array will not behave exactly the same as a Haxe array.

---

<div class="post-metadata">

### Author: ![witcher](https://community.haxe.org/user_avatar/community.haxe.org/witcher/32/2252_2.png) [@witcher](https://community.haxe.org/u/witcher)
#### Post date: [November 27, 2023, 10:32am UTC](https://community.haxe.org/t/how-to-compile-a-native-array-for-c-target/4092/4 "2023-11-27T10:32:57Z")

</div>

Correct. Looks like I should cast it into one type.

---

<div class="post-metadata">

### Author: ![witcher](https://community.haxe.org/user_avatar/community.haxe.org/witcher/32/2252_2.png) [@witcher](https://community.haxe.org/u/witcher)
#### Post date: [November 27, 2023, 10:54am UTC](https://community.haxe.org/t/how-to-compile-a-native-array-for-c-target/4092/5 "2023-11-27T10:54:04Z")

</div>

Great. However, I still can’t use Lambda because NA is not Iterable. I don’t know how to get iterator from NA. But that’s no longer an issue, I just can use ‘for’ instead. My case is shared logic between two platforms, iterating array, adding elements. So it might be better to use List\_1 instead of NativeArray

> #if cs  
> import cs.system.collections.generic.List\_1;  
> #else  
> typedef List\_1 = Array;  
> #end

---

<div class="post-metadata">

### Author: ![ibilon](https://community.haxe.org/user_avatar/community.haxe.org/ibilon/32/13_2.png) [@ibilon](https://community.haxe.org/u/ibilon)
#### Post date: [November 27, 2023, 11:01am UTC](https://community.haxe.org/t/how-to-compile-a-native-array-for-c-target/4092/6 "2023-11-27T11:01:46Z")

</div>

But Array is available on the c# target, why do you specifically want a native array?  
Using the same Array type will allow you to share logic more easily, since they’d have the same behavior.

---

<div class="post-metadata">

### Author: ![witcher](https://community.haxe.org/user_avatar/community.haxe.org/witcher/32/2252_2.png) [@witcher](https://community.haxe.org/u/witcher)
#### Post date: [November 27, 2023, 11:08am UTC](https://community.haxe.org/t/how-to-compile-a-native-array-for-c-target/4092/7 "2023-11-27T11:08:26Z")

</div>

Most part of the code is already done on the native target, I wouldn’t change it. And I would like to use native data types to avoid any platform (Unity) compatibility problems now and in the future.

---

<div class="post-metadata">

### Author: ![aartzrc](https://community.haxe.org/user_avatar/community.haxe.org/aartzrc/32/1407_2.png) [@aartzrc](https://community.haxe.org/u/aartzrc)
#### Post date: [November 28, 2023, 1:33am UTC](https://community.haxe.org/t/how-to-compile-a-native-array-for-c-target/4092/8 "2023-11-28T01:33:11Z")

</div>

Here’s some code we use to iterate on native CS arrays inside Haxe, hopefully they can get you a step further. They’re extension methods so do `using CSTools`.

```haxe
#if cs
import cs.system.collections.generic.List_1;

class CSTools {

    public static function iterator<T>(hashSet:cs.system.collections.generic.IEnumerable_1<T>):Iterator<T> {
        return new IEnumerableIterator(hashSet);
    }

    public static function toNative<T>(array:Array<T>):cs.NativeArray<T> {
        var nativeArray = new cs.NativeArray<T>(array.length);
        for(i in 0 ... array.length) nativeArray[i]=array[i];
        return nativeArray;
    }

    public inline static function toNativeList<T>(array:Array<T>):List_1<T> {
        var nativeList = new List_1<T>(array.length);
        for(i in array) nativeList.Add(i);
        return nativeList;
    }
}

class IEnumerableIterator<T> {
    var enumerator:cs.system.collections.generic.IEnumerator_1<T>;
    var hasNextBool = false;
    var nextCalled = false;
    public function new(enumerable:cs.system.collections.generic.IEnumerable_1<T>) {
        enumerator = enumerable.GetEnumerator();
    }

    public function hasNext():Bool {
        if(!nextCalled) {
            hasNextBool = enumerator.MoveNext();
            nextCalled = true;
        }
        return hasNextBool;
    }

    public function next():T {
        hasNext();
        nextCalled = false;
        return enumerator.Current;
    }
}

#end

```

Usage looks like this:

```haxe
var intArray = [1, 2, 3, 4]; // Haxe Array
var csArray = intArray.toNative(); // Converted to C# Array
for(i in csArray) trace(i); // Iterate over C# Array

```

---

<div class="post-metadata">

### Author: ![witcher](https://community.haxe.org/user_avatar/community.haxe.org/witcher/32/2252_2.png) [@witcher](https://community.haxe.org/u/witcher)
#### Post date: [November 28, 2023, 12:45pm UTC](https://community.haxe.org/t/how-to-compile-a-native-array-for-c-target/4092/9 "2023-11-28T12:45:35Z")

</div>

Thank for iterator. I supplemented your code collection:

```haxe
#if js
typedef NativeArray<T> = Array<T>;
typedef List_1<T> = Array<T>;
typedef Dictionary_2<T, K> = Map<T, K>;
#end

class CSTools {
	public static function iterator<T>(hashSet:cs.system.collections.generic.IEnumerable_1<T>):Iterator<T> {
		return new IEnumerableIterator(hashSet);
	}

	@:generic public static function push<T>(_this:cs.system.collections.generic.List_1<T>, x:T):Int {
		_this.Add(x);
		return _this.Count;
	}

	public static function pop<T>(_this:cs.system.collections.generic.List_1<T>):T {
		var i:T = _this[0];
		_this.RemoveAt(0);
		return i;
	}

	public static function sort<T>(_this:cs.system.collections.generic.List_1<T>, f:T->T->Int):Void {
		_this.Sort(f);
	}

	@:generic public static function get<T, K>(_this:cs.system.collections.generic.Dictionary_2<K, T>, key:K):T {
		return _this.get_Item(key);
	}

	@:generic public static function set<T, K>(_this:cs.system.collections.generic.Dictionary_2<K, T>, key:K, value:T):Void {
		return _this.set_Item(key, value);
	}

	@:generic public static function remove<T, K>(_this:cs.system.collections.generic.Dictionary_2<K, T>, key:K):Bool {
		return _this.Remove(key);
	}
}

```

so now we can use these in haxe code like

```haxe
 var deck:List_1<Int> = profile.Deck;
 var next:Int = request.Value;
 deck.push(next);

var events:Dictionary_2<String, GameRequest> = profile.Events;
events.get(request.Hash);

```

Using your toNative is bad practice,you’re creating a new copy array instead of make an interface with the current data. so, for example, on a server platform, with a large initial array, this isn’t applicable.

---

<div class="post-metadata">

### Author: ![ibilon](https://community.haxe.org/user_avatar/community.haxe.org/ibilon/32/13_2.png) [@ibilon](https://community.haxe.org/u/ibilon)
#### Post date: [November 28, 2023, 12:56pm UTC](https://community.haxe.org/t/how-to-compile-a-native-array-for-c-target/4092/10 "2023-11-28T12:56:32Z")

</div>

An alternative to a set of extension methods is making an abstract [Abstract - Haxe - The Cross-platform Toolkit](https://haxe.org/manual/types-abstract.html), that way you don’t have to add an import every time.
