ice7

Dynamically override toString

May 8th, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.50 KB | None | 0 0
  1. public class Person
  2. {
  3.     string FName {get;set;}
  4.     string LNAme {get; set;}
  5.     int Age {get;set;}
  6.  
  7.      public string ToString(Person obj)
  8.         {
  9.             string result = string.Empty;
  10.             obj.GetType().GetProperties().ToList().ForEach(p =>
  11.             {
  12.                 result += string.Concat(p.Name + ": " + p.GetValue(obj) + "\r\n");
  13.  
  14.             });
  15.             return result;
  16.         }
  17. }
  18.  
  19. //This is nice becasue you don't have to worry about how many properties you add, delete or change
Advertisement
Add Comment
Please, Sign In to add comment