2

Let's say I have this class with a constructor that fills the internal list with two entries:

class MyClass
{
    IList<int> someList;

    public MyClass()
    {
        someList = new List<int>();
        someList.Add(2);
        someList.Add(4);

        ... // do some other stuff
    }
}

Now let's say I have several constructors which all do the same with the internal list (but differ in other aspects).

I would like to know if I can outsource the generation and filling of the list directly to the field, like this:

class MyClass
{
    IList<int> someList = new List<int>(); someList.Add(2); someList.Add(4);
    // Does not compile.

    public MyClass()
    {
        ... // do some other stuff
    }
}

Is it possible to call several commands in the field definition, and if yes, how?

Kjara
  • 2,504
  • 15
  • 42

2 Answers2

1

You can pre-instantiated IList like this and add your values per accessing the Indexer:

IList<int> someList = new List<int>() { 2, 4 };

This will be initialization happens before the constructor is used.


Update 1

As OP mentioned in the comments, for LinkedList<T>() you have to use the constructor with some IEnumarable (in my Example an Array).

LinkedList<int> myList1 = new LinkedList<int>(new int[] {2,3,4});

Update 2

After reading your last comment, you're looking for Fluent Interfaces in your instantiation process. This is a method of chaining functions together and would look something like this:

Customer c1 = new Customer()  
                  .FirstName("matt")
                  .LastName("lastname")
                  .Sex("male")
                  .Address("austria");

This functionality is not given by default in Collection Classes.You have to implement your own version of IList<T> for this.

Lambda Expression is a way to achieve this, like your update shows...

  • 1
    No, I don't want to fill the list per constructor. I want to fill it before the constructor is called. – Kjara Aug 03 '16 at 12:00
  • Unfortunately it does not work for `LinkedList`... Any idea on how to do it in general (not just `List`)? – Kjara Aug 03 '16 at 12:05
  • @Kjara `LinkedList` helps you out with the constructor. :) You have to look where you can place the Indexers. Normally this works for every `IList` Child. – Smartis has left SO again Aug 03 '16 at 12:13
  • You mean `LinkedList(IEnumerable)`? This needs a collection as input. I see that it is a workaround, but it takes unnecessarry memory. I could do this with all collections `K`: `ICollection myCollection = new K(new T[] { // put my elements here })`, but I'm not very happy with it. – Kjara Aug 03 '16 at 12:18
  • @Kjara I'm not sure what you want to achieve. The `LinkedList` is a special kind of collection, which needs special handling. `IList` instead is a simple thing and can be per Indexer filled. – Smartis has left SO again Aug 03 '16 at 12:25
  • I am most interested in a general way to queue several commands in the field definition. I mean, it could be that instead of filling a collection, I wanted to print stuff on the Console and then set a value to 42... I don't care about the usefulness of this example, I just want to know if C# allows this (and if yes, how). – Kjara Aug 03 '16 at 12:29
1

Got it:

IList<int> someList = new Func<List<int>>(() => { IList<int> l = new List<int>(); l.Add(2); l.Add(4); return l; })();

Explanation:

() => { IList<int> l = new List<int>(); l.Add(2); l.Add(4); return l; }

is a function taking no argument and returning an IList<int>, so it is a Func<IList<int>>.

Althoug the compiler knows this, it seems I explicitly have to state that fact via

new Func<IList<int>>(...)

to be able to call it later. The call is done as usual by putting two brackets () behind the Func.

Or to write it in a more readable way (then I don't even need the new keyword, but instead must make the Func static):

static Func<IList<int>> foo = () => { IList<int> l = new List<int>(); l.Add(2); l.Add(4); return l; };

IList<int> someList = foo();
Kjara
  • 2,504
  • 15
  • 42