List<Double> constants = new ArrayList<Double>() {{
add(1.4);
add(0.4);
add(1.2);
add(2.4);
add(4.2);
add(5);
add(6.0);
add(7.0);
}};
List<Double> constants = new ArrayList<Double>() {{ add(1.4); add(0.4); add(1.2); add(2.4); add(4.2); add(5); add(6.0); add(7.0);
Share
In C# 3.0 or greater,
constantsis implicitly typed toList<double>with thevarkeyword. The list is initialized (by putting the numbers in braces) using the collection-initializer syntax.This is equivalent to (C# 2.0 or greater):
You can leave out the
Ds, but I prefer to be explicit with numeric literals.On another note, if this really represented a list of unnamed constants, it would be good to use an immutable collection such as
ReadOnlyCollection<T>. For example: