2020-11-24

C#将 List 转为 byte[]

如果需要查看更多文章,请微信搜索公众号 csharp编程大全,需要进C#交流群群请加微信z438679770,备注进群, 我邀请你进群! ! !

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.IO;namespace list{ public partial class Form1 : Form {  public Form1()  {   InitializeComponent();  }  //将List<double> 转为 byte[]  static byte[] ConvertDoubleArrayToBytes(List<double> matrix)  {   if (matrix == null)   {    return new byte[0];   }   using (MemoryStream stream = new MemoryStream())   {    BinaryWriter bw = new BinaryWriter(stream);    foreach (var item in matrix)    {     bw.Write(item);    }    return stream.ToArray();   }  }  //将byte[] 转为 List<double>  static List<double> ConvertBytesToDoubleArray(byte[] matrix)  {   if (matrix == null)    return null;   List<double> result = new List<double>();   using (var br = new BinaryReader(new MemoryStream(matrix)))   {    var ptCount = matrix.Length / 8;    for (int i = 0; i < ptCount; i++)    {     result.Add(br.ReadDouble());    }    return result;   }  }  private void Form1_Load(object sender, EventArgs e)  {   List<double> data = new List<double> { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 };   byte[] bdata = ConvertDoubleArrayToBytes(data);   data = ConvertBytesToDoubleArray(bdata);   foreach (var item in data)   {    listBox1.Items.Add(item);   }     } }}

  

 

 

List<T>是泛型集合
这种集合规定了集合内的数据类型,只能存放<T>的T类型数据;
而ArrayList不是泛型,这种集合中可以存放任意类型数据;
举个简单例子:List<Student> students=new List<Student>(); 那么读取数据时就不用类型转化了,即:Student stu=students[0]; 增、删、改、查的方法:students.Add(T t);//增 students.Remove(int index);//删 stucents.Remove(T t);//删 students[]=//修改的数据 //查或者改 遍历集合类似于遍历数组 ArrayList students=new ArrayList(); Student stu=students[0] as Student;









原文转载:http://www.shaoqun.com/a/492998.html

kk馆:https://www.ikjzd.com/w/1713

萌店:https://www.ikjzd.com/w/1538

卖家精灵:https://www.ikjzd.com/w/532


如果需要查看更多文章,请微信搜索公众号csharp编程大全,需要进C#交流群群请加微信z438679770,备注进群,我邀请你进群!!!usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSy
克雷格:克雷格
sonar:sonar
亚马逊新政即将实施!事关店铺安全!:亚马逊新政即将实施!事关店铺安全!
深圳阿尔卑斯山滑雪场在哪?:深圳阿尔卑斯山滑雪场在哪?
三亚酒店预订注意事项 :三亚酒店预订注意事项

No comments:

Post a Comment