C
C#14mo ago
Caeden

❔ Optional function parameter

I have this function public void abc(Entity entity, List<string> directions, Position occupied = null but I get CS1750: A type of value <null> cannot be used as a default parameter because there are no standard conversions to type Position
17 Replies
Thinker
Thinker14mo ago
Is Position a struct? In which case you could do Position? occupied = null
Caeden
CaedenOP14mo ago
Yes
Thinker
Thinker14mo ago
yeah structs can't be null unless you have a ? at the end which indicates that it's nullable
Caeden
CaedenOP14mo ago
Cannot convert Position? to Position
Thinker
Thinker14mo ago
Show your actual code
Caeden
CaedenOP14mo ago
The error above came from Position? x = null; as a test Crap it works if both types are ? Thabks
MODiX
MODiX14mo ago
Thinker
REPL Result: Success
record struct Position(int X, int Y);

Position? x = null;
record struct Position(int X, int Y);

Position? x = null;
Compile: 523.168ms | Execution: 74.427ms | React with ❌ to remove this embed.
Thinker
Thinker14mo ago
works fine
Caeden
CaedenOP14mo ago
I have the parameter Position? which works I have an if statement to check if it is null, and in the else I assume its not null and use the properties. C# raises error CS1061: Position? does not contain a definition for …
Thinker
Thinker14mo ago
yeah you have to check that the value isn't null and then you can access .Value which is the actual value
MODiX
MODiX14mo ago
Thinker
REPL Result: Success
record struct Position(int X, int Y);

Position? x = new(1, 2);

if (x is not null)
{
Console.WriteLine($"X is {x.Value.X}");
}
record struct Position(int X, int Y);

Position? x = new(1, 2);

if (x is not null)
{
Console.WriteLine($"X is {x.Value.X}");
}
Console Output
X is 1
X is 1
Compile: 642.851ms | Execution: 86.451ms | React with ❌ to remove this embed.
Caeden
CaedenOP14mo ago
It works but how come? What does the ? do?
Thinker
Thinker14mo ago
Makes the type nullable Structs cannot be null by themselves, so there's a special type called Nullable<T> which wraps around other structs and makes them able to be null. Nullable<T> looks a bit like this:
public readonly struct Nullable<T>
{
public bool HasValue { get; }

public T Value { get; }
}
public readonly struct Nullable<T>
{
public bool HasValue { get; }

public T Value { get; }
}
It has two properties, HasValue which indicates whether the value is null or not, and Value which is the actual wrapped value in case it's not null. Position? is just syntactic sugar for Nullable<Position> So if you want a struct which can be null then you put a ? at the end of it. Now you can access the value using .Value, however you first need to make sure that the value actually exists, otherwise you'll get an exception.
Caeden
CaedenOP14mo ago
Ah very nice Would you wrap a class in nullable
Thinker
Thinker14mo ago
No because reference types can already be null ? for reference types (classes, interfaces, etc.) is just a compiler annotation which marks them as possibly null
Caeden
CaedenOP14mo ago
Okay, thank you awesome explanation above
Accord
Accord14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server