HomeCSharpC# Error CS1958 – Object and collection initializer expressions may not be applied to a delegate creation expression

C# Error CS1958 – Object and collection initializer expressions may not be applied to a delegate creation expression

C# Error

CS1958 – Object and collection initializer expressions may not be applied to a delegate creation expression

Reason for the Error & Solution

Object and collection initializer expressions may not be applied to a delegate creation expression.

A delegate has no members like a class or struct has, and so there is nothing for an object initializer to initialize. If you encounter this error, it is probably because there are braces after the delegate creation expression. Just remove the braces and this error will disappear.

To correct this error

  1. Remove the braces.

Example

The following code produces CS1958:

// cs1958.cs  
public class MemberInitializerTest  
{
    delegate void D<T>();  
    public static void GenericMethod<T>() { }  
    public static void Run()  
    {  
        D<int> genD = new D<int>(GenericMethod<int>) { }; // CS1958  
       // Try the following line instead  
      // D<int> genD = new D<int>(GenericMethod<int>);  
    }  
}  

Leave A Reply

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

You May Also Like

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...