Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3423182
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:19:29+00:00 2026-05-18T06:19:29+00:00

Is there a way in c# to loop over the properties of a class?

  • 0

Is there a way in c# to loop over the properties of a class?

Basically I have a class that contains a large number of property’s (it basically holds the results of a large database query).
I need to output these results as a CSV file so need to append each value to a string.

The obvious way it to manually append each value to a string, but is there a way to effectively loop over the results object and add the value for each property in turn?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T06:19:29+00:00Added an answer on May 18, 2026 at 6:19 am

    Sure; you can do that in many ways; starting with reflection (note, this is slowish – OK for moderate amounts of data though):

    var props = objectType.GetProperties();
    foreach(object obj in data) {
        foreach(var prop in props) {
            object value = prop.GetValue(obj, null); // against prop.Name
        }
    }
    

    However; for larger volumes of data it would be worth making this more efficient; for example here I use the Expression API to pre-compile a delegate that looks writes each property – the advantage here is that no reflection is done on a per-row basis (this should be significantly faster for large volumes of data):

    static void Main()
    {        
        var data = new[] {
           new { Foo = 123, Bar = "abc" },
           new { Foo = 456, Bar = "def" },
           new { Foo = 789, Bar = "ghi" },
        };
        string s = Write(data);        
    }
    static Expression StringBuilderAppend(Expression instance, Expression arg)
    {
        var method = typeof(StringBuilder).GetMethod("Append", new Type[] { arg.Type });
        return Expression.Call(instance, method, arg);
    }
    static string Write<T>(IEnumerable<T> data)
    {
        var props = typeof(T).GetProperties();
        var sb = Expression.Parameter(typeof(StringBuilder));
        var obj = Expression.Parameter(typeof(T));
        Expression body = sb;
        foreach(var prop in props) {            
            body = StringBuilderAppend(body, Expression.Property(obj, prop));
            body = StringBuilderAppend(body, Expression.Constant("="));
            body = StringBuilderAppend(body, Expression.Constant(prop.Name));
            body = StringBuilderAppend(body, Expression.Constant("; "));
        }
        body = Expression.Call(body, "AppendLine", Type.EmptyTypes);
        var lambda = Expression.Lambda<Func<StringBuilder, T, StringBuilder>>(body, sb, obj);
        var func = lambda.Compile();
    
        var result = new StringBuilder();
        foreach (T row in data)
        {
            func(result, row);
        }
        return result.ToString();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there are a way to recursively loop over all the nested properties of
If I have a thread in an infinite loop, is there a way to
Is there a way to loop over the history object in javascript to find
Is there a way to loop over multiple lists in parallel in a makefile
I have a class that has 3 properties: class Three { int ID {
I'm wondering if there's a way to loop over a values specified in the
is there a way to simplify a 3-nested loop over a 3d-field? The code
Is there a good way to loop over a string with sscanf ? Let's
Is there a way to iterate over properties in an object without using a
Is there a way to loop in while if you start the script with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.