Some time ago, I've been working on a quite twisted project - I could only write the code in the single scope and it would be later put in a C# function (by another module).
I could only use namespaces which were declared before (I had no influence on them) and could only use variables from the scope I worked in. Because of that, I wasn't able to change the headers and included libraries.
The problem came when I wanted to operate on generic collections - I couldn't use neither lambda expressions, nor LINQ - I simply wasn't
able to put using System.Linq;
, as I had no access to the file header.
I had to do only a simple thing and it was easy to manage without LINQ or lambda. However, after that I was wondering what would happen, if I had to use some more complex operations on IEnumerable. From that, there comes my question:
Is it possible to use LINQ or lambdas without changing the file header and adding new namespaces?
Let's say that we have a List<int> _Numbers = new List<int>();
. Let it be filled with some numbers.
Now I want to select all the even numbers from it.
With using System.Linq;
in the header, the solution is obvious:
List<int> _NewList = _Numbers.Where(n => n % 2 == 0).ToList();
or
List<int> _NewList = (from _Number in _Numbers where _Number % 2 == 0 select _Number).ToList();
But without LINQ included, how can I achieve this? My first guess would be something like:
List<int> _NewList = System.Linq. // ??? What to add here?
I am aware that the problem is rather exotic, and it's very unusual to go this way, but I'm just curious and I've found no information about this case.