Setting List items in C# without automatic setter/getter

后端 未结 6 2056
难免孤独
难免孤独 2021-01-16 09:46

I\'m trying to make a manual setter/getter method in C#, but i\'m getting the following error from the \"set\"-line: Error: The best overloaded method match for \'System.Col

6条回答
  •  一整个雨季
    2021-01-16 10:26

    With a getter/setter the input and output types must be the same. (hence the declaration being public List Packages { ... - it is expecting an input of List and an output of List.

    What you are doing here is trying to get type List and set type Package. This is not allowed.

    What I think you want to do is the following:

    private List packages = new List();    
    
    public List Packages
    {
        // no setter
        get { return packages; }
    }
    

    Then call myObject.Packages to get all the packages and myObject.Packages.Add(package) to add.

提交回复
热议问题