小编Cal*_*tor的帖子

c# 无论如何要向 List<Type> 添加其他方法?

问题

我有 Point2D 类作为基类,用于存储我通常使用的点列表,List<Point2D>但现在我想添加其他方法和很少的属性来List<Point2D>喜欢 ToString 打印方法,根据特定坐标排序,过滤点的特定方法,我不不想使用扩展方法。

我尝试过的事情

我创建了一个Point2DList继承List<Point2D>类的新类,该类可以正常使用,但是当使用 FindAll 函数时,它现在返回List<Point2D>但我希望它返回Point2DList。我知道我可以编写自己的方法来接受谓词委托,但这工作量太大了。

代码

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

namespace ConsoleAppTest
{
    internal static class Program
    {
        private static void Main()
        {
            try
            {
                var points = new Point2DList();
                points.Add(new Point2D(0, 0));
                points.Add(new Point2D(10, 0));
                points.Add(new Point2D(10, 10));
                points.Add(new Point2D(0, 10));
                Console.WriteLine(points.ToString());
                Point2DList newPoints = points.FindAll((x) => x.X == 10);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                Console.ReadLine();
            } …
Run Code Online (Sandbox Code Playgroud)

c#

-1
推荐指数
1
解决办法
123
查看次数

标签 统计

c# ×1