C#的类,构造函数以及Array阵列的数据填充与绑定

今天学习,如标题。

创建一个类,字段属性构造函数:
C#的类,构造函数以及Array阵列的数据填充与绑定

Source code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Insus.NET.Models
{
public class Customer
{
public int CustomerID { get; set; } public string CustomerName { get; set; } public string PID { get; set; } public Customer()
{ } public Customer(int customerId, string customerName, string pID)
{
CustomerID = customerId;
CustomerName = customerName;
PID = pID;
}
}
}

接下来,我们需要准备Array数据集,方法有好几路,每个元素的对象,由于构造函有重载,因此写法也不一样:
方法一:
C#的类,构造函数以及Array阵列的数据填充与绑定

public Customer[] ArrayCustoners()
{
Customer c1 = new Customer();
c1.CustomerID = ;
c1.CustomerName = "张三";
c1.PID = ""; Customer c2 = new Customer(, "李四", "");
Customer c3 = new Customer() { CustomerID = , CustomerName = "吴广", PID = "" }; return new Customer[]
{
c1,c2,c3
};
}

Source Code

方法二:
C#的类,构造函数以及Array阵列的数据填充与绑定

 public Customer[] ArrayCustoners1 = {
new Customer(,"张三", ""),
new Customer(,"李四",""),
new Customer() { CustomerID = , CustomerName = "吴广", PID = "" }
};

Source Code

方法三:
C#的类,构造函数以及Array阵列的数据填充与绑定

public Customer[] ArrayCustoners2()
{
Customer c1 = new Customer();
c1.CustomerID = ;
c1.CustomerName = "张三";
c1.PID = ""; Customer[] array = new Customer[]; array[] = c1;
array[] = new Customer(, "李四", "");
array[] = new Customer() { CustomerID = , CustomerName = "吴广", PID = "" };
return array;
}

Source Code

方法四:
C#的类,构造函数以及Array阵列的数据填充与绑定

public Customer[] ArrayCustoners3()
{
Customer c1 = new Customer();
c1.CustomerID = ;
c1.CustomerName = "张三";
c1.PID = ""; return new Customer[]
{
c1,
new Customer(, "李四", ""),
new Customer() { CustomerID = , CustomerName = "吴广", PID = "" }
};
}

Source Code

也许还有其它写法,Insus.NET暂列出四种,根据上面来看,第二种是最简单的写法,不过在创建类别以及其构造方法,需要重载带参数的构造函数。

上一篇:WPF 之 布局(一)


下一篇:商城02——dubbo框架整合_商品列表查询实现_分页