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 656507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:43:19+00:00 2026-05-13T22:43:19+00:00

In the Java world we have Apache Commons’ ToStringBuilder to help with creating toString()

  • 0

In the Java world we have Apache Commons’ ToStringBuilder to help with creating toString() implementations.

Does anyone know of a decent free implementation for C#? Are there better alternatives I don’t know about?

If no free implementation exists than I guess this question becomes more of a question of “What would make a good ToStringBuilder in C# 3?”

Off the top of my head:

  • It could offer both reflection and manual ToString string creation.

  • It would be really cool if it could make use of Expression trees.

Something like this..

 public override string ToString()
   {
      return new ToStringBuilder<Foo>(this)
         .Append(t => t.Id)
         .Append(t => t.Name)
         .ToString();
   }

Which would return:

 "Foo{Id: 1, Name: AName}"
  • It could use System.Reflection.Emit to precompile a ToString delegate.

Any other ideas?

UPDATE

Just to clarify ToStringBuilder is a different creature to StringBuilder.. I’m looking for something akin to the functionality of Apache Common’s ToStringBuilder, it has features such as multi-line formatting, different styles and reflection base ToString creation. Thanks.

UPDATE 2

I’ve built my own. See here.

  • 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-13T22:43:19+00:00Added an answer on May 13, 2026 at 10:43 pm

    EDIT: OK, you want to use reflection so you don’t have to type property names. I think this will get you what you’re after:

    // forgive the mangled code; I hate horizontal scrolling
    public sealed class ToStringBuilder<T> {
        private T _obj;
        private Type _objType;
        private StringBuilder _innerSb;
    
        public ToStringBuilder(T obj) {
            _obj = obj;
            _objType = obj.GetType();
            _innerSb = new StringBuilder();
        }
    
        public ToStringBuilder<T> Append<TProperty>
        (Expression<Func<T, TProperty>> expression) {
    
            string propertyName;
            if (!TryGetPropertyName(expression, out propertyName))
                throw new ArgumentException(
                    "Expression must be a simple property expression."
                );
    
            Func<T, TProperty> func = expression.Compile();
    
            if (_innerSb.Length < 1)
                _innerSb.Append(
                    propertyName + ": " + func(_obj).ToString()
                );
            else
                _innerSb.Append(
                    ", " + propertyName + ": " + func(_obj).ToString()
                );
    
            return this;
        }
    
        private static bool TryGetPropertyName<TProperty>
        (Expression<Func<T, TProperty>> expression, out string propertyName) {
    
            propertyName = default(string);
    
            var propertyExpression = expression.Body as MemberExpression;
            if (propertyExpression == null)
                return false;
    
            propertyName = propertyExpression.Member.Name;
            return true;
        }
    
        public override string ToString() {
            return _objType.Name + "{" + _innerSb.ToString() + "}";
        }
    }
    

    Example:

    // from within some class with an Id and Name property
    public override string ToString() {
        return new ToStringBuilder<SomeClass>(this)
            .Append(x => x.Id)
            .Append(x => x.Name)
            .ToString();
    }
    

    Behold, the behavior you’re after:

    class Thing {
        public int Id { get; set; }
        public string Name { get; set; }
    
        public override string ToString() {
            return new ToStringBuilder<Thing>(this)
                .Append(t => t.Id)
                .Append(t => t.Name)
                .ToString()
        }
    }
    
    void Main() {
        var t = new Thing { Id = 10, Name = "Bob" };
        Console.WriteLine(t.ToString());
    }
    

    Output:

    Thing{Id: 10, Name: “Bob”}

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a java application on Websphere that is using Apache Commons FTPClient to
I have a Java application that uses an Apache Derby database with the embedded
I currently have a java applet sitting on my Apache server (in the htdocs
An enum in Java implements the Comparable interface. It would have been nice to
I would like to have a Java component which has a resize icon on
I have a single user java program that I would like to have store
I have an application where I would like to have mixed Java and Scala
We have a couple of applications running on Java 5 and would like now
I am developing a Java desktop application and would like to have an external
I have a Java application and I would like to make it extensible. To

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.