1

ゲッター/セッター プロパティを使用して変数を取得または設定していますが、クラスの配列を作成しているため、値を設定するためにパブリック変数を使用すると、コードは正常に動作しますが、プライベート変数の値を設定する方法を知りたいだけです。私のコードは

public class Person
{
    //getter and setter for each variable
    private string _Name;
    public string Name
    {
        get { return _Name;}
        set { _Name= value; }
    }

    private int _Age;
    public int Age
    {
        get {return _Age;  }
        set  {  _Age= value;   }
    }  
        .... // other properties

    // Another Class 
    public void setValues (Person[] p,int i)
    {    p[i].Age= 30;
    }

しかし、設定変数をプライベートに変更した場合、変数を設定する方法は??

    private int _Age;
    public int Age
    {
       get {return _Age;  }
       private  set  {  _Age= value;   }
    } 
4

3 に答える 3