In this article you will learn, how you can access your class properties value in a list and perform whatever operation you want with the properties value.
below is my Class named Employee. this class contains four properties.
below is my Class named Employee. this class contains four properties.
public class Employee
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string Mobile { get; set; }
}
In the below class I'm calling a function that takes my class object and add all the property value in the list.
GetPolicyDataString function concating all values with tild "~" seprated.
class Program
{
static void Main(string[] args)
{
Employee pi = new testGenrateString.Employee();
pi.FirstName = "SURAJ";
pi.LastName = "MADDHESHIYA";
pi.Address="Berlin";
pi.Mobile = "8527123456";
Program p = new testGenrateString.Program();
var val = p.GetPolicyDataString(pi);
Console.WriteLine(val);
Console.Read();
}
public string GetPolicyDataString(Employee ppInfo)
{
string dataString = string.Empty;
List<string> lstString = new List<string>();
foreach (var prop in ppInfo.GetType().GetProperties())
{
lstString.Add(Convert.ToString(prop.GetValue(ppInfo, null)));
}
dataString = string.Join("~", lstString.ToArray());
return dataString;
}
}
OUTPUT