Issues with Array Parameters in C
Category Programming Techniques
>
Principle: Control modifications to data as much as possible. If it is predictable that certain data will not or should not be changed, control it rather than expecting the caller using this data not to alter its value.
Unexpected modifications to parameters can lead to unpredictable results and such errors are difficult to detect. Therefore, when designing method parameters, it is crucial to consider the potential consequences of passing reference type parameters or passing reference type parameters by reference.
If data cannot be changed during transmission, ensure that its value (field or property) is immutable when constructing the object.
I. Control of Simple Parameters
1. Value Type Parameter Passing
Since a copy of the parameter is passed, the original value is unaffected and no control is needed.
2. Reference Type Parameter Passing
a. Data structures composed of value types
Fields should be set to read-only and properties should only have getters. Assignment should only be done through the constructor.
b. Data structures containing reference type fields
This is recursive and requires ensuring that fields are readonly and properties are getters, while the reference type fields also meet these requirements.
public class SuperClass
{
private readonly int _no;
private readonly SubClass _tag;
public int NO
{
get { return _no; }
}
public SubClass Tag
{
get { return _tag; }
}
public SuperClass(int no, SubClass tag)
{
_no = no;
_tag = tag;
}
}
public class SubClass
{
private readonly int _field;
public int Field
{
get { return _field; }
}
public SubClass(int field)
{
_field = field;
}
}
II. Control of Complex Reference Type Parameter Passing
Complex parameters refer to arrays or collection types, or parameters that contain these types of data. The above methods cannot guarantee that parameter data will not be modified because even if the object is read-only, the array or collection fields (properties) within the object can still be modified.
1. Collection Parameters (including reference parameters with collection fields)
For versions prior to .NET 4.5, interfaces that do not include methods for modifying collection elements can be used instead of specific collection types. For example, use IEnumerable.
2. Array Parameters
There is no good way to protect array type parameters from being modified, so avoid using array types as method parameters unless optional parameters are used.
III. Understanding the Differences Between the Following Concepts
- Differences between value types and reference types
- Differences between pass by value and pass by reference (ref and out)
- Differences between passing reference type parameters and passing reference type parameters by reference (ref and out) [This is最容易混淆]
The difference lies in the case where a new object is created for the reference during its use; the former does not affect the original value, while the latter does. Example:
void FunA(MyClass a)
{
a = new MyClass("A");
}
void FunB(ref MyClass a)
{
a = new MyClass("B");
}
void Test()
{
MyClass a = new MyClass("A");
FunA(a);
Print(a); // a remains the original object TEST
FunB(ref a);
Print(a); // a becomes the new object B
}
Remember one principle: Value types pass a copy of the value, reference types pass a reference to the object, so modifications to value parameters do not affect the original value, while modifications to reference types do; parameters passed by value do not affect the original value, while those passed by reference (ref and out) do.
>
Original Source: https://www.cnblogs.com/from1991/p/5335585.html
** Click to Share Notes
-
-
-