0

I'm kind of new with C#. I was just wondering if there's a way to run code stored on a string var, for example:

string var = (int num = num1 * num2).ToString();

I read in another question it's called something like macro sustitutions ("macro sustitución" in spanish), so I want to know if any of you knows a way to do this. Thanks!

joce
  • 9,624
  • 19
  • 56
  • 74
avatarbobo
  • 269
  • 2
  • 6
  • 14

2 Answers2

1

I'm not entirely sure why you would want to use the CSharpCodeProvider unless you are building an interactive CSharp compiling app. Anyways, check out these tutorials.

Compiling and Running code at runtime

CodeProject: Compiling and Executing Code at Runtime

Jon Gear
  • 1,008
  • 1
  • 11
  • 22
0

You could create a method which returns a string called MultipleNumbers. It would look something like this:

 public string MultipleNumbers(int firstNumber, int secondNumber)
    {
        string answer;
        return answer = (firstNumber * secondNumber).ToString();
    }

Your string would be:

string answer = MultipleNumbers(2, 4);

I would suggest not using var as a variable name since it is a reserved name. You can use var in place of string like this:

var answer = MultipleNumbers(2, 4);

I hope this helps!