相关疑难解决方法(0)

创建流畅的API

如何创建一个流畅的API?

这主要使用扩展方法吗?

c# fluent

53
推荐指数
6
解决办法
4万
查看次数

扩展方法的缺点?

扩展方法是一个非常有用的功能,您可以在任何类中添加所需的许多功能.但我想知道是否有任何不利因素可能给我带来麻烦.有什么意见或建议吗?

c# extension-methods

28
推荐指数
2
解决办法
7182
查看次数

扩展List <T>并违反开放/封闭原则

我刚刚在我的一个类中创建了以下方法

public static bool Assimilate(this List<Card> first, List<Card> second)
{
    // Trivial
    if (first.Count == 0 || second.Count == 0)
    {
        return false;
    }

    // Sort the lists, so I can do a binarySearch
    first.Sort();
    second.Sort();

    // Copia only the new elements
    int index;
    for (int i = 0; i < second.Count; i++)
    {
        index = first.BinarySearch(second[i]);
        if (index < 0)
        {
            first.Insert(~index, second[i]);
        }
    }

    // Edit
    second = null;

    return true;
}
Run Code Online (Sandbox Code Playgroud)

我的一个朋友,审查我的代码,说我不应该创建'扩展List类'的方法,因为这违反了开放/封闭原则.如果我想扩展类List,我应该创建一个继承自List的新类,并在该新类中实现我的"merge"方法.他是对的吗?扩展List类违反了开放/封闭原则?

c# list open-closed-principle

6
推荐指数
1
解决办法
599
查看次数