可变数组 [英] Variable Array

查看:73
本文介绍了可变数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个混合数据的数组或集合-例如多个团体和人员的姓名,地址和电话号码的列表.  我是C#的新手,只能够找到我将描述为1维的信息 数组或列表.  例如,如果我想在C#中使用以下类似Excel的表格-如何配置代码?

I am wanting to create an Array or Collection of mixed data - like a list of names, addresses and phone numbers for multiple groups and people.  I am new to C# and have only been able to locate information for what I would describe as a 1 dimensional array or list.  For example if I want to have the following excel-like table in C# - how do I configure the code?

姓名 地址 电话号码

Name   Address   Phone Number

Joe 123随处可见502/123-4567

Joe 123 anywhere 502/123-4567

吉尔235某个地方418/229-4750

Jill 235 someplace 418/229-4750

预先感谢

肯塔基州的肯

推荐答案

您需要以一种更有条理的方式思考,而不是以类似于Excel的方式容纳任何东西的细胞方式!

You need to think in a more structured way rather than in an "Excel-like bunch of cells holding anything" way!

您所描述的是个人信息的数组/集合,其中每个块包含该人的姓名,地址,电话号码等.

What you are describing is an array/collection of person information, where each block contains the person's name, address, phone number and so on.

因此,首先需要描述您的人,然后可以为其定义结构或类.

So first you need to describe your person, for which you can define a struct or class.

例如

public class Person
{
   public string Name {get;set;}
   public string Address {get;set;}
   public string PhoneNumber {get;set;}
}


现在您可以拥有一个Person对象数组.例如

Now you could have an array of Person objects. E.g.

Person[] persons = new Person()
{
   new Person {Name="Bob"},
   new Person {Name="Carol", Address="somewhere"}
}

请注意,这不一定是最好的方法-一切都取决于您从何处获取数据,您要如何处理数据等等.例如,您可能想定义一个地址类,该类首先包含FirstAddressLine,SecondAddressLine,Town,County ETC;然后定义您的Person类以容纳一个Address对象,而不是一个简单的地址字符串.

Note that this is not necessarily the best way - that all depends on where you get your data from, what you want to do with it and so on. For example, you may want to define an Address class first holding a FirstAddressLine, SecondAddressLine, Town, County etc; and then define your Person class to hold an Address object rather than a simple string for the address.



这篇关于可变数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