I was thinking about arrays and lists and wondering if and how classes can get an implementation to be initializable like them. Let's take this class as basis:
class TestClass
{
private List<int> Numbers = new List<int> ();
// Insert code here
}
What I would like to be able to do is to take the given values and internally fill my list.
TestClass test = new TestClass () { 2, 4, 7, 10 };
What would be normally possible is this:
List<int> test = new List<int> () { 2, 4, 7, 10 };
But I would like to have it for my own custom classes. The next question, which is optional, is if the same can be done for this syntax:
TestClass test = { 2, 4, 7, 10 };
I assume that is less likely to be possible. Note, these are different from this:
Cat cat = new Cat() { Name = "Sylvester", Age=8 };
These are direct and optional declarations of internal fields and properties.
List<int>
instead – Marcimarcia