[Proposal]: to add `Process` method, for handling objects like in linq style.
Proposal to add Process method, for handling objects like in linq style.
int Inc(int x) => x++;
int Dec(int x) => x--;
void Main()
{
var list = new List<int> { 1, 2, 3 };
var first = list.FirstOrDefault(x => x == 1)?.Process(x => Inc(x));
var second = list.FirstOrDefault(x => x == 2)?.Process(x => Dec(x));
}
P.S. name for the Process method can be any other.
Uh, neither of those two methods actually produce a visible change in external data.
Since you wouldn't need Process() for instance methods, why not define Inc()/Dec() as extension methods? So like
int Inc(this int x) => x + 1;
... Which wouldn't require the extra method either.
why not define
Inc()/Dec()as extension methods?
Yes it is possible to define those methods as extensions, but only when you want/can to do that, those methods can be too complicated for making them as extensions, they can be placed in another classes that were designed not to be static or they can be just a part of another lib, which you can't change.
Adding a method like you propose would cause it to show up on every single type. That is generally something we strongly avoid, especially when the value is limited.
I appreciate your suggestion, but I do not see us adding a method like this to .NET.
Not necessary to add this method to .net type, it is possible just to add some construction that will allow execute methods in linq style, like:
object func1(object o){}
object func2(object o){}
var object = (new object())->func1(x)->func2(x);
will be understanded by compiler like:
var object = func2(func1(new object()));
Not necessary to add this method to .net type, it is possible just to add some construction that will allow execute methods in linq style, like:
That's what I mean by showing up on every single type. If you added an extension method like:
public static U Process<T,U>(this T source, Func<T, U> func) => func(source);
then you could write what you're asking for, e.g.
new object().Process(func1).Process(func2);
and the compiler will transform that into:
Process(Process(new object(), func1), func2);
but Process would show up in IntelliSense for every type. You're of course able to add such an extension method to your own project, or to your own library (which you could publish to NuGet for others to use if you believe it's valuable), we just won't add it to .NET proper.