[英]Working with arrays in c#
My question should be simple, im working on a program that has an array contains , ID,name,Money. 我的问题应该很简单,我正在处理包含ID,名称,货币数组的程序。 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. 所以我分离了数组以使其更易于使用(搜索,添加等...),但是我觉得这样做有更有效的方法,因为如果我仅搜索id并直接将数组放回去每次我找到一个匹配项。 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[]
. 如果它们是数组,它们将是string[]
和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) Account
(具有ID, Account
持有人姓名和余额) Transaction
(with possibly the source account, the target account, and the amount) Transaction
(可能包含源帐户,目标帐户和金额) then you can have a List<Account>
and a List<Transaction>
. 那么您可以拥有一个List<Account>
和一个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. 您可能需要一个字典,以便您可以轻松地通过ID查找帐户...但是我至少要从列表开始。
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. 每当您发现两个集合中始终具有相同项目数(例如x[0]
和y[0]
相关)时,您至少应考虑使用一个单个集合可能会更好新型。 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. 接下来,了解LINQ-在查询数据时,它使工作变得更加轻松。
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>();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.