party34
party34

Reputation: 99

The type must be a non-nullable value

I'm trying to create a NativeArray of type Dictionary and i get this error:

The type Realtime.Messaging.Internal.ConcurrentDictionary<string,Chunk>' must be a non-nullable value type in order to use it as type parameterT' in the generic type or method `Unity.Collections.NativeArray'

NativeArray<ConcurrentDictionary<string, Chunk>> dictionary = new NativeArray<ConcurrentDictionary<string, Chunk>>(8, Allocator.TempJob);

I'm new to Unity and C#, this question was probably asked before but i've been searching for a fix and couldn't find anything.

How can I fix this?

Upvotes: 1

Views: 6911

Answers (1)

taquion
taquion

Reputation: 2767

The answer is in the error message you get: .. must be a non-nullable value type... ConcurrentDictionary is a reference type and it seems that NativeArray has a type parameter constraint to accept only structs, the kind of constraint as the following:

class Foo<T> where T:struct{}

This means that you can only create NativeArray of value types (structs): int, byte, char, ...etc, or your own structs...

Upvotes: 4

Related Questions