Asaf Shazar
Asaf Shazar

Reputation: 1065

List add a ref to string object

Is there any option to do something like this:

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);


    }

}

so when I change the AllProperties[0] it will change the PassPort string variable???

Upvotes: 1

Views: 56

Answers (1)

heijp06
heijp06

Reputation: 11788

I am not really sure what it is you are after, but you might be able to use an indexer:

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();
            }
        }
    }
}

And the use that like this:

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

Upvotes: 2

Related Questions