• C# 学习 关于List的总结


    List是C#中常见的可伸缩数组组件,因为是可伸缩的,常常被用来代替数组,不需要手动分配数组大小。

    1. /* 首先是创建了一个字符串列表,在列表中添加了三个名称,并打印所有大写的名称. */
    2. using System;
    3. using System.Collections.Generic;
    4. namespace list_tutorial
    5. {
    6. class Program
    7. {
    8. static void Main(string[] args)
    9. {
    10. var names = new List<string> {
    11. "GG","Ana","Felipe"
    12. };
    13. /*这里是使用字符串内插功能.加个$ 符号,可以将内容打印出来.{name.ToUpper()} 作用是将上面list中的内容全部大写的字母. */
    14. foreach (var name in names)
    15. {
    16. Console.WriteLine($"Hello{name.ToUpper()}!");
    17. }
    18. }
    19. }
    20. }

    (1)添加元素、删除元素

    (2)添加索引

    (3)使用Count确定列表的长度,使用的方法是{names.Count}

    (4)使用names.Sort()对list进行排序

    1. using System;
    2. using System.Collections.Generic;
    3. namespace list_tutorial
    4. {
    5. class Program
    6. {
    7. static void Main(string[] args)
    8. {
    9. var names = new List<string> {
    10. "GG","Ana","Felipe"
    11. };
    12. // add作用是添加
    13. names.Add("Maria");
    14. names.Add("Bill");
    15. //remove作用是删除
    16. names.Remove("Ana");
    17. foreach (var name in names)
    18. {
    19. Console.WriteLine($"Hello{name.ToUpper()}!");
    20. }
    21. //添加索引
    22. Console.WriteLine($"My name is {names[0]}");
    23. }
    24. }
    25. }

    完整的代码

    1. using System;
    2. using System.Collections.Generic;
    3. namespace list_tutorial
    4. {
    5. class Program
    6. {
    7. static void Main(string[] args)
    8. {
    9. WorkingWithStrings();
    10. }
    11. static void WorkingWithStrings()
    12. {
    13. var names = new List<string>{"name", "Ana" , "Felipe"};
    14. foreach (var name in names)
    15. {
    16. Console.WriteLine($"Hello {name.ToUpper()}!");
    17. }
    18. Console.WriteLine();
    19. names.Add("Maria");
    20. names.Add("Bill");
    21. names.Remove("Ana");
    22. foreach (var name in names)
    23. {
    24. Console.WriteLine($"Hello {name.ToUpper()}!");
    25. }
    26. Console.WriteLine($"my name is {names[0]}");
    27. Console.WriteLine($"The list has {names.Count} people in it");
    28. var index = names.IndexOf("Felipe");
    29. if (index == -1)
    30. {
    31. Console.WriteLine($"IndexOf returns {index}");
    32. }
    33. else
    34. {
    35. Console.WriteLine($"The name {names[index]} is at index {index}");
    36. }
    37. index = names.IndexOf("Not Found");
    38. if (index == -1)
    39. {
    40. Console.WriteLine($"IndexOf returns {index}");
    41. }
    42. else
    43. {
    44. Console.WriteLine($"The name {names[index]} is at index {index}");
    45. }
    46. names.Sort();
    47. foreach (var name in names)
    48. {
    49. Console.WriteLine($"hello {name.ToUpper()}!");
    50. }
    51. }
    52. }
    53. }

  • 相关阅读:
    【表结构数据】—CDA-LEVEL1备考
    51驱动AS608光学指纹识别模块 12864显示
    Java中的Listener和Adapter
    17.1 隐藏执行CMD命令
    Qt之Model/View架构
    软件架构设计(八) 基于架构的软件开发方法
    并联电容器电容量测试
    力扣2860 补9.20
    【Docker 基础教程】侃侃而谈Docker镜像
    Spring5入门到实战------17、Spring5新功能 --Nullable注解和函数式注册对象。整合JUnit5单元测试框架
  • 原文地址:https://blog.csdn.net/qwazp3526cn/article/details/126991550