C# Eval - Execute Method - The return type

CSharp · Console

/Program.cs

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

// @nuget: Z.Expressions.Eval

/*
### The return type

When you use the `Execute` method, you have 2 choices that will affect the return type. You can specify:

1. **A TResult**: `Eval.Execute<TResult>(code)`
2. **Nothing**: `Eval.Execute(code)`

1 - If you use the generic `Execute` method and specify a `TResult` type (`Eval.Execute<TResult>(code)`), the method will return a `TResult`

2 - If you don't specify a return type (`Eval.Execute(code)`), the method will return an `object`. Under the hood, the `Eval.Execute<object>` method is called.

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

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

public class Program
{
	public static void Main()
	{
		// 1. **A `TResult`**: `context.Execute<TResult>(code)`
		{
			List<int> list1 = Eval.Execute<List<int>>("new List<int>() { 1, 2, 3 };");
		
			FiddleHelper.WriteTable(list1);
		}

		
		// 2. **Nothing**: `context.Execute(code)`
		{
			object object2 = Eval.Execute("new List<int>() { 1, 2, 3 };");
			List<int> list2 = (List<int>)object2;
			
			FiddleHelper.WriteTable(list2);
		}
	}
}

;