C#中List〈string〉和string[]数组之间的相互转换

时间:2023-07-11 22:33:27 

1,从System.String[]转到List<System.String>

System.String[] str={"str","string","abc"};

List<System.String> listS=new List<System.String>(str);

 

2, 从List<System.String>转到System.String[]

List<System.String> listS=new List<System.String>();

listS.Add("str");

listS.Add("hello");

System.String[] str=listS.ToArray();

 

测试如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            System.String[] sA = { "str","string1","sting2","abc"};
            List<System.String> sL = new List<System.String>();
            for (System.Int32 i = 0; i < sA.Length;i++ )
            {
                Console.WriteLine("sA[{0}]={1}",i,sA[i]);
            }
            sL = new List<System.String>(sA);
            sL.Add("Hello!");
            foreach(System.String s in sL)
            {
                Console.WriteLine(s);
            }
            System.String[] nextString = sL.ToArray();
            Console.WriteLine("The Length of nextString is {0}",nextString.Length);
            Console.Read();
        }
    }
}

结果显示:

C#中List〈string〉和string[]数组之间的相互转换

标签:List<string>,string,c#
0
投稿

猜你喜欢

  • C#使用Resources资源文件

    2023-07-04 08:42:48
  • IDEA-Maven项目的jdk版本设置方法

    2022-07-21 16:04:17
  • Java如何给Word文档添加多行文字水印

    2023-11-10 07:40:58
  • Java网络编程之简易聊天室的实现

    2022-05-05 22:23:45
  • SpringCloud超详细讲解微服务网关Gateway

    2021-09-11 14:54:49
  • Mybatis之association和collection用法

    2021-10-13 10:09:10
  • 用Linq从一个集合选取几列得到一个新的集合(可改列名)

    2023-08-23 22:48:23
  • SWT(JFace) 文本编辑器 实现代码

    2023-01-27 22:39:37
  • Android实现闪光灯效果

    2023-08-02 12:20:06
  • Java 三种进制的数值常量操作

    2021-11-14 21:39:41
  • C#自定义签名章实现方法

    2022-08-02 05:22:37
  • Java Comparable及Comparator接口区别详解

    2023-07-03 08:19:50
  • 全面详解Spring Bean生命周期教程示例

    2023-08-09 11:41:15
  • java日期格式化SimpleDateFormat的使用详解

    2023-08-25 03:22:15
  • Spring Security OAuth2认证授权示例详解

    2022-09-11 19:45:47
  • android panellistview 圆角实现代码

    2022-09-03 17:34:19
  • Android沉浸式顶部实现代码及效果

    2021-06-19 18:51:49
  • Android viewpager 3D画廊的实现方法

    2021-05-28 21:19:27
  • Android ProgressDialog使用总结

    2023-07-10 22:16:33
  • Android实现带指示点的自动轮播无限循环效果

    2021-12-09 19:22:44
  • asp之家 软件编程 m.aspxhome.com