今天學習,如標題。創建一個類,欄位屬性構造函數:Source code: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Task ...
今天學習,如標題。
創建一個類,欄位屬性構造函數:
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; } } }View Code
接下來,我們需要準備Array數據集,方法有好幾路,每個元素的對象,由於構造函有重載,因此寫法也不一樣:
方法一:
public Customer[] ArrayCustoners() { Customer c1 = new Customer(); c1.CustomerID = 9; c1.CustomerName = "張三"; c1.PID = "123456789012"; Customer c2 = new Customer(10, "李四", "321245677812"); Customer c3 = new Customer() { CustomerID = 30, CustomerName = "吳廣", PID = "213445678912" }; return new Customer[] { c1,c2,c3 }; }Source Code
public Customer[] ArrayCustoners1 = { new Customer(9,"張三", "123456789012"), new Customer(10,"李四","321245677812"), new Customer() { CustomerID = 30, CustomerName = "吳廣", PID = "213445678912" } };Source Code
public Customer[] ArrayCustoners2() { Customer c1 = new Customer(); c1.CustomerID = 9; c1.CustomerName = "張三"; c1.PID = "123456789012"; Customer[] array = new Customer[3]; array[0] = c1; array[1] = new Customer(10, "李四", "321245677812"); array[2] = new Customer() { CustomerID = 30, CustomerName = "吳廣", PID = "213445678912" }; return array; }Source Code
public Customer[] ArrayCustoners3() { Customer c1 = new Customer(); c1.CustomerID = 9; c1.CustomerName = "張三"; c1.PID = "123456789012"; return new Customer[] { c1, new Customer(10, "李四", "321245677812"), new Customer() { CustomerID = 30, CustomerName = "吳廣", PID = "213445678912" } }; }Source Code
也許還有其它寫法,Insus.NET暫列出四種,根據上面來看,第二種是最簡單的寫法,不過在創建類別以及其構造方法,需要重載帶參數的構造函數。