func

C#: Func<> instead of methods? [duplicate]

本秂侑毒 提交于 2019-12-04 17:11:25
问题 This question already has answers here : Func Delegate vs Function (6 answers) Closed 6 years ago . This is a curiosity questions for you all in the know: Is there any harm/downside to using a Func instead of a method? Simple example: private static Func<int, int, DBContext, List<T>> Foo = (i1, i2, dbc) => (i1 != 0) ? dbc.Bar(i2) : new List<T> { /*some default values ...*/ }; Vs private static List<T> Foo(int i1, int i2, DBContext dbc) { return i1 != 0 ? dbc.Bar(i2) : new List<T> { /*some

Does C have __func__ functionality for names of the arguments of a function?

余生长醉 提交于 2019-12-04 17:00:31
Does the "C" standard support something similar to __func__ for the function arguments' names? if you want a quick and dirty solution for this make pre-processor macros like this... #define FUNCTION_HEADER(a) a { const char* __func__ = #a; #define FUNCTION_FOOTER() } ... and use it for your function headers and footers like this (tested with VS 2008): #include <windows.h> #define FUNCTION_HEADER(a) a { const char* __func__ = #a; #define FUNCTION_FOOTER() } FUNCTION_HEADER( int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) ) MessageBoxA(0, __func__,

Encapsulating Action<T> and Func<T>?

ぐ巨炮叔叔 提交于 2019-12-04 10:30:45
问题 I'm trying to make a design for some sort of IExecutable interface. I will not get into details, but the point is that I have several Actions that need to be executed from a base class. They may take different parameters (no big deal), and they may/may not return a value. So far, this is my design: public abstract class ActionBase { // ... snip ... } public abstract class ActionWithResultBase<T>: ActionBase { public abstract T Execute(); } public abstract class ActionWithoutResultBase:

Conversion from Func<object,string> to Func<string,string> works but to Func<int,string> fails

喜欢而已 提交于 2019-12-04 03:58:16
问题 I have the following code: static Func<object, string> s_objToString = (x) => x.ToString(); static Func<string, string> s_stringToString = s_objToString; //compiles static Func<int, string> s_intToString = s_objToString; //error The second line compiles but the third line fails to compile with error: Cannot implicitly convert type ' System.Func<object,string> ' to ' System.Func<int,string> ' Why is that? I understand that with genetics although string is derived from object a List<string>

wrong generic type in swift

孤者浪人 提交于 2019-12-03 20:30:31
After run following code in playgroud, why x value is 2? Is there anything wrong with swift generic type and "is" operator? class Item {} class Campaign: Item {} class AdGroup : Item {} class A<T: Item> { func val() -> Int{ let item = T() if item is Campaign { return 1 } else { return 2 } } } var m = A<Campaign>() let x = m.val() 来源: https://stackoverflow.com/questions/27899744/wrong-generic-type-in-swift

Concatenate two Func delegates

核能气质少年 提交于 2019-12-03 15:14:58
问题 Assume that I have thes Class: public class Order { int OrderId {get; set;} string CustomerName {get; set;} } I declare below variables, too Func<Order, bool> predicate1 = t=>t.OrderId == 5 ; Func<Order, bool> predicate2 = t=>t.CustomerName == "Ali"; Is there any way that concatenate these variables(with AND/OR) and put the result in 3rd variable? for example: Func<Order, bool> predicate3 = predicate1 and predicate2; or Func<Order, bool> predicate3 = predicate1 or predicate2; 回答1: And: Func

In few words, what can be said about Func<>

若如初见. 提交于 2019-12-03 11:04:17
问题 I've been seing Func<> for sometime now, and I've manage to avoid it (for now). But, now it looks like I can't dodge it forever. For instance, I tried Dynamic Linq, but almost everything was in terms of Func<>. I've tried one of my book (C# 2008/Deitel&Deitel) and also MSDN but I'm not getting it yet. They all jump straight in the subject. What can be said (in few words) about Func<> Can I get some links on the web that can get me started on that matter? Thanks for helping 回答1: Func<> is a

C#: Func<> instead of methods? [duplicate]

那年仲夏 提交于 2019-12-03 10:50:34
This question already has an answer here: Func Delegate vs Function 6 answers This is a curiosity questions for you all in the know: Is there any harm/downside to using a Func instead of a method? Simple example: private static Func<int, int, DBContext, List<T>> Foo = (i1, i2, dbc) => (i1 != 0) ? dbc.Bar(i2) : new List<T> { /*some default values ...*/ }; Vs private static List<T> Foo(int i1, int i2, DBContext dbc) { return i1 != 0 ? dbc.Bar(i2) : new List<T> { /*some default values ...*/ }; } I see severale downsides: performance impact (delegate vs method) - small but it's there no

Using FluentValidation's WithMessage method with a list of named parameters

二次信任 提交于 2019-12-03 10:10:27
I am using FluentValidation and I want to format a message with some of the object's properties value. The problem is I have very little experience with expressions and delegates in C#. FluentValidation already provides a way to do this with format arguments. RuleFor(x => x.Name).NotEmpty() .WithMessage("The name {1} is not valid for Id {0}", x => x.Id, x => x.Name); I would like to do something like this to avoid having to change the message string if I change the order of the parameters. RuleFor(x => x.Name).NotEmpty() .WithMessage("The name {Name} is not valid for Id {Id}", x => new { Id =

Encapsulating Action<T> and Func<T>?

北城以北 提交于 2019-12-03 06:01:54
I'm trying to make a design for some sort of IExecutable interface. I will not get into details, but the point is that I have several Actions that need to be executed from a base class. They may take different parameters (no big deal), and they may/may not return a value. So far, this is my design: public abstract class ActionBase { // ... snip ... } public abstract class ActionWithResultBase<T>: ActionBase { public abstract T Execute(); } public abstract class ActionWithoutResultBase: ActionBase { public abstract void Execute(); } So far, each of my concrete actions need to be a child from