public struct bank_account
{
public string name;
public string credit_card_number;
}
private List<bank_account> all_accounts = new List<bank_account>();
public bank_account getAccount(string card_number)
{
bank_account holder; // this pointer is the wrong way to do it but im used to c++
foreach(bank_account acc in all_accounts)
{
if (card_number == acc.credit_card_number)
holder = acc;
}
return holder;
}
Im new to c#. Use to c++. Normally in c++ the holder variable would be a pointer. and all i want it to do is point to the correct bank_account structure so i could return it so another function could access the structs variables. How is this done in c#?
if (...) return holder;instead of parsing all the list, assuming there is one and only one result.holderwill be unassigned. An unassigned variable might be fine in C++, but not ok in C#.bank_account holder = null;and you will be fine. So after the foreach if nothing found, null is returned. And you can eliminate the variable using only two returns.