Record ToString() override don't print non-public properties #4165
-
Hi everyone, I was watching this video from Tim Corey, and around minute 37:00 he mention that internal properties are not being returned from the ToString() record override, even if the caller is in the same assembly. First I thought this could be due to his way of setting up the record, using primary constructor, but then I fully initialized a record and still got the same result. Probably this is by design, but maybe someone could clarify the rationale behind printing public-only properties, even if the caller fulfill the constraints of public class Program
{
public static void Main(string[] args)
{
Student john = new("John", "Doe");
Console.WriteLine($"Hello {john}!"); //Will print only 'Doe'
}
}
public record Student
{
public Student(string FirstName, string LastName)
{
this.FirstName = FirstName;
this.LastName = LastName;
}
internal string FirstName { get; init; }
public string LastName { get; init; }
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
This isn't a thing in C#. There isn't a way for |
Beta Was this translation helpful? Give feedback.
This isn't a thing in C#. There isn't a way for
ToString()
to know whether it's being called from the same assembly or from a different assembly, or even if that should mean anything. For example, it could be called from an internal logging adapter that's then getting sent to a non-internal method. Very intentionally, we do not include internal members.