习题20240806

文章目录

1.题目: 创建一个包含数字 1, 2, 3, 4, 5 的 List 并打印出列表的长度。

List<int>list = new List<int> { 1,2,3,4,5};
            Console.WriteLine(list.Count);

2.题目: 给定 List names = new List { “Alice”, “Bob”, “Charlie” },将其第一个元素改为 “Zara”,然后打印整个列表。

List<string>names=new List<string> { "Alice","Bob","Charlie"};
            names[0] = "Zara";
            foreach(var name in names)
            {
                Console.WriteLine(name);
            }

3.题目: 将 List list1 = new List { 1, 2, 3 } 和 List list2 = new List { 4, 5, 6 } 合并为一个列表,并打印结果。

 List<int>list1=new List<int> { 1,2,3};
            List<int>list2=new List<int> { 4,5,6};

            var items=list1.Concat(list2).OrderBy(n=>n);
            foreach(var item in items)
            {
                Console.WriteLine(item);
            }

4.题目: 从 List numbers = new List { 10, 20, 30, 40, 50 } 中移除值为 30 的元素,并打印结果。

List<int> numbers= new List<int> { 10,20,30,40,50};
            var items = numbers.Where(n => n != 30);
            foreach(var item in items)
            {
                Console.WriteLine(item);
            }

5.题目: 在 List names = new List { “John”, “Jane”, “Doe” } 的末尾添加 “Smith”,然后打印整个列表。

List<string> names = new List<string> { "John", "Jane", "Doe" };
            names.Add("Smith");
            foreach(var name in names)
            {
                Console.WriteLine(name);
            }
        }

6.题目: 给定 List numbers = new List { 1, 2, 3, 4, 5 },计算并打印所有元素的总和。

List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
            var items=numbers.Sum(x => x);
            Console.WriteLine(items);

7.题目: 创建一个 List 并向其中添加 10 个随机整数,然后打印这些整数。

List<int>numbers= new List<int>();
            for(int i=0;i<10;i++)
            {
                Random random= new Random();
                numbers.Add(random.Next(10));
            }
            foreach(var number in numbers)
            {
                Console.WriteLine(number);
            }

8.题目: 检查 List numbers = new List { 5, 10, 15, 20 } 是否包含元素 10,并打印结果。

List<int> numbers = new List<int> { 5, 10, 15, 20 };
            bool b=numbers.Any(n=>n==10);
            Console.WriteLine(b);

9.题目: 对 List names = new List { “Alice”, “Bob”, “Charlie” } 进行排序,然后打印排序后的列表。

List<string> names = new List<string> { "Alice", "Bob", "Charlie" };
            var items=names.OrderByDescending(x => x);
            foreach(var item in items)
            {
                Console.WriteLine(item);
            }

10.题目: 通过 List numbers = new List { 1, 2, 3, 4, 5 } 创建一个新的 List,其中包含原列表的每个元素的平方,然后打印新列表。

List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
            var items = numbers.Select(n => new { a = n * n });
            foreach(var item in items )
            {
                Console.WriteLine(item);
            }

11.题目: 给定一个 Dictionary<string, int>, 例如 {“cat”: 1, “dog”: 2}, 向字典中添加一个新的键值对 “bird”: 3,然后打印出字典中的所有项。

Dictionary<string,int>dic= new Dictionary<string, int>
            {
                {"dog",1 },
                {"cat",2 }
            };

            dic.Add("bird", 3);

            foreach(var di in dic)
            {
                Console.WriteLine(di);
            }

12.题目: 创建一个 Dictionary<string, int>,其中包含几个键值对,例如 “apple”: 3, “banana”: 5,然后打印出所有的键和值。

Dictionary<string,int>dic= new Dictionary<string, int>
            {
                {"apple",3 },
                {"banana",5 }
            };

            foreach(var di in dic.Keys)
            {
                Console.WriteLine(di);
            }
            foreach(var di1 in dic.Values)
            {
                Console.WriteLine(di1);
            }

13.题目: 检查 Dictionary<string, int> 是否包含键 “apple”,并打印结果。

Dictionary<string,int>dic= new Dictionary<string, int>
            {
                {"apple",3 },
                {"banana",5 }
            };

            var items = dic.Any(n => n.Key == "apple");
            Console.WriteLine(items);

14.题目: 从 Dictionary<string, int> 中移除键 “banana”,然后打印字典的内容。

Dictionary<string,int>dic= new Dictionary<string, int>
            {
                {"apple",3 },
                {"banana",5 }
            };

            dic.Remove("banana");
            foreach(var item in dic)
            {
                Console.WriteLine(item);
            }

15.题目: 对一个 Dictionary<string, int> 执行遍历,并计算所有值的总和。

Dictionary<string,int>dic= new Dictionary<string, int>
            {
                {"peach",1 },
                {"apple",3 },
                {"banana",5 }
            };

            var items = dic.Sum(n => n.Value);
            Console.WriteLine(items);

16.题目: 创建一个 Dictionary<int, string>,其中键是从 1 到 3 的整数,值是对应的字符串 “one”, “two”, “three”,然后打印字典中的所有项。

Dictionary<int, string> dict = new Dictionary<int, string>
            {
                {1,"one" },
                {2,"two"},
                {3,"three" }
            };

            foreach(var dic in dict)
            {
                Console.WriteLine(dic);
            }

17.题目: 给定 Dictionary<string, int> dictionary = new Dictionary<string, int> { {“A”, 10}, {“B”, 20} },更新键 “A” 的值为 15,然后打印字典的内容。

