Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.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#_List - Fatal编程技术网

C# 如何将对象从列表中从方法返回到方法?

C# 如何将对象从列表中从方法返回到方法?,c#,list,C#,List,我有一个存储来自不同帐户类的对象的列表。我有一个方法,允许提交帐户。用户输入帐户Id,Id和我的列表被传递给另一个方法,该方法搜索对象以查看该帐户是否存在。我希望它随后将帐户详细信息返回到提交方法 我不知道如何从搜索方法返回对象 我的列表看起来像列表库 我在C++中完成了这个任务,我刚开始C,发现很难适应它。任何帮助都将不胜感激 我的列表看起来像列表库 我在C++中完成了这个任务,我刚开始C,发现很难适应它。任何帮助都将不胜感激 编辑以包含我的代码,但这不起作用 public static vo

我有一个存储来自不同帐户类的对象的列表。我有一个方法,允许提交帐户。用户输入帐户Id,Id和我的列表被传递给另一个方法,该方法搜索对象以查看该帐户是否存在。我希望它随后将帐户详细信息返回到提交方法

我不知道如何从搜索方法返回对象

我的列表看起来像
列表库

我在C++中完成了这个任务,我刚开始C,发现很难适应它。任何帮助都将不胜感激

我的列表看起来像
列表库

我在C++中完成了这个任务,我刚开始C,发现很难适应它。任何帮助都将不胜感激

编辑以包含我的代码,但这不起作用

public static void processLodgement(List<CAccount> bank)
            {
                CAccount p;
                string ID;
                double amount = 0;

                Console.WriteLine("Process Lodgement\n");
                Console.WriteLine("Enter account ID\n");
                ID = Console.ReadLine();


                Console.WriteLine("Enter Lodgement Amount\n");
                double.TryParse(Console.ReadLine(), out amount);

                findAccount(bank, ID);






            }

            public static CAccount findAccount(List<CAccount>bank, string accountID)
            {
                for(int i=0; i < bank.Count(); i++)
                {

                    if (bank[i].GetID()==accountID)

                    {
                        return bank[i];

                    }



                }


            }  
公共静态无效流程提交(列表银行)
{
cacp计数;
字符串ID;
双倍金额=0;
Console.WriteLine(“进程提交”);
Console.WriteLine(“输入帐户ID\n”);
ID=Console.ReadLine();
Console.WriteLine(“输入提交金额”);
double.TryParse(Console.ReadLine(),out amount);
金融账户(银行,身份证);
}
公共静态CAccount findAccount(Listbank,字符串accountID)
{
对于(int i=0;i
您需要使用
id
和帐户列表作为参数,使用
帐户
作为返回类型

public Account FindAccount(int id, List<Account bank)
{
   // Look for account based on ID or do something, and return the account.
}
或者只使用一个循环:

foreach (var acc in bank)
{
     if (acc.ID == id)
         return acc;
}
// Handle account not found.
编辑:为了响应您发布的代码,它应该是这样的

public static CAccount findAccount(List<CAccount>bank, string accountID)
{
     for(int i=0; i < bank.Count; i++)
     {
         if (bank[i].GetID() == accountID)
         {
             return bank[i];
         }
     }
     throw new Exception("Account not found!");
 }  
publicstaticcaccountfindaccount(Listbank,stringaccountid)
{
对于(int i=0;i

在方法的末尾,您需要返回一些内容(比如null),或者抛出一个错误。另外,使用
Count
而不是
Count()
以获得更好的性能(
Count()
用于LINQ查询)

您已经有代码了吗?-显示它:-)-可能是这样的:公共帐户选择(列表银行){return bank[0];}
public ObjectType FuncName(int something,List){return List[0];}
Hi我在那里包括了我的代码:)你说“它不工作了”是什么意思,编译它时出错了还是没有按预期工作?谢谢你的帮助,开始了:)
public static CAccount findAccount(List<CAccount>bank, string accountID)
{
     for(int i=0; i < bank.Count; i++)
     {
         if (bank[i].GetID() == accountID)
         {
             return bank[i];
         }
     }
     throw new Exception("Account not found!");
 }