C# Error CS0765 – Partial methods with only a defining declaration or removed conditional methods cannot be used in expression trees

C# Error

CS0765 – Partial methods with only a defining declaration or removed conditional methods cannot be used in expression trees

Reason for the Error & Solution

Partial methods with only a defining declaration or removed conditional methods cannot be used in expression trees

Although a call to a removed partial method is an expression, it is not an acceptable expression in an expression tree.

To correct this error

  1. Add an implementing declaration for the partial method, or remove the code that is causing the conditional method to be excluded from compilation.

Example

The following code generates CS0765 in two locations:

// cs0765.cs  
using System;  
using System.Collections;  
using System.Collections.Generic;  
using System.Diagnostics;  
using System.Linq;  
using System.Linq.Expressions;  
  
public delegate void dele();  
  
public class ConClass  
{  
    [Conditional("CONDITION")]  
    public static void TestMethod() { }  
}  
  
public partial class PartClass : IEnumerable  
{  
    List<object> list = new List<object>();  
  
    partial void Add(int x);  
  
    public IEnumerator GetEnumerator()  
    {  
        for (int i = 0; i < list.Count; i++)  
            yield return list[i];  
    }  
  
    static void Main()  
    {  
        Expression<Func<PartClass>> testExpr1 = () => new PartClass { 1, 2 }; // CS0765  
        Expression<dele> testExpr2 = () => ConClass.TestMethod(); // CS0765  
    }  
}  

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...