问题描述
如题,C#/.NET应用程序编程开发中,在不使用循环语句的情况下如何实现复制一个集合到另一个集合?
比如,现在有一个数字集合list
:
var list = new List<int>{ 1,2,3,4,5,6,7,8,9};
现要求在不使用循环语句(for,foreach,while等)的情况下,将list
集合中的元素全部复制到另一个集合中,使用C#/.NET应该如何实现呢?
方案一
使用关键字new
创建一个泛型集合实例,将list
集合作为构造函数传入即可,如下:
List<int> copy = new List<int>(list);
完整示例:
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
var list = new List<int>{ 1,2,3,4,5,6,7,8,9};
var copyList= Copy(list);
foreach(var num in copyList){
Console.Write("{0} ",num);
}
}
private static List<int> Copy(List<int> list)
{
var copy = new List<int>(list);
return copy;
}
}
方案二
如果在.NET 3.0+版本中,可以直接调用List<T>.ToList()
方法,如下:
List<int> copy = list.ToList();
完整示例:
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
var list = new List<int>{ 1,2,3,4,5,6,7,8,9};
var copyList= Copy(list);
list[0]=0;
Console.WriteLine("original list:");
foreach(var num in list){
Console.Write("{0} ",num);
}
Console.WriteLine();
Console.WriteLine("copy list:");
foreach(var num in copyList){
Console.Write("{0} ",num);
}
}
private static List<int> Copy(List<int> list)
{
var copy = list.ToList();
return copy;
}
}
输出结果:
original list:
0 2 3 4 5 6 7 8 9
copy list:
1 2 3 4 5 6 7 8 9
方案三
调用List<T>.AddRange()
方法,如下(完整示例):
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
var list = new List<int>{ 1,2,3,4,5,6,7,8,9};
var copyList= Copy(list);
list[0]=0;
Console.WriteLine("original list:");
foreach(var num in list){
Console.Write("{0} ",num);
}
Console.WriteLine();
Console.WriteLine("copy list:");
foreach(var num in copyList){
Console.Write("{0} ",num);
}
}
private static List<int> Copy(List<int> list)
{
var copy = new List<int>();
copy.AddRange(list);
return copy;
}
}
输出结果:
original list:
0 2 3 4 5 6 7 8 9
copy list:
1 2 3 4 5 6 7 8 9
方案四
使用BinaryFormatter
序列号化与反序列化来复制一个对象,如下:
public static List<T> Copy<T>(this List<T> lst)
{
List<T> lstCopy = new List<T>();
foreach (var item in lst)
{
using (MemoryStream stream = new MemoryStream())
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, item);
stream.Position = 0;
lstCopy.Add((T)formatter.Deserialize(stream));
}
}
return lstCopy;
}
完整示例:
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
public class Program
{
public static void Main()
{
var list = new List<int>{ 1,2,3,4,5,6,7,8,9};
var copyList= list.Copy();
list[0]=0;
Console.WriteLine("original list:");
foreach(var num in list){
Console.Write("{0} ",num);
}
Console.WriteLine();
Console.WriteLine("copy list:");
foreach(var num in copyList){
Console.Write("{0} ",num);
}
}
}
public static class ListExtensions
{
public static List<T> Copy<T>(this List<T> lst)
{
List<T> lstCopy = new List<T>();
foreach (var item in lst)
{
using (MemoryStream stream = new MemoryStream())
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, item);
stream.Position = 0;
lstCopy.Add((T)formatter.Deserialize(stream));
}
}
return lstCopy;
}
}
输出结果:
original list:
0 2 3 4 5 6 7 8 9
copy list:
1 2 3 4 5 6 7 8 9
温馨提示:本文标注有(完整示例)的示例代码可以直接粘贴在try.dot.net中运行。
版权声明:本作品系原创,版权归码友网所有,如未经许可,禁止任何形式转载,违者必究。
发表评论
登录用户才能发表评论, 请 登 录 或者 注册