This question already has answers here:
Call constructor as a function in C#
(3 answers)
Closed 7 years ago.
I have the following lambda expression:
Something((o, i) => new TheNewSwiss(o, i));
Is there a shorthand syntax? Something simliar to this but for ctors?
Something((o, i) => TheNewSwiss.New(o,i));
Something(TheNewSwiss.New);
No, there's no equivalent of method group conversions for constructors. (I can see it being handy, but it doesn't exist.) You just need to use the lambda expression syntax you've got in your first snippet.
Related
This question already has answers here:
What does Lambda Expression Compile() method do?
(3 answers)
Performance of Expression.Compile vs Lambda, direct vs virtual calls
(4 answers)
Compiled C# Lambda Expressions Performance
(4 answers)
Closed 3 years ago.
I understand the result and how to execute the compiled expression but why bother doing it?
// Lambda expression as data in the form of an expression tree.`
System.Linq.Expressions.Expression<Func<int, bool>> expr = i => i < 5;
// Compile the expression tree into executable code.
Func<int, bool> deleg = expr.Compile();
// Invoke the method and print the output.
Console.WriteLine("deleg(4) = {0}", deleg(4));
/* This code produces the following output:
deleg(4) = True
*/
`
This question already has answers here:
What does the => operator mean in a property or method?
(7 answers)
Closed 5 years ago.
The code below is used to initialise a store in some C# code I am looking at. However I do not understand the => syntax in this context.
Any ideas?
protected new ServiceRepositoryStore<T> Store => (ServiceRepositoryStore<T>) base.Store;
That is an example of an expression bodied read-only property:
https://msdn.microsoft.com/en-us/magazine/dn802602.aspx
It's a C# 6.0 feature and is the equivalent of:
protected new ServiceRepositoryStore<T> Store
{
get
{
return (ServiceRepositoryStore<T>) base.Store;
}
}
This question already has an answer here:
LINQ - Query syntax vs method chains & lambda [closed]
(1 answer)
Closed 5 years ago.
In some cases I see LINQ written this way:
L.Select(_ => _.A).Where(...)
and in some other cases, I see this:
A = from B in C where (...)
Do these two syntaxes have different names?
I understand both, but they seem to be referred to as LINQ so I am a bit confused.
The first one is Method Syntax or Method extension syntax or Fluent
The second one is Query Syntax or Query Expression Syntax
This question already has answers here:
Like in Lambda Expression and LINQ
(6 answers)
Closed 5 years ago.
I'm trying to filter out results based on an input string, and I need to use SQL LIKE operation for that and not a normal comparison. Couldn't find the solution online(probably didn't use the correct search words) :
return _context.Cities.Where(t => t.Name == cityName);
And I need it to do WHERE t.name LIKE '%CityName%' . How do I simulate it here ?
_context.Cities.Where(t=> t.Name.Contains(cityName));
This question already has answers here:
Generating Delegate Types dynamically in C#
(2 answers)
Closed 8 years ago.
In C# can you do something like
Func<typeof(variableType),int)> myDelegate;
where you can pass the type arguments dynamically to a delegate?
No. typeof() is evaluated at run-time. Your delegate declaration is evaluated at compile time. The typeof() evaluation would have to occur first for this to work.
You can not use Func<typeof(variableType),int)> myDelegate;.and get syntax error.
Use:
Func<object,int> myDelegate;
or:
Func<dynamic,int> myDelegate;
And see this:Generating Delegate Types dynamically in C#