This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public readonly record struct Username | |
{ | |
private string Value { get; } | |
public Username(string value) | |
{ | |
if (string.IsNullOrEmpty(value)) | |
{ | |
throw new ArgumentNullException(nameof(value)); | |
} | |
Value = value; | |
} | |
public override string ToString() | |
{ | |
if (Value == null) | |
{ | |
throw new InvalidCastException("Cannot create default Username"); | |
} | |
return Value; | |
} | |
} |
Interesting idea. Is the if (Value==null) { ... } check in ToString() not needed since it is guarded from being null in the constructor?
Curious on why the check is in ToString().
Because you can do default(Username) in which Value will be null.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Interesting idea. Is the if (Value==null) { ... } check in ToString() not needed since it is guarded from being null in the constructor?
Curious on why the check is in ToString().