Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Hacking & PentestingMy 5 favorite Siri AI ‘onscreen awareness’ tricks – and why(22.09.2026 um 15:06 Uhr)
Apple iOS & macOSDowngrade von iOS 27 auf iOS 26 nicht mehr möglich(22.09.2026 um 14:30 Uhr)
Apple iOS & macOSOnyX und Deeper: Neue Versionen für macOS Golden Gate 27(22.09.2026 um 15:59 Uhr)
Hacking & PentestingMy 5 favorite Siri AI ‘onscreen awareness’ tricks – and why(22.09.2026 um 15:06 Uhr)
Apple iOS & macOSDowngrade von iOS 27 auf iOS 26 nicht mehr möglich(22.09.2026 um 14:30 Uhr)
Apple iOS & macOSOnyX und Deeper: Neue Versionen für macOS Golden Gate 27(22.09.2026 um 15:59 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

C# Override Records ToString

Introduction Learn how to control formatting records by overriding ToString method along with a virtual method PrintMembers. By controlling formatting, a developer can format for outputting to a frontend, along with hiding one or more…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!




Introduction



Learn how to control formatting records by overriding ToString method along with a virtual method PrintMembers. By controlling formatting, a developer can format for outputting to a frontend, along with hiding one or more properties or redacting data.



The record used where based on a condition will hide BirthDate, mask SSN, and fully redact UserName and Password. It will also present PhoneNumbers as a comma-delimited list.




public partial record Person(
string FirstName,
string LastName,
DateOnly BirthDate,
string SSN,
string UserName,
string Password,
string[] PhoneNumbers) : ITaxpayer { }






Shows full and redacted output



Source code






Controlling output



For demonstration, if the project runs from Microsoft Visual Studio, show all property values, and when not executed from Microsoft Visual Studio, redaction is done. When the project is moved to production, either leave the code as is or set RevealSensitiveInformation to false. A json is not used as a user could change the setting. An alternative is to use an environment variable.






Demonstration data






public class MockedData
{
public static List<Person> SamplePeople() =>
[
new("Jane", "Smith", new DateOnly(1990, 5, 22),
"987-65-4321","jsmith","pass1", []),
new("Karen", "Payne", new DateOnly(1985, 1, 15),
"123-45-6789", "kpayne", "pass2",["555-1234", "555-5678","154-3557"]),
new("Michael", "Johnson", new DateOnly(1978, 3, 10),
"987-65-4321", "mjohnson", "pass3",["555-1111", "555-2222"]),
new("Emily", "Davis", new DateOnly(1995, 7, 30),
"456-78-9012", "edavis", "pass4",["555-3333"]),
new("David", "Wilson", new DateOnly(1982, 11, 5),
"321-54-9876", "dwilson", "pass5",["555-4444", "555-5555"]),
];
}









Code to override the default for the above record



The code is placed in a partial class which is optional.




public partial record Person
{
public override string ToString()
{
var builder = new StringBuilder();
var person = this;


if (!Appsettings.Instance.RevealSensitiveInformation)
{

person = person with
{
BirthDate = default,
UserName = "redacted",
Password = "redacted"
};
}

person.PrintMembers(builder);

return builder.ToString();
}

protected virtual bool PrintMembers(StringBuilder builder)
{
if (Appsettings.Instance.RevealSensitiveInformation)
{
builder.Append($"{FirstName,-10}{LastName,-10}{SSN,-13}{BirthDate,-14:MM/dd/yyyy}{UserName,-10}{Password,-12}");
}
else
{
builder.Append($"{FirstName,-10}{LastName,-10}{SSN.MaskSsn(),-13}{BirthDate,-14:MM/dd/yyyy}{UserName,-10}{Password,-12}");
}


if (!(PhoneNumbers?.Length > 0))
{
builder.Append("None");
return true;
}

builder.Append(string.Join(", ", PhoneNumbers));
builder.Append("");

return true;

}
}






ToString override




  • Set Person to the current instance.

  • If reaction, create and new instance of Person redacting BirthDate, UserName and Password using nondestructive mutation.

  • Pass the StringBuilder to PrintMembers which redacts depending on the value of Appsettings.Instance.RevealSensitiveInformation.



For masking SSN, a language extension MaskSsn is utilized.






Usage






internal partial class Program
{
static void Main(string[] args)
{
var people = MockedData.SamplePeople();
AnsiConsole.MarkupLine("[cyan]First Last SSN Birthdate " +
"UserName Password Phone numbers[/]");
foreach (var person in people)
{
AnsiConsole.MarkupLine(person.Colorize());
}

SpectreConsoleHelpers.ExitPrompt();
}
}






Colorize is a language extension method to use Spectre.Console colors for output to the console.






Overriding ToString only



In some cases, using PrintMembers is not necessary, as in the following case, for the conditional output of the Line2 property.




public record Address(string Line1, string? Line2, string City, string Country, string PostCode)
{
public override string ToString() =>
$"{Line1}, {(Line2 is not null ? Line2 + ", " : string.Empty)}{City}, {Country}, {PostCode}";
}






results




private static void DisplayAddressExample()
{
var addresses = new List<Address>
{
new("123 Main St", null, "Portland", "USA", "97201"),
new("456 Maple Ave", "Apt 4B", "Eugene", "USA", "97401"),
new("789 Oak Dr", "", "Salem", "USA", "97301")
};

foreach (var address in addresses)
{
Console.WriteLine(address);
}
}









Summary



With the information provided, a developer can format and redact data for record types.

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten C# Override Records ToString

Thematisch verwandte Begriffe: Override, Records, ToString · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-95667 | The MISP installer scripts (for Debian 12, Debian 13, Ubuntu 24.04, and …
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick