我们给一个整数数组ARR和任务是整型数组转换成C#中的列表LST。为了执行此任务,我们有以下方法:
方法1: List
句法:
List lst = new List { 1, 2, 3};
例子:
C#
// C# program to convert a
// given an integer array
// to the list
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
public class GFG{
static void Main(string[] args)
{
// given integer array
// { 10, 20, 30, 40, 50 }
// using List class
List lst = new List { 10, 20, 30, 40, 50 };
// you can write the above line of code as
// int[] ints = new [] { 10, 20, 30, 40, 50 };
// List lst = ints.OfType().ToList();
// printing output
foreach (int i in lst)
{
Console.Write(i + " ");
}
}
}
C#
// C# program to convert a
// given an integer array
// to the list
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
public class GFG{
static List get_list(int[] arr)
{
// List(IEnumerable)
// Constructor
// is a used to convert a
// given an integer array
// to the list
List L = new List (arr);
return L;
}
static void Main(string[] args)
{
// given integer array
int[] arr = { 10, 20, 30, 40, 50 };
// function calling
List lst = get_list(arr);
// printing output
foreach (int i in lst)
{
Console.Write(i + " ");
}
}
}
C#
// C# program to convert a
// given an integer array
// to the list
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
public class GFG{
static List get_list(int[] arr)
{
// AddRange(IEnumerable)
// Method is a used to convert
// given an integer array
// to the list
List L = new List();
L.AddRange(arr);
return L;
}
static void Main(string[] args)
{
// given integer array
int[] arr = { 10, 20, 30, 40, 50 };
// function calling
List lst = get_list(arr);
// printing output
foreach (int i in lst)
{
Console.Write(i + " ");
}
}
}
C#
// C# program to convert a
// given an integer array
// to the list
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
public class GFG{
static List get_list(int[] arr)
{
// ToList() Method is a used
// to convert a given an
// integer array to the list
List L = arr.ToList();
return L;
}
static void Main(string[] args)
{
// given integer array
int[] arr = { 10, 20, 30, 40, 50 };
// function calling
List lst = get_list(arr);
// printing output
foreach (int i in lst)
{
Console.Write(i + " ");
}
}
}
C#
// C# program to convert a
// given an integer array
// to the list
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
public class GFG{
static List get_list(int[] arr)
{
// Add() Method is a used
// to convert a given an
// integer array to the list
List L = new List();
for(int i = 0 ; i < arr.Length ; i++)
{
L.Add(arr[i]);
}
return L;
}
static void Main(string[] args)
{
// given integer array
int[] arr = { 10, 20, 30, 40, 50 };
// function calling
List lst = get_list(arr);
// printing output
foreach (int i in lst)
{
Console.Write(i + " ");
}
}
}
输出:
10 20 30 40 50
方法2: List
句法:
List lst = new List(integer_array);
例子:
C#
// C# program to convert a
// given an integer array
// to the list
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
public class GFG{
static List get_list(int[] arr)
{
// List(IEnumerable)
// Constructor
// is a used to convert a
// given an integer array
// to the list
List L = new List (arr);
return L;
}
static void Main(string[] args)
{
// given integer array
int[] arr = { 10, 20, 30, 40, 50 };
// function calling
List lst = get_list(arr);
// printing output
foreach (int i in lst)
{
Console.Write(i + " ");
}
}
}
输出:
10 20 30 40 50
方法3: AddRange(IEnumerable
句法:
List lst = new List();
lst.AddRange(integer_array)
例子:
C#
// C# program to convert a
// given an integer array
// to the list
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
public class GFG{
static List get_list(int[] arr)
{
// AddRange(IEnumerable)
// Method is a used to convert
// given an integer array
// to the list
List L = new List();
L.AddRange(arr);
return L;
}
static void Main(string[] args)
{
// given integer array
int[] arr = { 10, 20, 30, 40, 50 };
// function calling
List lst = get_list(arr);
// printing output
foreach (int i in lst)
{
Console.Write(i + " ");
}
}
}
输出:
10 20 30 40 50
方法4:ToList()方法: Enumerate.Tolist方法来自System.Linq命名空间,该命名空间从IEnumerable
句法:
List lst = integer_array.ToList();
例子:
C#
// C# program to convert a
// given an integer array
// to the list
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
public class GFG{
static List get_list(int[] arr)
{
// ToList() Method is a used
// to convert a given an
// integer array to the list
List L = arr.ToList();
return L;
}
static void Main(string[] args)
{
// given integer array
int[] arr = { 10, 20, 30, 40, 50 };
// function calling
List lst = get_list(arr);
// printing output
foreach (int i in lst)
{
Console.Write(i + " ");
}
}
}
输出:
10 20 30 40 50
方法5: Add()方法 :此方法用于将对象添加到列表的末尾。因此,这也可以用于将给定的整数数组转换为列表。
句法:
List lst = new List();
lst.Add(int val);
例子:
C#
// C# program to convert a
// given an integer array
// to the list
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
public class GFG{
static List get_list(int[] arr)
{
// Add() Method is a used
// to convert a given an
// integer array to the list
List L = new List();
for(int i = 0 ; i < arr.Length ; i++)
{
L.Add(arr[i]);
}
return L;
}
static void Main(string[] args)
{
// given integer array
int[] arr = { 10, 20, 30, 40, 50 };
// function calling
List lst = get_list(arr);
// printing output
foreach (int i in lst)
{
Console.Write(i + " ");
}
}
}
输出:
10 20 30 40 50