Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C语言中的方法链接#_C# - Fatal编程技术网

C# C语言中的方法链接#

C# C语言中的方法链接#,c#,C#,我实际上不知道这在C#中叫什么。 但我想向类中添加函数性,以便同时添加多个项 myObj.AddItem(mItem).AddItem(mItem2).AddItem(mItem3); 使用以下技巧: public class MyClass { private List<MyItem> _Items = new List<MyItem> (); public MyClass AddItem (MyItem item) { //

我实际上不知道这在C#中叫什么。 但我想向类中添加函数性,以便同时添加多个项

myObj.AddItem(mItem).AddItem(mItem2).AddItem(mItem3);
使用以下技巧:

public class MyClass
{
    private List<MyItem> _Items = new List<MyItem> ();

    public MyClass AddItem (MyItem item)
    {
        // Add the object
        if (item != null)
            _Items.Add (item)

        return this;
    }
}
公共类MyClass
{
私有列表_Items=新列表();
公共MyClass附加项(MyItem)
{
//添加对象
如果(项!=null)
_项目。添加(项目)
归还这个;
}
}

它返回当前实例,该实例将允许您链接方法调用(从而“同时”添加多个对象)。

如果您的项用作列表,您可能需要实现类似iList或iEnumerable/iEnumerable的接口

无论如何,像您希望的那样链接调用的关键是返回您想要的对象

public Class Foo
{
   public Foo AddItem(Foo object)
   {
        //Add object to your collection internally
        return this;
   }
}
像这样的

class MyCollection
{
    public MyCollection AddItem(Object item)
    {
        // do stuff
        return this;
    }
}
怎么样

AddItem(ICollection<Item> items);
你可以这样使用它们

myObj.AddItem(new Item[] { item1, item2, item3 });
myObj.AddItem(item1, item2, item3);

这不是方法链接,而是在一次调用中向对象添加多个项。

您提到的技术称为可链接方法。它通常在创建DSL或C#中使用

典型的模式是让AddItem()方法返回它所属的类(或接口)的实例。这允许后续调用链接到它

public MyCollection AddItem( MyItem item )
{
   // internal logic...

   return this;
}
用于向集合添加项的方法链接的一些替代方案包括:

使用
params
语法允许将多个项作为数组传递给方法。当您要隐藏数组创建并为方法提供变量参数语法时,此功能非常有用:

public void AddItems( params MyItem[] items )
{
    foreach( var item in items )
        m_innerCollection.Add( item );
}

// can be called with any number of arguments...
coll.AddItems( first, second, third );
coll.AddItems( first, second, third, fourth, fifth );
提供IEnumerable或IEnumerable类型的重载,以便将多个项一起传递给集合类

public void AddItems( IEnumerable<MyClass> items )
{
    foreach( var item in items )
         m_innerCollection.Add( item );
}
“实际上,我不知道这在c#中被称为什么。”

流畅的API;
StringBuilder
是最常见的.NET示例:

var sb = new StringBuilder();
string s = sb.Append("this").Append(' ').Append("is a ").Append("silly way to")
     .AppendLine("append strings").ToString();

其他人的回答是直接方法链接,但如果您使用的是C#3.0,您可能会对集合初始值设定项感兴趣……它们仅在您进行构造函数调用时可用,并且仅当您的方法具有适当的
Add
方法和实现
IEnumerable
时可用,但您可以执行以下操作:

MyClass myClass = new MyClass { item1, item2, item3 };
你为什么不用这个关键词呢


如果您的类继承自ICollection,则可以添加扩展方法来支持此操作:

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void CanChainStrings()
    {
        ICollection<string> strings = new List<string>();

        strings.AddItem("Another").AddItem("String");

        Assert.AreEqual(2, strings.Count);
    }
}
[TestClass]
公共类UnitTest1
{
[测试方法]
公共无效CanChainStrings()
{
ICollection strings=新列表();
strings.AddItem(“其他”).AddItem(“字符串”);
arest.AreEqual(2,strings.Count);
}
}
公共静态类ChainAdd
{
公共静态ICollection AddItem(此ICollection集合,T项)
{
集合。添加(项目);
回收;
}
}

不需要将参数限制为将项添加到MyClass。可能是Object、Item或其他东西。+1学到了新东西:D我认为我必须为更改方法创建一个不同的方法。方法提供了一个很好的示例如何实现这一点
MyClass myClass = new MyClass { item1, item2, item3 };
public void AddItem (params MyClass[] object)
{
    // Add the multiple items
}
[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void CanChainStrings()
    {
        ICollection<string> strings = new List<string>();

        strings.AddItem("Another").AddItem("String");

        Assert.AreEqual(2, strings.Count);
    }
}
public static class ChainAdd
{
    public static ICollection<T> AddItem<T>(this ICollection<T> collection, T item)
    {
        collection.Add(item);
        return collection;
    }
}