none
Question about Predicate<> RRS feed

  • Question

  • Hi guys,

    I just moved from c++ to c#.

    And I have following code snippet, it works.

    namespace anything

    {
        public class Configuration
        {
            public int StoreId { get; set; }
            ......
            public bool CheckStore(int Id)
            {
                return Id == StoreId;
            }
        }

    }

    Then I want to rewrite it with Predicate<>.

    namespace anything

    {
        public class Configuration
        {
            public int StoreId { get; set; }
            ......
            public Predicate<int> CheckStore = id => id == StoreId;
        }

    }

    but get error CS0236, I need to define

            public static int StoreId { get; set; }

    to get it fixed.

    Not sure why?

    Thanks.

    Richard


    lurc

    Saturday, October 14, 2017 8:56 AM

Answers

  • Try another way of initialization:

    public class Configuration
    {
       public int StoreId { get; set; }
    
       public readonly Predicate<int> CheckStore;
    
       public Configuration()
       {
          CheckStore = id => id == StoreId;
       }
    }


    • Edited by Viorel_MVP Saturday, October 14, 2017 8:24 PM
    • Marked as answer by richardlu Sunday, October 15, 2017 5:31 AM
    Saturday, October 14, 2017 8:20 PM

All replies

  • Try another way of initialization:

    public class Configuration
    {
       public int StoreId { get; set; }
    
       public readonly Predicate<int> CheckStore;
    
       public Configuration()
       {
          CheckStore = id => id == StoreId;
       }
    }


    • Edited by Viorel_MVP Saturday, October 14, 2017 8:24 PM
    • Marked as answer by richardlu Sunday, October 15, 2017 5:31 AM
    Saturday, October 14, 2017 8:20 PM
  • Thanks Viorel_, it works there.

    May I know the reason?


    lurc

    Sunday, October 15, 2017 5:32 AM