码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2016-04-27 14:14:40      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:

今天学习,如标题。

创建一个类,字段属性构造函数:
技术分享


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暂列出四种,根据上面来看,第二种是最简单的写法,不过在创建类别以及其构造方法,需要重载带参数的构造函数。


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

标签:

原文地址:http://www.cnblogs.com/insus/p/5438676.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!