Passing state to Func <bool> Tuple <string, string, Func <bool>>

I am trying to create a list of tuples with properties and conditions for checking them. Therefore, I had in mind this idea:

List<Tuple<string, string, Func<bool>>> properties = new List<Tuple<string, string, Func<bool>>> { Tuple.Create(FirstName, "User first name is required", ???), }; ... 

How to pass an expression like (FirstName == null) as Func?

+6
source share
2 answers

Like this (using lambda expression):

 var properties = new List<Tuple<string, string, Func<bool>>> { Tuple.Create<string, string, Func<bool>>( FirstName, "User first name is required", () => FirstName == null), }; 
+9
source

Something like that:

 List<Tuple<string, string, Func<bool>>> properties = new List<Tuple<string, string, Func<bool>>> { Tuple.Create(FirstName, "User first name is required", new Func<bool>(() => FirstName == null)), }; 

Note that there are some restrictions on input output for lambda expressions ... for this reason, the new Func<bool> delegate creation method is used.

Alternative:

 Tuple.Create(FirstName, "User first name is required", (Func<bool>)(() => FirstName == null)), Tuple.Create<string, string, Func<bool>>(FirstName, "User first name is required", () => FirstName == null), new Tuple<string, string, Func<bool>>(FirstName, "User first name is required", () => FirstName == null), 

In the end, you should repeat Func<bool> somewhere.

+6
source

Source: https://habr.com/ru/post/989614/


All Articles