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

  • Home
  • SEARCH
  • 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 8317103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:30:43+00:00 2026-06-08T21:30:43+00:00

I created a struct: public struct User { public string name; public string email;

  • 0

I created a struct:

public struct User {
   public string name;
   public string email;
   public string age;
}

Then create one:

 User user = new User();
 user.name = "Freewind";
 user.email = "abc@test.com";
 user.age = 100;

Then display it:

MessageBox.Show(user.ToString());

I hope it can print all of the fields of the user struct, but it’s not. It just shows:

MyApp.User

Is there a easy way to display all the fields of a struct?

  • 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-06-08T21:30:46+00:00Added an answer on June 8, 2026 at 9:30 pm

    Override the ToString method on your struct:

    public override string ToString()
    {
        return String.Format("name={0}, email={1}, age={2}", this.name, this.email, this.age);
    }
    

    Note that this is not automatic and you will have to manually add any fields/properties to the string.

    With reflection you can do something like this:

    public override string ToString()
    {
        Type type = this.GetType();
        FieldInfo[] fields = type.GetFields();
        PropertyInfo[] properties = type.GetProperties();
        User user = this;
    
        Dictionary<string, object> values = new Dictionary<string, object>();
        Array.ForEach(fields, (field) => values.Add(field.Name, field.GetValue(user)));
        Array.ForEach(properties, (property) =>
            {
                if (property.CanRead)
                    values.Add(property.Name, property.GetValue(user, null));
            });
    
        return String.Join(", ", values);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I created a simple struct which consists of two value types. public struct Identifier
I would like to define a global/public variable that is created by struct. I
I have the following code: #include <map> #include <string> class policy1 { public: struct
I have this custom type: public struct PasswordString { private string value; public PasswordString(string
I want to create a class one of whose private: members is a struct
I have the following struct defined in a user control: public struct ColumnData {
I have a group of 13 properties in a class. I created a struct
I have created the following C library for reading an image: typedef struct {
I want to create a struct from data harvested by line from a file.
I want to create a struct which is like a CGPoint, but with 3

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.