func

Cannot assign a delegate of one type to another even though signature matches

青春壹個敷衍的年華 提交于 2019-11-29 03:44:43
问题 My morbid curiosity has me wondering why the following fails: // declared somewhere public delegate int BinaryOperation(int a, int b); // ... in a method body Func<int, int, int> addThem = (x, y) => x + y; BinaryOperation b1 = addThem; // doesn't compile, and casting doesn't compile BinaryOperation b2 = (x, y) => x + y; // compiles! 回答1: C# has very limited support for "structural" typing. In particular, you can't cast from one delegate-type to another simply because their declarations are

How to use a Func in an expression with Linq to Entity Framework?

假装没事ソ 提交于 2019-11-29 00:11:08
I am trying to write a linq to entity extension method that takes a Func to select a property Id and compare it against a list of ids. Classes public class A { public int AId { get; set; } } public class B { public int BId { get; set; } } Extension Method public static IQueryable<T> WithId<T>(this IQueryable<T> entities, Func<T, int> selector, IList<int> ids) { Expression<Func<T, bool>> expression = x => ids.Contains(selector(x)); return entities.Where(expression); // error here (when evaluated) } Calling Method var ids = new List<int> { 1, 2, 3 }; DbContext.EntityAs.WithId(e => e.AId, ids);

Func<T>() vs Func<T>.Invoke()

这一生的挚爱 提交于 2019-11-28 20:59:29
I'm curious about the differences between calling a Func directly vs using Invoke() on it. Is there a difference ? Is the first, syntactical sugar, and calls Invoke() underneath anyway ? public T DoWork<T>(Func<T> method) { return (T)method.Invoke(); } vs public T DoWork<T>(Func<T> method) { return (T)method(); } Or am I on the wrong track entirely :) Thanks. There's no difference at all. The second is just a shorthand for Invoke , provided by the compiler. They compile to the same IL. Invoke works well with new C# 6 null propagation operator, now u can do T result = method?.Invoke(); instead

Can someone explain what the C# “Func<T,T>” does?

孤者浪人 提交于 2019-11-28 18:10:55
I'm reading the Pro MVC 2 book, and there is an example of creating an extension method for the HtmlHelper class. Here the code example: public static MvcHtmlString PageLinks(this HtmlHelper html, PagingInfo pagingInfo, Func<int,string> pageUrl) { //Magic here. } And here is an example usage: [Test] public void Can_Generate_Links_To_Other_Pages() { //Arrange: We're going to extend the Html helper class. //It doesn't matter if the variable we use is null HtmlHelper html = null; PagingInfo pagingInfo = PagingInfo(){ CurrentPage = 2, TotalItems = 28, ItemsPerPage = 10 }; Func<int, String> pageUrl

Go map of functions

孤人 提交于 2019-11-28 16:39:46
问题 I have Go program that has a function defined. I also have a map that should have a key for each function. How can I do that? I have tried this, but this doesn't work. func a(param string) { } m := map[string] func { 'a_func': a, } for key, value := range m { if key == 'a_func' { value(param) } } 回答1: Are you trying to do something like this? I've revised the example to use varying types and numbers of function parameters. package main import "fmt" func f(p string) { fmt.Println("function f

Why is Func<> created from Expression<Func<>> slower than Func<> declared directly?

无人久伴 提交于 2019-11-28 16:07:02
Why is a Func<> created from an Expression<Func<>> via .Compile() considerably slower than just using a Func<> declared directly ? I just changed from using a Func<IInterface, object> declared directly to one created from an Expression<Func<IInterface, object>> in an app i am working on and i noticed that the performance went down. I have just done a little test, and the Func<> created from an Expression takes "almost" double the time of an Func<> declared directly. On my machine the Direct Func<> takes about 7.5 seconds and the Expression<Func<>> takes about 12.6 seconds. Here is the test

golang “undefined” function declared in another file?

我怕爱的太早我们不能终老 提交于 2019-11-28 15:45:52
I'm trying to write a basic go program that calls a function on a different file, but a part of the same package. However, it returns: undefined: NewEmployee Here is the source code: main.go : package main func main() { emp := NewEmployee() } employee.go : package main type Employee struct { name string age int } func NewEmployee() *Employee { p := &Employee{} return p } func PrintEmployee (p *Employee) { return "Hello world!" } JimB Please read "How to Write Go Code" . Don't use /src in your GOPATH . Packages are located in $GOPATH/src . For build or install you need to have your files in a

What's so great about Func<> delegate?

柔情痞子 提交于 2019-11-28 15:40:30
Sorry if this is basic but I was trying to pick up on .Net 3.5. Question: Is there anything great about Func<> and it's 5 overloads? From the looks of it, I can still create a similar delgate on my own say, MyFunc<> with the exact 5 overloads and even more. eg: public delegate TResult MyFunc<TResult>() and a combo of various overloads... The thought came up as I was trying to understand Func<> delegates and hit upon the following scenario: Func<int,int> myDelegate = (y) => IsComposite(10); This implies a delegate with one parameter of type int and a return type of type int. There are five

Explanation of Func

两盒软妹~` 提交于 2019-11-28 15:13:18
I was wondering if someone could explain what Func<int, string> is and how it is used with some clear examples. Jon Skeet Are you familiar with delegates in general? I have a page about delegates and events which may help if not, although it's more geared towards explaining the differences between the two. Func<T, TResult> is just a generic delegate - work out what it means in any particular situation by replacing the type parameters ( T and TResult ) with the corresponding type arguments ( int and string ) in the declaration. I've also renamed it to avoid confusion: string ExpandedFunc(int x)

Combining expression trees

痞子三分冷 提交于 2019-11-28 13:02:01
I have the following expression: public Expression<Func<T, bool>> UserAccessCheckExpression<T>(int userId) where T : class { return x => (IsAdmin || userId == CurrentUserId || userId == 0); } Then I want to apply this filter to several collections (IQueryable) like this one: return tasks .Where(t => t.TaskUsers .Any(x => UserAccessCheckExpression<TaskUser>(x.User) && x.SomeBool == true)); I'm getting the following error while doing so: Error 40 Cannot implicitly convert type System.Linq.Expressions.Expression<System.Func<TaskUser,bool>> to bool I can't use workaround with interface inheritance