Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 2 years ago.
Improve this question
Is there an attribute or capability in C# to automatically memoize static function results, based on input parameters, without writing all the logic for it myself?
For example in Python you can depend on functools:
import functools
#functools.lru_cache()
def my_function(param1):
...
Is there some similar package or built-in for C#?
[Memoizable]
public static MyFunction(MyType param1)
{
...
Results should be cached to avoid re-calculating for the same inputs.
Nope, does not exist. There are external tools that do so, but there is nothing automatic in C# for this. You will have to program it, if you need it, or use some external tool that does code rewrite.
public class SampleClass
{
private readonly IDictionary<int,int> _cache =
new Dictionary<int, int>();
public int Calculate(int input)
{
if (!_cache.ContainsKey(input))
{
int result = input*2;
_cache.Add(input,result);
}
return _cache[input];
}
}
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
I want to run python script from C#(Visual Studio). I don't want to use C# process to do it, because I must have access to all of the python's modules, classes, and methods (I'd like to treat it as python object). I'm looking for sth like this:
"Python code"
"PyModule"
class PyClass:
def method:
print("Hello world!")
C# code
using PyModule.PyClass
PyClass.method()
I found python.net http://pythonnet.github.io/ , but they say that their solution is unverifiable.
I have to write it in Python 3, so IronPython isn't solution for me.
Do you know any solution that is similar to this?
Well, do have a look into IronPython as suggested in the comments. You can write your python script, save it to, say, greet.py
def greet(name):
return 'Hello ' + name + '!'
Now from C# you can do:
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
static void RunPythonScript()
{
var engine = Python.CreateEngine();
dynamic scope = engine.CreateScope();
engine.ExecuteFile("greeting.py", scope);
var greeting = scope.greet("John");
Console.WriteLine(greeting);
}
Result as you might expect is Hello John!
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
From a C# program, I want to examine a given python file for the functions it offers. If possible, I also want to get metainformation about each function and parameter, like a description.
I can control both the C# side and the Python side.
How can I achieve that?
(As for the use case: I am going to write a dispatcher function in Python that the C# program can call inside the python script to execute a specific function, given the required arguments.)
Never tried this. But think you could use a combination of the module optparse (Taken from How do I get list of methods in a Python class?):
from optparse import OptionParser
import inspect
inspect.getmembers(OptionParser, predicate=inspect.ismethod)
With function.__doc__ to get the documentation from the method like
def factorial(x):
'''int f(int x); returns the factorial of an integer number'''
#code here
>>> factorial.__doc__
'int f(int x); returns the factorial of an integer number'
Use the first part (int f(int x);) as the description of what parameters it needs and returns (You could take this part just taking the string until first semicolon)
And use getattr to call the function (https://docs.python.org/2/library/functions.html#getattr):
getattr(myObject, 'factorial', '3')()
Hope this helps you
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I want to know if a string has any non-whitespace characters in it -- so if it's not null or just full of spaces or tabs. I'm tired of doing this:
if(!String.IsNullOrWhitespace(something))
There is nothing wrong with this, it's just verbose.
This works (since a string is simply an array of Chars)...
something.Any()
...but it breaks if the variable is NULL (and it wouldn't account for whitespace).
I know I can write an extension method for this, but I feel like there should be something in the core C# library that I'm just missing.
You're not missing anything. My recommendation is an extension method:
public static class StringExtensions
{
public static bool HasValue(this string value)
{
return !string.IsNullOrEmpty(value);
}
}
Usage:
if (myString.HasValue()) ...
Why not use String.IsNullOrEmpty? https://msdn.microsoft.com/en-us/library/system.string.isnullorempty(v=vs.110).aspx
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
I have a C# codebase which has evolved over the past year, and I'd like to get some visibility on the number of interface type references vs class type references. Suppose the code is:
namespace Infrastructure {
public class Foo {
private ConcreteClass _field1;
private OtherConcreteClass _field2;
private IInterface _field3;
public void SomeMethod(ConcreteClass parameter1, IInterface parameter2) {
_field1 = parameter1;
_field3 = parameter2;
OtherConcreteClass newVariable = new OtherConcreteClass();
}
}
}
Does anyone know of a tool that would report that there are 2 declared interface references and 4 declared class references in this snippet? I've looked at R#, SourceMonitor, CLOC, and a few others, with no success so far.
The best code metric tool I used so for is nDepend (http://www.ndepend.com/) but I'm not sure it will give you the information you want (but it'wll give you many more ...)
I hope this help
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
All in the Title.
Also please give an answer in less than a week.
I need someone to really help me with this, I'm using an online course and it says I have to Write the NumberBoard method, then write a NumberBoard constructer and call the method.
Here's the link to the .zip file that has the PDF telling me what to do, and the project that they give you:
https://d396qusza40orc.cloudfront.net/gameprogramming%2Frequired_assessment_materials%2FRequiredProjectMaterials.zip
(Copy and paste if link not clickable)
To get to the project, open the code folder and click the "GameProject" thing
Something like this will work
public class SomeClass
{
public SomeClass() //constructor
{
SomeMethod(); //Calling the method in constructor
}
public void SomeMethod() // Method
{
//Method implementation
}
}