我正在为一群人编写C#代码,我正在设计要编码的方法的签名,并且我需要一种方法来确保人们不会修改他们将收到的某些参数。
他们会收到大型结构,因此它们是通过引用发送的,但是我希望他们在不修改原始值的情况下使用数据。但是由于结构很大,我们不想复制它们。
我们可以假设他们不想改变数据,我们只需要保护他们免受犯错误的影响。
C# 提供了什么解决方案?
以下是一个例子。
class MyDataBlok {
List<double> samples;
int someParams;
double lots of other params
}
class MySetOfDataBlock
{
List<MyDataBlock> DataSet;
bool SomeParam;
Double lots of other params;
}
class MethodsToBeCoded
{
ProcessSetOfData( /*some tag defining data as protected*/ MySetOfDataBlock data)
{
//Here I want code that uses data without modifying data
// nor the content on any data.DataSet[i]
}
}
struct
包含像List<...>
这样的引用类型成员,那么使用它就没问题了吗?在这种情况下,向列表中添加/删除元素会反映在调用代码中吗? - Thorsten Dittmar