c# foreach property in object code example
Example 1: loop over object properties c#
foreach (PropertyInfo prop in someObject.GetType().GetProperties())
{
Console.WriteLine($"{prop.Name}: {prop.GetValue(someObject, null)}");
}
Example 2: for each property in object c#
foreach (PropertyInfo propertyInfo in obj.GetType().GetProperties())
{
}
Example 3: c# loop through object
static void Main()
{
foreach (int number in SomeNumbers())
{
Console.Write(number.ToString() + " ");
}
Console.ReadKey();
}
public static System.Collections.IEnumerable SomeNumbers()
{
yield return 3;
yield return 5;
yield return 8;
}