2
 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#?

GioPoe
  • 109
  • 3
  • 12
  • 2
    Use classes instead of structs. Classes and structs work differently in C# than in C++ – FCin Sep 26 '19 at 07:18
  • Indeed, use a class if you want a reference, else you have a copy of the struct. And you can improve performance by writing: `if (...) return holder;` instead of parsing all the list, assuming there is one and only one result. –  Sep 26 '19 at 07:19
  • What if there is no account with that account number? `holder` will be unassigned. An unassigned variable might be fine in C++, but not ok in C#. – Sweeper Sep 26 '19 at 07:20
  • Write: `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. –  Sep 26 '19 at 07:21
  • @OlivierRogier a value type can't be null though. – Sweeper Sep 26 '19 at 07:22
  • So, use a class, because you ask for a reference or return a empty default struct with defaults values (empty strings here or anything else). –  Sep 26 '19 at 07:22
  • As other stated Struct are not the same in C# and C++, but it's easier to understand once you look at the difference between Class and Struct in C#. It's easier to compare to C++ https://stackoverflow.com/questions/13049/whats-the-difference-between-struct-and-class-in-net – xdtTransform Sep 26 '19 at 07:36

2 Answers2

2

Your code is basically fine, take a look at LINQ which will make things a lot easier to read.

public bank_account getAccount(string card_number)
{
    return allAccounts.FirstOrDefault(x => x.CreditCardNumber == cardNumber);
}

And as others have mentioned: dont use struct for this, use a class. If you use a struct you will always return a struct with empty strings in it if the credit card number was not found. If your credit card number was found, you will returrn a copy of the original struct, not the struct itself.

If you use a class you will return eiter a reference to the original object or null if the number was not found. Check out C#/C++ struct differences, they really do behave different.

See the following sample to get an idea how it could look like:

public class BankAccount
{
    // don't publish your private fields, use properties for public access
    public string Name { get; set; }
    public string CreditCardNumber { get; set; }
}

public class BankAccounts
{
    private List<BankAccount> allAccounts = new List<BankAccount>();

    public BankAccount GetAccount(string cardNumber)
    {
        return allAccounts.FirstOrDefault(x => x.CreditCardNumber == cardNumber);
    }
}
Michael Sander
  • 2,677
  • 23
  • 29
  • This solution returns a struct with null strings if not found. –  Sep 26 '19 at 07:25
  • Remind that with struct, in C#, the returned value is a COPY of the eventual item found. It is not the item itself. Use a class to have a reference (i.e. an hidden pointer). –  Sep 26 '19 at 07:33
0

Let me make it easy, use the var

foreach(var acc in all_accounts) { 
   if (card_number == acc.credit_card_number)
      return acc; 
}
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321