c#将字节数组转换成整数
Given an integer array and we have to convert it into a list in C#.
给定一个整数数组,我们必须将其转换为C#列表。
将int []转换为List <int> (Converting int[] to List<int>)
A list is used to represent the list of the objects, it is represented as List<T>, where T is the type of the list objects/elements.
列表用于表示对象的列表,它表示为List <T> ,其中T是列表对象/元素的类型。
A list is a class which comes under System.Collections.Generic package, so we have to include it first.
列表是System.Collections.Generic包下的一个类,因此我们必须首先包含它。
To convert an integer array to the list, we use toList() method of System.Linq. Here, is the syntax of the expression that converts an integer array to the list.
要将整数数组转换为列表 ,我们使用System.Linq的 toList()方法。 这是将整数数组转换为列表的表达式的语法。
List<T> arr_list = array_name.OfType<T>().ToList();
Here, T is the type and array_name is the input array.
在这里, T是类型, array_name是输入数组。
Example (for integer array)
示例(对于整数数组)
List<int> arr_list = arr.OfType<int>().ToList();
Here, arr is the input array.
在这里, arr是输入数组。
Syntax:
句法:
var array_name = new[] {initialize_list/elements};
C#程序将整数数组转换为列表 (C# program to convert an integer array to the list )
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
namespace Test
{
class Program
{
static void Main(string[] args)
{
var arr = new[] { 10, 20, 30, 40, 50 };
//creating list
List<int> arr_list = arr.OfType<int>().ToList();
//printing the types of variables
Console.WriteLine("type of arr: " + arr.GetType());
Console.WriteLine("type of arr_list: " + arr_list.GetType());
Console.WriteLine("List elements...");
//printing list elements
foreach (int item in arr_list)
{
Console.Write(item + " ");
}
Console.WriteLine();
//hit ENTER to exit
Console.ReadLine();
}
}
}
Output
输出量
type of arr: System.Int32[]
type of arr_list: System.Collections.Generic.List`1[System.Int32]
List elements...
10 20 30 40 50
翻译自: https://www.includehelp.com/dot-net/convert-an-integer-array-to-the-list-in-c-sharp.aspx
c#将字节数组转换成整数