Setting List items in C# without automatic setter/getter

后端 未结 6 2061
难免孤独
难免孤独 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:16

    You can not do that.

    if you are trying to set an element you should...

    public void addPackage(Package pack){
        packages.Add(pack);
    }
    

    usage: MyClass m= new MyClass(); m.addPackage(new Package());

    if you are trying to set the collection then ...

    public List Packages
    {
        set { packages=value; }
        get { return packages; }
    }
    

    usage:

    MyClass m= new MyClass();
    m.Packages=new List();
    

提交回复
热议问题