I would like to be able to mark a function somehow (attributes maybe?) so that when it’s called from anywhere, some other code gets to process the parameters and return a value instead of the called function, or can let the function execute normally.
I would use it for easy caching.
For example, if I had a function called Add10 and it would look like this:
int Add10 (int n)
{
return n + 10;
}
If the function go called repeatedly with the same value (Add10(7)) it would always give the same result (17) so it makes no sense to recalculate every time. Naturally, I wouldn’t do it with functions as simple as this but I’m sure you can understand what I mean.
Does C# provide any way of doing what I want?
I need a way to mark a function as cached so that when someone does Add10(16) some code somewhere is ran first to check in a dictionary is we already know the Add10 value of 16 and return it if we do, calculate, store and return if we don’t.
You want to memoize the function. Here’s one way:
http://blogs.msdn.com/b/wesdyer/archive/2007/01/26/function-memoization.aspx