[英]How to call a method inside another method's object (list)? C#
我試圖弄清楚這是怎么可能的(如果有的話)。 代碼看起來基本上是這樣的:
public class MyClass
{
public Method1()
{
List<string> mylist = new List<string>();
mylist. /*how can I call Method2?*/
}
public Method2()
{
/*automation code for changing sequence of the UI objects
(action)*/
}
}
我可以使用內置的方法,如Sort()
, Add()
所有它們之后
using namespace System;
using namespace System.Collections;
using namespace System.Collections.Generic;
等等,但我想知道是否必須將 Method2 Method2()
放入另一個 class 中,將其作為命名空間導入,然后在Method1()
中的 object mylist
中使用它。
您會以相反的方式進行:與其嘗試為 object 提供新方法,不如為該方法提供 object:
public class MyClass
{
public void Method1()
{
List<string> mylist = new List<string>();
Method2(mylist); /*how can I call Method2?*/
}
public void Method2(List<string> list)
{
//does something with list
}
}
從技術上講,還有擴展方法的可能性。 但是你真的很想了解微軟為什么要制作擴展方法以及它們有什么用處。 您不只是垃圾郵件擴展方法,只是因為它在技術上是可行的。
相信我,當你輸入mylist.
您真的只想查看 Microsoft 方法,因為 Microsoft 提供了List<T>
class。 您不想看到每個人都認為可能有用的數千種方法。
public class MyClass
{
public void Method1()
{
List<string> mylist = new List<string>();
/*how can I call Method2?*/
Method2(mylist);
}
public void Method2(List<string> mylist)
{
//does something with my list
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.