In this C# tutorial, I will explain to you with examples and practical code on how to convert object to comma separated string in C#.
Convert Object to Comma Separated String in C#
There will be situations where you are required to convert an object or a list of objects into a comma-separated string in C#. So, let us find out various techniques to convert objects into comma-separated strings in C#.
You can use one of the below 4 methods to convert the object to a separated string in C#.
- Using String.Join() method
- Using Simple Object Conversion
- Using LINQ to Transform a List of Objects
- Using Custom Object Conversion
1. Using C# String.Join() method
C# provides a built-in method called String.Join
, which is one of the most straightforward ways to create a comma-separated string from a collection. Here’s the simplest example:
using System;
namespace CommaSeparatedStringExample
{
class Program
{
static void Main(string[] args)
{
string[] fruits = { "Apple", "Banana", "Cherry" };
string result = String.Join(",", fruits);
Console.WriteLine(result);
}
}
}
Here, you can see I run the above code in a console application using Visual Studio, and you can see the output in the screenshot below.
2. Using Simple Object Conversion
Imagine you have a list of integers, and you want to convert this list into a comma-separated string in C#. Here’s how to do it with the complete code:
using System;
using System.Collections.Generic;
namespace IntegerListToCSV
{
class Program
{
static void Main(string[] args)
{
List<int> numbers = new List<int>() { 1, 2, 3, 4, 5 };
string result = String.Join(",", numbers);
Console.WriteLine(result);
}
}
}
Now, when you run the above code, you can see the output in the screenshot below:
3. Using LINQ to Transform a List of Objects
Let’s say you have a list of custom objects and want to convert each object’s specific property to a comma-separated string. Here, you can use LINQ (Language Integrated Query).
Here’s an example with a class named Person
with the complete code.
using System;
using System.Collections.Generic;
using System.Linq;
namespace LINQExample
{
class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<Person> persons = new List<Person>()
{
new Person { Name = "John", Age = 25 },
new Person { Name = "Jane", Age = 30 },
new Person { Name = "Doe", Age = 22 }
};
string names = String.Join(",", persons.Select(p => p.Name));
Console.WriteLine(names); // Output: John,Jane,Doe
}
}
}
4. Using Custom Object Conversion
Sometimes, you may need to convert multiple properties of an object to a comma-separated string in C#. For instance, each object could be represented as “Name:Age” and then each object’s representation could be combined into a larger comma-separated string. Here’s how to achieve that with a complete example.
using System;
using System.Collections.Generic;
using System.Linq;
namespace CustomObjectToCSV
{
class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<Person> persons = new List<Person>()
{
new Person { Name = "John", Age = 25 },
new Person { Name = "Jane", Age = 30 },
new Person { Name = "Doe", Age = 22 }
};
string result = String.Join(",", persons.Select(p => $"{p.Name}:{p.Age}"));
Console.WriteLine(result); // Output: John:25,Jane:30,Doe:22
}
}
}
Conclusion
Converting objects to comma-separated strings in C# is possible by using built-in methods like String.Join
and powerful features like LINQ. I hope you understood “how to convert an object to comma-separated string c#.”
You may also like:
- Convert String to Byte Array in C#
- Convert Object to JSON Without Escape Characters in C#.NET
- How to convert string to double with 2 decimals in C#.Net?
- Add Values to a String Array in C#
Bijay Kumar is a renowned software engineer, accomplished author, and distinguished Microsoft Most Valuable Professional (MVP) specializing in SharePoint. With a rich professional background spanning over 15 years, Bijay has established himself as an authority in the field of information technology. He possesses unparalleled expertise in multiple programming languages and technologies such as ASP.NET, ASP.NET MVC, C#.NET, and SharePoint, which has enabled him to develop innovative and cutting-edge solutions for clients across the globe. Read more…