This code is invalid to start with:
struct particle
{
double[] position = new double[100];
}
You can't specify variable initializers for instance variables in structs.
However, accessing data within another object or value is easy - if it's accessible. In this case your fields are private and you haven't provided any access methods or properties, so you won't be able to get at them "from the outside" without more code.
Here's some modified code:
public class Particle
{
private readonly double[] positions = new double[100];
// TODO: Rename these to something useful
public double Gbest { get; private set; }
private double Lbest;
private double Pconst = 0.5;
public Particle(int g)
{
Gbest = g; // Or whatever
}
}
List<Particle> swarm = new List<Particle>();
for (int i = 0; i < swarmSize; i++)
{
swarm.Add(new Particle(i));
}
double total = 0;
foreach (Particle particle in swarm)
{
total += particle.Gbest;
}
Now this isn't doing anything particularly useful, because it's not clear what you're trying to do - but I would suggest you get an introductory book on C#. Stack Overflow is great for specific questions, but I think you're early enough on your journey into C# that a good book or tutorial would help you more.
ParticleYou have an array ofobject.