Open In App

C# | Join Method | Set – 2

Improve
Improve
Like Article
Like
Save
Share
Report

In C#, Join() is a string method. This method is used to concatenates the members of a collection or the elements of the specified array, using the specified separator between each member or element. This method can be overloaded by passing different parameters to it. For the first three methods, please refer to Join() Method in C# Set -1.

String.Join(String, IEnumerable<String>)

This method is used to concatenates the members of a constructed collection of type String, using the specified separator between each member.

Syntax:

public static string Join(string separator, IEnumerable L1)

Parameters:

separator: It is string which is use as a separator.separator is included in the returned string only if values has more than one element and type of this is System.String.

L1: It is a collection that contains the strings to concatenate and type of this is System.Collections.Generic.IEnumerable<String>.

Return Type: This method returns a string of type System.String which consists of the members of values delimited by the separator string. If values has no members, the method returns String.Empty.

Exception: This method can give ArgumentNullException if the L1 is null.

Example: In the below program, a list of words is created using in-built list collections. So the object of same List collection is passed along with separator in the join ( ) method and as a result, the user gets the resulted string.




// C# program to demonstrate the 
// Join(String, IEnumerable <string> L1 )
using System;
using System.Collections.Generic;
  
namespace ConsoleApplication1 {
  
class Geeks {
      
    // Main Method
    static void Main(string[] args)
    {
  
        // getting the added words 
        // from list collections
        // and copying them into 
        // another object of list type
        List<String> alpha = AddWords();
  
        // passing the object of list 
        // type along with the separator
        string str1 = string.Join("--", alpha);
  
        // getting the value of the string..
        Console.WriteLine("The value of the string is " + str1);
  
    }
  
    // creating a collection of
    // string values using List
    private static List<String> AddWords()
    {
  
        List<String> alpha = new List<string>();
  
        // methods to add a string into list
        alpha.Add("Hello");
        alpha.Add("Geeks");
        alpha.Add("How");
        alpha.Add("are");
        alpha.Add("you?");
  
        // returning the object 
        // of the list type...
        return alpha;
    }
}
}


Output:

The value of the string is Hello--Geeks--How--are--you?

String.Join<T>(String, IEnumerable<T>)

This method is used to concatenates the members of a constructed collection of any user defined data type say T, using the specified separator between each member.

Syntax:

public static string Join(string separator, IEnumerable T1)

Parameters:

separator: It is string which is use as a separator.separator is included in the returned string only if values has more than one element and type of this is System.String.

T1: It is a collection that contains the objects to concatenate and type of this is System.Collections.Generic.IEnumerable<T>.

Return Type: This method returns a string of type System.String which consists of the members of values delimited by the separator string. If values has no members, the method returns String.Empty.

Exception: This method can give ArgumentNullException if the T1 is null.

Example: In the below code, first a user defined class of items is created and as a result various item names were added to constructor. Also, the list will hold the objects of class “item” type. As a result, in the main method, the list containing the objects of class item type is passed along with separator ‘ / ‘ to get the output string value.




// C# program to demonstrate the 
// Join(String, IEnumerable <T > T1)
using System;
using System.Collections.Generic;
  
namespace ConsoleApplication2 {
      
// making a user defined data type..
public class items {
      
    public string itemname;
      
    // constructor to hold the 
    // string values of item class
    public items(string name1)
    {
        itemname = name1;
    }
  
    public override string ToString()
    {
        return this.itemname;
    }
}
  
class Geeks {
      
    // Main Method
    static void Main(string[] args)
    {
        List<items> alpha = Additems();
  
        // passing the list of objects
        // of item class to join method( )
        string str1 = string.Join("--", alpha);
  
        Console.WriteLine("The value of the string is " + str1);
  
    }
  
    private static List<items> Additems()
    {
          
        // adding the objects of item 
        // class into a list
        List<items> alpha = new List<items>();
        alpha.Add(new items("fans"));
        alpha.Add(new items("Bulb"));
        alpha.Add(new items("Windows"));
        alpha.Add(new items("table"));
        alpha.Add(new items("chair"));
  
        return alpha;
    }
}
}


Output:

The value of the string is fans--Bulb--Windows--table--chair


Last Updated : 31 Jan, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads