在平常的应用中,我们经常会遇到处理多个属性相同的对象。此时我们可以定义成数组类。但在每个对象中,往往又包含多个相同性质的属性,此时我们再在该类中定义数组。注意,

自定大类Agent.cs代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
    
    
   class Agent
    {
     

       //定义类,并在类中定义数组。
        public class psom
        {
            public double[] sd = new double[10];//定义数组 10为自己定义的,可根据需求拟定
            public int x;
        }
        public psom[] pso = new psom[10]; //初始化psom类数组
        public Agent() //Agent类的构造函数,在构造函数中实例化数组类元素
        {
           //实例化数组类元素
            for (int i = 0; i <10; i++)
            {
               pso[i] = new psom();
            }
        }
        public void print()
        {
            for (int i = 0; i < 10; i++)
            {
                pso[i].sd[i] = i;
                Console.WriteLine(pso[i].sd[i]);
           }
        }
        
    }
}

在Program.cs文件中,调用向数组Agent类中写数据

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication3
{
    class Program
    {

 
        static void Main(string[] args)
        {
            Agent agent = new Agent();
            agent.print();
        }
    }
}