Home > Net >  CS1950 The best overloaded add method Dictionary<string,Func<string>) for the collection in
CS1950 The best overloaded add method Dictionary<string,Func<string>) for the collection in

Time:02-10

I am running into this error.. not sure what I am missing. This is reproducible code.

    class Program
    {
        static void Main(string[] args)
        {
            string myString = "test";

            IDictionary<string, Func<string>> identityInformation = new Dictionary<string, Func<string>>
        {
            { "text1", test(myString) },
            { "text2", test(myString) }
        };


    }
        public static string test(string myString)
        {
            return myString;
        }

    }

CodePudding user response:

Not sure which is defined interface you need to follow. There are multiple ways to fix it. Because Func it has no parameters and returns string, which test method is not. Therefore:

A- Change dictionary to store results

 Dictionary<string, string>

B- change signature of dictionary to Func<string,string> and store delegates. Then myString as parameter will be used later.

IDictionary<string, Func<string,string>> identityInformation = new Dictionary<string, Func<string,string>>
{
    { "text1", test },
    { "text2", test }
};

C- add lambda

IDictionary<string, Func<string>> identityInformation = new Dictionary<string, Func<string>>
{
    { "text1", () => test(myString) },
    { "text2", () => test(myString) }
};
  •  Tags:  
  • Related