Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/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#_Properties_Structure - Fatal编程技术网

C# 如何在公共属性中设置私有结构字段?

C# 如何在公共属性中设置私有结构字段?,c#,properties,structure,C#,Properties,Structure,我有一个结构: public struct ServiceDescription { string serviceDescriptionText; string serviceLogoRef; /// <summary> /// The position to print the service on the label. /// </sum

我有一个结构:

public struct ServiceDescription
        {
            string serviceDescriptionText;
            string serviceLogoRef;

            /// <summary>
            /// The position to print the service on the label.
            /// </summary>
            int servicePosition;

            /// <summary>
            /// Constructor
            /// </summary>
            /// <param name="serviceDescriptionText"></param>
            /// <param name="serviceLogoRef"></param>
            /// <param name="servicePosition"></param>
            public ServiceDescription(string serviceDescriptionText, string serviceLogoRef,
                                        int servicePosition)
            {
                this.serviceDescriptionText = serviceDescriptionText;
                this.serviceLogoRef = serviceLogoRef;
                this.servicePosition = servicePosition;
            }
        }

如何在我的属性的setters中设置结构的每个私有字段?

使用可变结构通常是一个非常糟糕的主意;可变值类型语义不是人们通常期望的。但是,您可以简单地添加一个
集合
,它的工作方式与任何其他常规的setter类似。这不是一个好主意:

public string Text
{
    get { return text; }
    set { text = value; } // this is a really bad idea on a struct
}
如果是我,那将是一个具有
私有只读
字段的不可变结构和一个设置所有字段(以及
get
-仅属性)的构造函数,或者是一个类-可能具有自动实现的属性,即

public class ServiceDescription {
    public string Text {get;set;}
    //...
}

拥有可变结构通常是一个非常糟糕的主意;可变值类型语义不是人们通常期望的。但是,您可以简单地添加一个
集合
,它的工作方式与任何其他常规的setter类似。这不是一个好主意:

public string Text
{
    get { return text; }
    set { text = value; } // this is a really bad idea on a struct
}
如果是我,那将是一个具有
私有只读
字段的不可变结构和一个设置所有字段(以及
get
-仅属性)的构造函数,或者是一个类-可能具有自动实现的属性,即

public class ServiceDescription {
    public string Text {get;set;}
    //...
}

你是说
设置{p_sServiceDescription=value;}
?可变结构是邪恶的。这种类型首先不应该是结构,它不符合作为结构的任何标准。嗯,我不知道,因为这将如何在我的结构中设置私有字符串变量?@Servy如果它是不可变的,我将不会对它作为结构有任何问题。有些情况下,一组关联的数据作为一个结构非常有意义。@dotNetBlackBelt如果属性在您的结构中,它可以访问私有字段,但我一开始甚至没有看到
p_sServiceDescription
字段。您的意思是
设置{p_sServiceDescription=value;}
?可变结构是邪恶的。这种类型首先不应该是结构,它不符合作为结构的任何标准。嗯,我不知道,因为这将如何在我的结构中设置私有字符串变量?@Servy如果它是不可变的,我将不会对它作为结构有任何问题。有些情况下,一组关联的数据作为一个结构非常有意义。@dotNetBlackBelt如果属性在您的结构中,它可以访问私有字段,但我甚至没有看到一个
p\u sServiceDescription
字段。请注意,对于打算用作其他类型属性的struct,struct的可设置字段更糟糕,因为您无法通过
instance.StructField.Value=“newValue”
真正设置它,因为事实属性返回了struct的副本。感谢您的高级知识。+1。请注意,对于打算用作其他类型属性的struct,struct的可设置字段更糟糕,因为您无法通过
instance.StructField.Value=“newValue”
真正设置它,因为事实属性返回了struct的副本。感谢您的高级知识。