简体   繁体   中英

Working with arrays in c#

My question should be simple, im working on a program that has an array contains , ID,name,Money. So i separated the arrays to be easier to work with (search, add etc...) But i feel there is a more effect way of doing this, cause if i search im only searching the ids and have to put the array back together each time i find a match. Is there a better way of doing this? thanks for your help. Here is some of my code

  // Variables for reading Accounts
   static List<int> importedNumbers = new List<int>();
   static List<string> people = new List<string>();
   static List<decimal> Money = new List<decimal>();
   static  List<string> BorP = new List<string>();

    // Variables for reading Transactions
   static List<int> NumbersTrans = new List<int>();
    static List<decimal> Amount = new List<decimal>();

    static void Main(string[] args)
    {
        string path = @"path";
        ReadTransactions(@"path");       
        ReadAccount(path);       
        Duplicates(); 
        Charges(path);

        Console.WriteLine("Type in ID to search ");
       string id = Console.ReadLine();
       int ID = int.Parse(id);
        Search(importedNumbers, ID);
        Console.ReadLine();           
    }

this is my decaration part of the program, is there a better way so i don't have so many arrays

Firstly - these are lists, they're not arrays. If they were arrays, they would be things like string[] and int[] .

However, you definitely shouldn't have all these separate lists. Instead, work out what the relevant types are - it's not really clear from your existing names, but you probably want something like:

  • Account (with an ID, an accountholder name and a balance)
  • Transaction (with possibly the source account, the target account, and the amount)

then you can have a List<Account> and a List<Transaction> . Basically, one collection for each logical collection of items. You might want a `Dictionary so that you can look up accounts by ID easily... but I'd at least just start with a list.

Any time you find yourself with two collections which should always have the same number of items in, such that x[0] and y[0] are related, you should at least consider whether you might be better off with a single collection of a new type. See my blog post on this topic for a longer example.

Next, learn about LINQ - it makes life much easier when you're querying data.

You should declare a class to hold your data, and use a list of objects of that class.

class MyClass // (pick a more appropriate name, I don't know what kind of data you're handling)
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Money { get; set; }
    public string BorP { get; set; }
}

...

List<MyClass> items = new List<MyClass>();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM