15,967,050 members
1.00/5 (1 vote)
See more:
EX:

C#
```class A
{
private List<double> u = new List<double>();

privated void fun1(double x, double y)
{
for (int i=0;i<100;i++)
{
}
for (int i=0;i<100;i++)
{
u[i] = x + y*i;
}
}
public void fun2()
{
for (int i=0;i<100;i++)
{
fun1(i,i);
}
}

}```

As shown above. I want to write a code, defined a List:
"private List<double> u = new List<double>()"
It will be used in the function fun1
and when called the fun1 100 times, the count of list u will become large,
But what I wanted is u.Count = 100 all the time;
so I mean like that below :

```////////////////////////////
///////////if I can define it like that below, if not, how can I achieve it?
public List<double> u = new List<double>()
{
for(int i=0;i<100;i++)
{
}
}

//////////////////////
privated void fun1(double x, double y)
{
for (int i=0;i<100;i++)
{
u[i] = x + y*i;
}
}
public void fun2()
{
for (int i=0;i<100;i++)
{
fun1(i,i);
}
}

}```
Posted
Sergey Alexandrovich Kryukov 9-May-14 0:58am
Why? Why having 100 elements all the time? It makes no sense at all. List is not an array.
—SA

## Solution 1

Initialize with zero like below
C#
```List<double> u =
Enumerable.Range(0, 100).Select(x => new double()).ToList();```

Or initialize with values
C#
```List<double> u =
Enumerable.Range(0, 100).Select(x => (double)(x+x*x)).ToList();```

v3
King Fisher 9-May-14 2:17am
nice. ;)
can you show me Some Link Related to this.

## Solution 2

There's no need to initialize the List<double> the way you are doing it now: just add the values to it directly as they are computed. In this example the values are computed in the Class constructor:
C#
```public class A
{
private readonly List<double> privateList = new List<double>();

// constructor
public A(double x, double y)
{
for (int i = 0; i < 100; i++)
{
}

publicList = privateList;
}

// public Property that exposes the private inner list
public List<double> publicList { private set; get; }
}```
Example of use:
C#
```A instanceOfA;

private void Form1_Load(object sender, EventArgs e)
{
instanceOfA = new A(2, 4);
}```
Or, you can leave out the whole business of exposing the inner list via a public Property, and just make the inner, read-only, list public. Like this:
C#
```public class A
{
public readonly List<double> AList = new List<double>();

public A(double x, double y)
{
for (int i = 0; i < 100; i++)
{
}
}
}```

v3

## Solution 3

Please see my comment to the question. It makes no practical sense at all. If you need something like that, use array of 100 elements. It looks like you want to kill all the benefits of list over the arrays. But why? You can use arrays when you need them.

—SA

Zhang, G. 9-May-14 2:17am
Because the u.Count is not a fixed value.
in fact, I want to define:
private int nx;
public int Nx{get...set...}
private List<double> u=new List<double> (Nx)
but it is not work.
Sergey Alexandrovich Kryukov 9-May-14 10:05am
It does not work because it should not work. You simply never need it. This is the first think you have to understand.
If you want to continue this discussion, you have to explain your ultimate goal. Why? You cannot answer "because" I want N members on the list, you have to answer, why do you want them, and so on, until we understand the ultimate goal. Then we can help you with the solution.
—SA