List将ref添加到string对象

时间:2014-12-06 09:11:46

标签: c# list

有没有选择做这样的事情:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class Student
{


    public string PassPort { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Message { get; set; }
    public List<string> AllProperties { get; set; }

    public Student()
    {
        AllProperties = new List<string>();
        AllProperties.Add(ref PassPort);
        AllProperties.Add(ref FirstName);
        AllProperties.Add(ref LastName);
        AllProperties.Add(ref Message);


    }

}

因此,当我更改AllProperties[0]时,它将更改PassPort字符串变量???

1 个答案:

答案 0 :(得分:2)

我不确定你追求的是什么,但你可以使用索引器:

class Student
{
    public string PassPort { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Message { get; set; }

    public string this[int index]
    {
        get { throw new NotImplementedException(); }
        set
        {
            switch (index)
            {
                case 0:
                    PassPort = value;
                    break;
                case 1:
                    // etc.
                default:
                    throw new NotImplementedException();
            }
        }
    }
}

这样使用:

class Program
{
    static void Main(string[] args)
    {
        Student student = new Student();
        student[0] = "PASS";
    }
}