This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
CompilerServices.Operators equivalent on C#
I was looking for Microsoft.CSharp.CompilerServices.Operators but couldn't find it.
No there is no real equivalent to this in the C# runtime assembly.
However many these methods are essentially implementing the late bound operations for VB.Net in a declarative method (and indeed there are cases where the late binder simply just defers to these methods for operations). So these could be replicated in C# by defining methods which just explicitly defer to the C# dynamic binder.
For example, the rough equivalent of DivideObject in C# would be the following
public static dynamic DivideObject(dynamic left, dynamic right)
{
return left / right;
}
Related
This question already has answers here:
Return multiple values to a method caller
(28 answers)
Is there a way I can return more than one integer from a method? [duplicate]
(5 answers)
Closed 2 years ago.
When my method needs to return more than one value, I can use out parameters or tuples.
Is this a personal preference or do we have a recommended practice documentation how to choose between the two options?
Prefer not to use a complex class type for every o such scenario, because my solution will be bloated with so many classes.
I understand that TryParse is an example exists in .net framework, however it was introduced before tuples.
I think that if you have many results that you want to return, ValueTuple ( for .NET Framesowrk 4.7 and above ) may be better for you because is a struct and immutable, especially if you have many results to return, or you use some kind of API like LINQ and you dont want to create objects of all the result.
You can read also :
Returning two values, Tuple vs 'out' vs 'struct'
What's the difference between System.ValueTuple and System.Tuple?
This question already has answers here:
Why would you use Expression<Func<T>> rather than Func<T>?
(12 answers)
Closed 3 years ago.
I am starting to see methods like the one below used more and more, but it's a concept that I don't fully understand.
public virtual Task<List<T>> GetAsync(Expression<Func<T, bool>> exp)
{
using (var conn = _factory.OpenDbConnection())
{
return conn.SelectAsync(exp);
}
}
Can someone help me translate the method parameter there that is an Expression? Like explain it as how it differs from a standard instance parameter?
These are called Expression Trees (https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/expression-trees/).
Basically it's a lamdba that can be translated to another platform, from the looks of it some kind of database in your case. This function would be translated (by a library) to SQL and then executed in the database.
Within the code of your program you would generally only need lambdas (Func<>), but in some cases you need an Expression Tree. Besides your example, sometimes you need a dynamically constructed function which can be done using these.
General information on lambdas can be found here: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/lambda-expressions
This question already has answers here:
Is there a way to implement custom language features in C#?
(6 answers)
Closed 6 years ago.
I'm writing a library for personal use that greatly expands C# features, and I was wondering on something quite interesting... Is it possible to create you own keywords? For example, if, foreach, for etc.
The reason I want to do this can be found at my previous question.
No, you can not do that. Language keywords are defined in the language definition. You could probably use the open sourced parts (compilers, etc) and create your own version of them.
This question already has answers here:
What does the => operator mean in a property or method?
(7 answers)
Closed 7 years ago.
While researching C# operator overloading, I stumbled across this block of code on the MSDN web site:
public static Complex operator +(Complex c1, Complex c2) =>
new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
// Override ToString() to display a complex number
// in the traditional format:
public override string ToString() => $"{this.real} + {this.imaginary}";
This is a really useful way of defining simple methods in certain circumstances, but I don't recall ever seeing it described anywhere. I tried searching the C# 5.0 language specification for a description of this method declaration syntax, but could find nothing. I also found nothing in my web searches.
Two questions:
In which version of C# did this method declaration syntax become available?
Where in the language specification is this syntax described?
It was added in C# 6, you can read about it on the official Github of the new compiler here.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why it is not posible to define generic indexers in .NET?
how to write a function to take any object with an index operator
I've never seen any usage like that. But I just wonder if it is possible to make an implementation like bleow. I know that it's not working. But I mean a similar usage if exist.
public T this<T>[T param]
{
get
{
....
}
}
No, generic properties, and indexers (a property), aren't possible.