Dictionary<string, int> dictionary = new Dictionary<string, int>
            {
                {"A",10},
                {"B",20}
            };
            dictionary["A"] = 15;
            foreach(var dic in dictionary)
            {
                Console.WriteLine(dic);
            }

18.题目: 从 Dictionary<string, int> 中获取并打印键为 “orange” 的值,如果该键不存在,则打印 “Key not found”。

Dictionary<string, int> dictionary = new Dictionary<string, int>
            {
                {"A",10},
                {"B",20}
            };
            if(dictionary.TryGetValue("orange",out int orangeCount))
            {
                Console.WriteLine(orangeCount);
            }
            else
            {
                Console.WriteLine("Key not found");
            }

19.题目: 创建一个 Dictionary<string, List>,其中键是 “math” 和 “science”,对应的值是整数列表。将 “math” 的值更新为 {90, 80, 70},然后打印 “math” 的值。

List<int>list1= new List<int> { 100,100,100};
            List<int>list2= new List<int> { 100,100,100};
            Dictionary<string, List<int>> dic = new Dictionary<string, List<int>>
            {
                {"math",list1 },
                {"science",list2 }
            };
            List<int>list3= new List<int> { 90,80,70};
            dic["math"] = list3;
            var items = dic.FirstOrDefault(n => n.Key.Equals("math")).Value;
            foreach(var item in items )
            {
                Console.WriteLine(item);
            }

20.题目: 给定 Dictionary<int, string> dictionary = new Dictionary<int, string> { {1, “one”}, {2, “two”} },使用 LINQ 查询所有键值对,将其转换为一个新的列表,然后打印每对键值对。

Dictionary<int, string> dictionary = new Dictionary<int, string>
            {
                {1,"one" },
                {2,"two"}
            };

            var items=dictionary.ToList();
            foreach(var item in items)
            {
                Console.WriteLine(item);
            }

21.写出线程的定义

线程是CPU调度的基本单位,每个线程执行的都是某一个进程的代码的某个片段。

22.题目一:创建和管理线程

描述:
编写一个 C# 程序,创建一个线程来执行一个任务,并在主线程中等待子线程完成。
要求:
创建一个线程来执行一个简单的任务,例如打印 1 到 10 的数字。
主线程等待子线程完成后,输出“子线程完成”。

 Thread thread = new Thread(() =>
            {
                for (int i = 1; i <= 10; i++)
                {
                    Console.WriteLine(i);
                    Thread.Sleep(500);
                }
            });
            thread.Start();

            thread.Join();

            Console.WriteLine("子线程完成");

23.题目二:线程同步

描述:
编写一个程序,使用 lock 关键字来同步访问一个共享资源。共享资源是一个计数器,每次线程访问时都需要增加计数器的值。
要求:
创建一个共享的计数器变量。
使用多个线程同时增加计数器的值。
使用 lock 确保每次只有一个线程可以修改计数器。


    public class Program
    {
        int num = 0;

        void Compute()
        {
            while (true)
            {
                lock (this)
                {
                    Thread.Sleep(500);
                    Console.WriteLine(Thread.CurrentThread.Name+num);
                    num++;
                }
            }
        }

        static void Main(string[] args)
        {
            Program p = new();
            Thread tA = new(new ThreadStart(p.Compute))
            {
                Name = "线程一"
            };
            Thread tB = new(new ThreadStart(p.Compute))
            {
                Name = "线程二"
            };
            Thread tC = new(new ThreadStart(p.Compute))
            {
                Name = "线程三"
            };
            Thread tD = new(new ThreadStart(p.Compute))
            {
                Name = "线程四"
            };
            tA.Start();
            tB.Start();
            tC.Start();
            tD.Start();
            Console.ReadLine();

        }
    }

24.题目三:线程池

描述:
编写一个程序,使用线程池来执行多个任务。每个任务都输出当前线程的 ID 和任务编号。
要求:
使用 ThreadPool 提交多个任务。
每个任务打印其线程 ID 和任务编号。
确保所有任务执行完毕后,主线程输出“所有任务完成”。

 private static void printStr(string str)
        {
            Console.WriteLine("{0}是:{1}", str, Thread.CurrentThread.ManagedThreadId);
        }

        static void Main(string[] args)
        {
            ThreadPool.GetMaxThreads(out int maxWorkerThreads,out int maxCompletionportThreads);
            Console.WriteLine("最大线程数,工作线程:{0},IO线程数:{1}", maxWorkerThreads, maxCompletionportThreads);
            ThreadPool.GetMinThreads(out int minWorkerThreads, out int minCompletionPortThreads);
            Console.WriteLine("最小线程数,工作线程:{0},IO线程数:{1}", minWorkerThreads, minCompletionPortThreads);

            ThreadPool.SetMaxThreads(20, 20);
            ThreadPool.SetMinThreads(3, 3);
            ThreadPool.GetMaxThreads(out int newmaxWorkerThreads, out int newmaxCompletionportThreads);
            Console.WriteLine("最大线程数,工作线程:{0},IO线程数:{1}", newmaxWorkerThreads, newmaxCompletionportThreads);
            ThreadPool.GetMinThreads(out int newminWorkerThreads, out int newminCompletionPortThreads);
            Console.WriteLine("最小线程数,工作线程:{0},IO线程数:{1}", newminWorkerThreads, newminCompletionPortThreads);


            Console.WriteLine("启动多线程...");
            for(int i = 0; i < 10; i++)
            {
                ThreadPool.QueueUserWorkItem(p => printStr("当前线程"));
            }

            Thread.Sleep(4000);
            Console.WriteLine("所有任务完成...");
            Console.ReadKey();

        }
  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值