C# Eval - Execute Method - FizzBuzz Interview Question

CSharp · Console

/Program.cs

// C# Eval Expression
// Doc: https://eval-expression.net/eval-execute

// @nuget: Z.Expressions.Eval

/*
# How to use the Execute Method?

The `Execute` method allows you to execute any C# code or expression at runtime.

It could be from a very simple math expression from an user input: https://dotnetfiddle.net/Jf9LMp

To a more complex C# code, such as solving the famous [FizzBuzz](https://en.wikipedia.org/wiki/Fizz_buzz) interview question:  https://dotnetfiddle.net/BBJ4Ut

In short, the `Execute` method from the C# Eval Expression library let you execute any C# code at runtime that you usually write in Visual Studio.

See more: https://eval-expression.net/eval-execute
*/

using System;
using System.Collections.Generic;
using Z.Expressions;

public class Program
{
	public static void Main()
	{
		var list = Eval.Execute<List<string>>(@"

var answers = new List<string>();

for (int i = 1; i <= 100; i++)
{
    if (i % 15 == 0)
        answers.Add($'{i} => FizzBuzz');
    else if (i % 3 == 0)
        answers.Add($'{i} => Fizz');
    else if (i % 5 == 0)
        answers.Add($'{i} => Buzz');
    else
        answers.Add($'{i} => {i}');
}

return answers;

		");

		Console.WriteLine(string.Join(Environment.NewLine, list));

	}
}

;