samedi 8 avril 2023

Is there any way to return comma separated string containing the values of the properties of a C# object.?

Looking for Solution to return property names on header and then values

public override string ToString()
    {
        StringBuilder builder = new StringBuilder();
        var props = this.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); //        string typeName = this.GetType().Name;


        var itemStr = string.Join(", ",
       props.Select(p => p.GetValue(this,null)? .ToString())
          .ToArray());
        return itemStr.ToString();

    }
}

I try to use string. Join but its returning only values like john, smith.But I need in following format. "firstname","lastname" and then in next line its values "John","smith" "Mary","land"





Aucun commentaire:

Enregistrer un commentaire