# Linq\_Expressions\_Expression\_TDelegate

***

**1. Creating a Delegate from an Expression**

```csharp
Expression<Func<int, bool>> predicate = x => x > 5;
Func<int, bool> func = predicate.Compile();
```

**2. Using a Delegate to Filter a Collection**

```csharp
int[] numbers = { 1, 2, 3, 4, 5, 6 };
var filteredNumbers = numbers.Where(func);
```

**3. Creating a Lambda Expression from a Delegate**

```csharp
Func<int, bool> func = x => x > 5;
Expression<Func<int, bool>> predicate = func.ToExpression();
```

**4. Using a Lambda Expression to Create a Delegate**

```csharp
Expression<Func<int, bool>> predicate = x => x > 5;
Func<int, bool> func = predicate.Compile();
```

**5. Creating a Delegate from a Property**

```csharp
Expression<Func<int, int>> propertyExpression = x => x.Value;
Func<int, int> propertyDelegate = propertyExpression.Compile();
```

**6. Creating a Delegate from a Method**

```csharp
Expression<Func<int, int, int>> methodExpression = (x, y) => x + y;
Func<int, int, int> methodDelegate = methodExpression.Compile();
```

**7. Creating a Delegate from a Static Method**

```csharp
Expression<Func<int>> staticMethodExpression = () => Math.Sqrt(16);
Func<int> staticMethodDelegate = staticMethodExpression.Compile();
```

**8. Creating a Delegate from a Constructor**

```csharp
Expression<Func<int, int, MyClass>> constructorExpression = (x, y) => new MyClass(x, y);
Func<int, int, MyClass> constructorDelegate = constructorExpression.Compile();
```

**9. Creating a Delegate from a Member Expression**

```csharp
Expression<Func<int, int>> memberExpression = x => x.Value;
Func<int, int> memberDelegate = memberExpression.Compile();
```

**10. Creating a Delegate from a Block Expression**

```csharp
Expression<Func<int, int>> blockExpression = x => { x++; return x; };
Func<int, int> blockDelegate = blockExpression.Compile();
```

**11. Creating a Delegate from a Conditional Expression**

```csharp
Expression<Func<int, bool>> conditionalExpression = x => x > 5 ? true : false;
Func<int, bool> conditionalDelegate = conditionalExpression.Compile();
```

**12. Creating a Delegate from a Binary Expression**

```csharp
Expression<Func<int, int, int>> binaryExpression = (x, y) => x + y;
Func<int, int, int> binaryDelegate = binaryExpression.Compile();
```

**13. Creating a Delegate from an Array Creation Expression**

```csharp
Expression<Func<int[]>> arrayCreationExpression = () => new int[] { 1, 2, 3 };
Func<int[]> arrayCreationDelegate = arrayCreationExpression.Compile();
```

**14. Creating a Delegate from a List Initialization Expression**

```csharp
Expression<Func<List<int>>> listInitializationExpression = () => new List<int> { 1, 2, 3 };
Func<List<int>> listInitializationDelegate = listInitializationExpression.Compile();
```

**15. Creating a Delegate from an Object Initialization Expression**

```csharp
Expression<Func<MyClass>> objectInitializationExpression = () => new MyClass { Value = 1 };
Func<MyClass> objectInitializationDelegate = objectInitializationExpression.Compile();
```

**16. Creating a Delegate from a New Array Expression**

```csharp
Expression<Func<int[]>> newArrayExpression = () => new int[3] { 1, 2, 3 };
Func<int[]> newArrayDelegate = newArrayExpression.Compile();
```

**17. Creating a Delegate from a New Value Expression**

```csharp
Expression<Func<int>> newValueExpression = () => 1;
Func<int> newValueDelegate = newValueExpression.Compile();
```

**18. Creating a Delegate from a Quote Expression**

```csharp
Expression<Func<int>> quoteExpression = Expression.Quote(() => 1);
Func<int> quoteDelegate = quoteExpression.Compile();
```

**19. Creating a Delegate from a RuntimeVariables Expression**

```csharp
Expression<Func<int>> runtimeVariablesExpression = () => __arglist();
Func<int> runtimeVariablesDelegate = runtimeVariablesExpression.Compile();
```

**20. Creating a Delegate from an Index Expression**

```csharp
Expression<Func<string, char>> indexExpression = (x) => x["a"];
Func<string, char> indexDelegate = indexExpression.Compile();
```

**21. Creating a Delegate from an Invocation Expression**

```csharp
Expression<Func<int, int>> invocationExpression = (x) => Math.Sqrt(x);
Func<int, int> invocationDelegate = invocationExpression.Compile();
```

**22. Creating a Delegate from a Lambda Expression**

```csharp
Expression<Func<int, int>> lambdaExpression = (x) => { x++; return x; };
Func<int, int> lambdaDelegate = lambdaExpression.Compile();
```

**23. Creating a Delegate from a ListInit Expression**

```csharp
Expression<Func<List<int>>> listInitExpression = () => new List<int> { 1, 2, 3 };
Func<List<int>> listInitDelegate = listInitExpression.Compile();
```

**24. Creating a Delegate from a Loop Expression**

```csharp
Expression<Func<int>> loopExpression = () => { int sum = 0; for (int i = 0; i < 10; i++) { sum += i; } return sum; };
Func<int> loopDelegate = loopExpression.Compile();
```

**25. Creating a Delegate from a MakeUnary Expression**

```csharp
Expression<Func<int, int>> makeUnaryExpression = (x) => -x;
Func<int, int> makeUnaryDelegate = makeUnaryExpression.Compile();
```

**26. Creating a Delegate from a MemberAccess Expression**

```csharp
Expression<Func<int, int>> memberAccessExpression = (x) => x.Value;
Func<int, int> memberAccessDelegate = memberAccessExpression.Compile();
```

**27. Creating a Delegate from a MemberInit Expression**

```csharp
Expression<Func<MyClass>> memberInitExpression = () => new MyClass { Value = 1 };
Func<MyClass> memberInitDelegate = memberInitExpression.Compile();
```

**28. Creating a Delegate from a MethodCall Expression**

```csharp
Expression<Func<int, int>> methodCallExpression = (x) => Math.Sqrt(x);
Func<int, int> methodCallDelegate = methodCallExpression.Compile();
```

**29. Creating a Delegate from a New Expression**

```csharp
Expression<Func<MyClass>> newExpression = () => new MyClass();
Func<MyClass> newDelegate = newExpression.Compile();
```

**30. Creating a Delegate from a Parameter Expression**

```csharp
Expression<Func<int, int>> parameterExpression = (x) => x;
Func<int, int> parameterDelegate = parameterExpression.Compile();
```

**31. Creating a Delegate from a TypeBinary Expression**

```csharp
Expression<Func<int, bool>> typeBinaryExpression = (x) => x is int;
Func<int, bool> typeBinaryDelegate = typeBinaryExpression.Compile();
```

**32. Creating a Delegate from a TypeIs Expression**

```csharp
Expression<Func<int, bool>> typeIsExpression = (x) => x is int;
Func<int, bool> typeIsDelegate = typeIsExpression.Compile();
```

**33. Creating a Delegate from a Conditional Expression**

```csharp
Expression<Func<int, int>> conditionalExpression = (x) => x > 5 ? 1 : 0;
Func<int, int> conditionalDelegate = conditionalExpression

```
