How to add the first element of a list to another list in C#

1 Answer

0 votes
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        List<string> source = new List<string> { "aaa", "bbb", "ccc" };
        List<string> target = new List<string> { "ddd" };

        if (source.Count > 0) {
            target.Add(source[0]);  // Add the first element of source to target
        }

        Console.WriteLine(string.Join(", ", target));  
    }
}


/*
run:

ddd, aaa

*/

 



answered Oct 16, 2025 by avibootz
...