consider this cod here:
public ref struct RefValue <T> where T : unmanaged
{
public ref readonly T Item1;
public RefTuple(ref T item1)
{
Item1 = ref item1;
}
}
//This function gets a 'ref' directly to the dictionary value
//and 'RefValue<T>' is a ref-struct which has a 'ref readonly field T'
//to store args passed as 'ref param'
public RefValue<Stats> GetStatsByType(TileType t)
{
return new RefValue<Stats>(ref CollectionsMarshal.GetValueRefOrAddDefault(_typeStats, t, out _));
}
//THIS LINE HERE:!
//says: CS 1612: The return value is not a variable and therefore cannot be changed
public ref readonly Stats ExampleAsRef => ref GetStatsByType(TileType.Blue).Item1;
I don't understand why... I am keeping everything in sync/aligned, like: the property returns ref readonly, the function returns ref readonly, the 'T' value of 'RefValue' is ref readonly, how does it come that he does want to accept that, I don't legit understand :/
Would be super cool if some1 has an answer to this, I do not..
Best regards
Stats is a simple struct, with some primitive fields. PS: am using .NET 7 RC2, mac OS X 10.15, C#11
PS2: here the missing types:
public struct Stats
{
/// <summary>
/// Count-Clicks, with maxTime inbetween them
/// </summary>
public int? Click;
public int? Swaps;
public int? Match;
public int? RePainted;
public int? Destroyed;
public Stats()
{
Destroyed = 0;
Click = 0;
Swaps = 0;
Match = 0;
RePainted = 0;
}
}
public enum TileType : short
{
Red, Blue, Green, Purple, Violet,
Length = Violet + 1, Empty = -1,
}
I also tried this, without any success as well:
public ref readonly Stats GetStatsByType(TileType t)
{
return ref (new RefTuple<Stats>(ref CollectionsMarshal.GetValueRefOrAddDefault(TypeStats, t, out var existedB4))).Item1;
}