The type must be a non-nullable value - c#

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?

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...

Related

How can I initialize a System.Nullable<Int32>? [duplicate]

This question already has answers here:
GetType on Nullable Boolean
(2 answers)
Closed 1 year ago.
I need a Nullable type but how do I initialize such a variable? Whenever I try to do this it automatically converts to a normal Int32.
Nullable<Int32> nullableInt32 = new Nullable<Int32>(3);
Console.WriteLine(nullableInt32.GetType()); // gives me System.Int32
Is this a bug? How can I actually initalize the Nullable?
From Microsoft's documentation:
If you want to determine whether an instance is of a nullable value type, don't use the Object.GetType method to get a Type instance to be tested with the preceding code. When you call the Object.GetType method on an instance of a nullable value type, the instance is boxed to Object. As boxing of a non-null instance of a nullable value type is equivalent to boxing of a value of the underlying type, GetType returns a Type instance that represents the underlying type of a nullable value type.
So your int? gets boxed to int, and GetType() is called on the boxed instance.
Unless you know the type at compile time and use typeof, there is no way to get a type of a nullable object:
var type = typeof(int?);
In practice this shouldn't matter because if you don't know the type at compile time, it means you're using some sort of type erasure (i.e. a cast to object), and that means boxing, and nullable value types don't exist there. You can't use polymorphism because that doesn't work with value types.
If you think you have a valid need for this, feel free to explain your use case in the comments.

Nullable restrictions [duplicate]

This question already has answers here:
Why can't I write Nullable<Nullable<int>>?
(5 answers)
Closed 5 years ago.
Does anyone know why this code doesn't compile?
Nullable<Nullable<int>> n = null;
I realize Nullable has a constraint
where T : struct
But Nullable is struct. I also know this constraint has a restriction "The type argument must be a value type. Any value type except Nullable can be specified." (https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/constraints-on-type-parameters).
So how does it work? Is this solved on compiler level?
The error message is:
The type int? must be a non-nullable value type in order to use it
as parameter 'T' in the generic type or method Nullable<T>
So it must not only be a value type but a non-nullable value type. But Nullable<int> is a nullable value type.
Here's the compiler error CS0453 which also shows this example:
This error occurs when you use a non-value type argument in
instantiating a generic type or method which has the value constraint
on it. It can also occur when you use a nullable value type argument.
Q: Is this solved on compiler level?
Yes, which means it' not very interesting to know how they achieved this constraint. It's an implementation detail of the compiler which doesn't need to use a C# language feature.
Why is not allowed?
Well, what would be the benefit of a Nullable<Nulable<int>>? Nullables were introduced to give value types the opportunity to be null(so undefined, without value). This is already achieved for a Nullable<int>, it can be null. So by nesting it in another nullable you would not get anything. It's not allowed for the same reason why you can't have a Nullable<string>, a string as every other reference type can already be null.

The type 'T1' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable<T>'

Here iam getting error
The type 'string' must be a non-nullable value type in order to use it as parameter 'T1' in the generic type or method 'GetDataKeyValue(System.Web.UI.WebControls.GridView, int, string)'
String Process = GetDataKeyValue<String>(gvTargetRate, RowIndex, "Process");
Here iam getting error
The type 'T1' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable'
private Nullable<T> GetDataKeyValue<T>(GridView gv, int RowIndex, T column)
{
if (column == null)
return default(T);
return (T)gv.DataKeys[RowIndex].Values[column];
}
You can't create a Nullable<> out of any type. As the compiler tells you, you can only do that for value types. Since the method isn't restricted to any type, it can't say for sure you won't pass it, let's say, an object.
What you want isn't possible, actually, as explained in this question and its answer.
And by the way, T isn't a nullable type by definition, so default(T) will become a non-null value in the case of value types. string is on object, so it can be null without the use of Nullable<T>.
The Nullable type requires that T is a non-nullable value type, for example int or DateTime. Reference types like string can already be null. There would be no point in allowing things like Nullable so it is disallowed.
more info.

Why string or object type don't support nullable reference type? [duplicate]

This question already has answers here:
C# nullable string error
(5 answers)
Closed 8 years ago.
Look into following code block:
//Declaring nullable variables.
//Valid for int, char, long...
Nullable<int> _intVar;
Nullable<char> _charVar;
//trying to declare nullable string/object variables
//gives compile time error.
Nullable<string> _stringVar;
Nullable<object> _objVar;
While compiling code compiler gives following error message:
The type 'string'/'object' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable'
I read it several times but still unable to understand. Can anyone clarify this? Why object or string dont support nullable reference type?
object and string are reference types, so they're already nullable. For example, this is already valid:
string x = null;
The Nullable<T> generic type is only for cases where T is a non-nullable value type.
In the declaration for Nullable<T> there is a constraint on T:
public struct Nullable<T> where T : struct
That where T : struct is precisely the part that constrains T to be a non-nullable value type.
Nullable<T> is defined as:
public struct Nullable<T> where T : struct
meaning: it only works on value-type T (excluding Nullable<TSomethingElse> itself).
You cannot use Nullable<T> on reference-types (or on Nullable<T>), but you don't need to since all reference-types (including object and string) are already "nullable", in that you can assign null to them.
string and object are reference types, and therefore are "nullable" already. The Nullable<T> type exists as a wrapper around value types that don't support null out of the box.
string myString = null //fine
int myInt = null //compiler error

Are nullable value types just wrappers around the regular value type?

The reason I ask is that you can cast a nullable type to a regular type with the Value property. That makes me think that the regular type is just wrapped up in the nullable type.
Yes, it is a generic struct:
public struct Nullable<T> where T : struct, new()
This is probably more confusing if you've only seen the T? syntax - but that is just syntactic sugar, the compiler is changing it to Nullable<T>.
Source: http://msdn.microsoft.com/en-us/library/b3h38hb0.aspx , http://msdn.microsoft.com/en-us/library/1t3y8s4s.aspx
Yes - 'Nullable<T> Structure':
Represents an object whose underlying
type is a value type that can also be
assigned null like a reference type.
[BTW, if your curious you can use Reflector to 'look under the hood']
According to MSDN "Nullable types are instances of the System.Nullable<T> struct. A nullable type can represent the correct range of values for its underlying value type, plus an additional null value"

Categories