Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将引用正确分配给列表c#_C#_String_List_Reference - Fatal编程技术网

将引用正确分配给列表c#

将引用正确分配给列表c#,c#,string,list,reference,C#,String,List,Reference,我试图将字符串添加到列表中,以便在某个时间点使用循环打印它们,更具体的是,这是我代码的一部分: public class Foo{ public string propertyA; public string propertyB; public string propertyC; public List<string> list; Public Foo(){ list = new List<string>();

我试图将字符串添加到
列表中
,以便在某个时间点使用循环打印它们,更具体的是,这是我代码的一部分:

public class Foo{
    public string propertyA;
    public string propertyB;
    public string propertyC;
    public List<string> list;
    Public Foo(){
         list = new List<string>();
         list.Add(propertyA);
         list.Add(propertyB);
         list.Add(propertyC);
    }
}
公共类Foo{
公共字符串属性a;
公共字符串属性b;
公共字符串属性c;
公开名单;
公共食物({
列表=新列表();
列表。添加(属性a);
列表。添加(属性b);
列表。添加(propertyC);
}
}

在后面的代码中,在分配
propertyA
和其他变量并尝试迭代列表后,我得到了空字符串。我要求属性在列表中。我的问题是,实现这一点的最佳方法是什么?

看起来您得到的是空字符串,因为当您向列表中添加属性时,在调用Foo()构造函数时尚未设置属性中的值

尝试传递值并在Foo构造函数中设置它们,如下所示:

public class Foo{
    public string propertyA;
    public string propertyB;
    public string propertyC;
    public List<string> list;
    Public Foo(string propA, string propB, string propC){
         propertyA = propA;
         propertyB = propB;
         propertyC = propC;
         list = new List<string>();
         list.Add(propertyA);
         list.Add(propertyB);
         list.Add(propertyC);
    }
}

如果要在创建
Foo
实例并枚举它们后分配
propertyA
B
C
,可以尝试以下操作:

public class Foo
{
    public string propertyA { get { return list[0]; } set { list[0] = value; } }
    public string propertyB { get { return list[1]; } set { list[1] = value; } }
    public string propertyC { get { return list[2]; } set { list[2] = value; } }
    public List<string> list = new List<string>() {"", "", ""};
}
公共类Foo
{
公共字符串属性{get{return list[0];}set{list[0]=value;}
公共字符串propertyB{get{return list[1];}set{list[1]=value;}
公共字符串propertyC{get{return list[2];}set{list[2]=value;}
public List List=new List(){“”,“”};
}

有关代码以您可能不期望的方式运行的原因,请参见您看到的是预期行为。稍后更新“propertyA”等不会更新已添加到集合中的字符串

你可以考虑使用<代码>字典<代码>代替你自己的类,然后添加和更新元素更容易:(并且你不必用新的属性名来更新你的类)

或者,如果您想要一个类以及向其添加属性的选项,那么像这样扩展
Dictionary
类至少会使维护更容易,因此您可以添加更多与底层
Dictionary
保持同步的属性,而无需太多麻烦

public class PropertyCollection : Dictionary<string, string>
{
    public string PropertyA
    {
        get { return GetValue(); }
        set { StoreValue(value); }
    }

    public string PropertyB
    {
        get { return GetValue(); }
        set { StoreValue(value); }
    }

    protected string GetValue([CallerMemberName] string propName = "")
    {
        if (ContainsKey(propName))
            return this[propName];

        return "";
    }

    protected void StoreValue(string propValue, [CallerMemberName] string propName = "")
    {
        if (ContainsKey(propName))
            this[propName] = propValue;
        else
            Add(propName, propValue);
    }
}
公共类属性集合:字典
{
公共字符串属性a
{
获取{return GetValue();}
设置{StoreValue(value);}
}
公共字符串属性b
{
获取{return GetValue();}
设置{StoreValue(value);}
}
受保护的字符串GetValue([CallerMemberName]string propName=”“)
{
if(ContainsKey(propName))
返回此[propName];
返回“”;
}
受保护的void存储值(string propValue,[CallerMemberName]string propName=”“)
{
if(ContainsKey(propName))
此[propName]=propValue;
其他的
添加(propName、propValue);
}
}

我假设您正在分配给
属性a
。。。等等,在代码的后面?是的,我在代码的后面分配它。当您在构造函数上填充列表时,当您更改/分配一些值给propertyA时,它不会更新列表。是的,它仍然这样做,我认为将属性添加到列表会创建对属性的某种引用,但事实并非如此。问题是我无法在构造函数上分配属性,它是稍后分配的。
var properties = new Dictionary<string, string>();
properties.Add("propertyA", "some value of property A");
properties["propertyA"] = "some new value";
MessageBox.Show(string.Join(Environment.NewLine, properties));
public class PropertyCollection : Dictionary<string, string>
{
    public string PropertyA
    {
        get { return GetValue(); }
        set { StoreValue(value); }
    }

    public string PropertyB
    {
        get { return GetValue(); }
        set { StoreValue(value); }
    }

    protected string GetValue([CallerMemberName] string propName = "")
    {
        if (ContainsKey(propName))
            return this[propName];

        return "";
    }

    protected void StoreValue(string propValue, [CallerMemberName] string propName = "")
    {
        if (ContainsKey(propName))
            this[propName] = propValue;
        else
            Add(propName, propValue);
    }
}