Expression<Func< int , bool >> exprTree = num => num < 5;
// Decompose the expression tree.
ParameterExpression param = (ParameterExpression)exprTree.Parameters[0];
BinaryExpression operation = (BinaryExpression)exprTree.Body;
ParameterExpression left = (ParameterExpression)operation.Left;
ConstantExpression right = (ConstantExpression)operation.Right;
Console .WriteLine( "Decomposed expression: {0} => {1} {2} {3}" ,
param.Name, left.Name, operation.NodeType, right.Value);
* This source code was highlighted with Source Code Highlighter .
In this example, we first describe the lambda expression num => num <5 , and then use the objects from the Expression Trees to parse the expression.class Test1
{
}
static void Main( string [] args)
{
dynamic t = new Test1();
string str = t.Hello(); // Error 1
dynamic d = 7.0;
int i = d; // Error 2
}
* This source code was highlighted with Source Code Highlighter .
Surprisingly, this code will compile and run. It's all about the magic word dynamic , it allows us to call any properties or methods by name, and also to cast an object to any type. During Runtime (code execution) errors will crash, Error 1: that the method was not found, Error 2: that it is impossible to result in a double to int. Let's try to fix them: to fix the first error, our Test1 class is inherited from the System.Dynamic.DynamicObject type and overload one of the methods, to fix the second one just explicitly specify the type conversion:class Test1 : DynamicObject
{
public override bool TryInvokeMember(InvokeMemberBinder binder, object [] args, out object result)
{
if (binder.Name == "Hello" )
{
result = "Test1 is Dynamic Object!" ;
return true ;
}
return base .TryInvokeMember(binder, args, out result);
}
}
static void Main( string [] args)
{
dynamic t = new Test1();
string str = t.Hello();
dynamic d = 7.0;
int i = ( int ) d;
}
* This source code was highlighted with Source Code Highlighter .
Now our code will work. The variable str get the value "Test1 is a dynamic object!" , and i value is 7 .class Test1
{
public void Method( int a = 0, string b = "Hello" , bool c = true )
{
Console .WriteLine( "{0}, {1}, {2}" , a, b, c);
}
}
static void Main( string [] args)
{
Test1 o = new Test1();
//
o.Method(1, "Hello" , true );
//
o.Method(b: "hello" , c: true , a: 1);
//
// ( )
o.Method();
//
o.Method(1, "Hello" );
//
o.Method(c: false );
}
* This source code was highlighted with Source Code Highlighter .
Now, because of the renaming of a parameter of a method, the code may not compile if someone has used the setting value by name, so you need to be careful. I am happy with default values, and I will try not to use the functionality of the named parameters.excel.Cells[1, 1].Value = "Hello" ;
//
((Excel.Range)excel.Cells[1, 1]).Value2 = "Hello" ;
* This source code was highlighted with Source Code Highlighter .
This example is taken from the New Futures in C # 4.0 document. On the one hand, it's nice that now you don’t have to suffer and find what type you need to bring an object to call its property or method, but on the other hand, IntelliSense is lost.IList< string > strings = new List < string >();
IList< object > objects = strings;
* This source code was highlighted with Source Code Highlighter .
But it is impossible. Because you can write next:objects[0] = 5;
string s = strings[0];
* This source code was highlighted with Source Code Highlighter .
That is, initially we had a list of strings, then we designated it as a list of objects, and we want to work with it as objects, setting any other object to it, although the list is still a list of strings.IEnumerable < object > objects = strings;
* This source code was highlighted with Source Code Highlighter .
This functionality will bring great utility in working with linq, there are often problems that return objects of one type, and you need to get a list of another type (base).public interface IEnumerable < out T> : IEnumerable
{
IEnumerator<T> GetEnumerator();
}
public interface IEnumerator< out T> : IEnumerator
{
bool MoveNext();
T Current { get ; }
}
* This source code was highlighted with Source Code Highlighter .
There is another word in . It can also be used in the description of generic delegates and interfaces. It carries the same meaning as the word out , only in this case the described type can be used only in the transfer of parameters, here is an example:public interface IComparer< in T>
{
public int Compare(T left, T right);
}
* This source code was highlighted with Source Code Highlighter .
That is, in this case, if IComparer <object> can be considered as IComparer <string> , because if it can compare objects of type object , then string can also.public delegate TResult Func< in TArg, out TResult>(TArg arg);
* This source code was highlighted with Source Code Highlighter .
ConclusionSource: https://habr.com/ru/post/60206/
All Articles