Problems with Arrays Passed as Parameters in C

  • 2021-09-16 07:53:53
  • OfStack

Principle: Control modifications to data as much as possible. If you can predict that data will not or should not be changed, control it, and don't expect callers who use the data not to change its value.

If the parameters are accidentally modified during use, it will bring unpredictable results, and this error is difficult to check. Therefore, when designing method parameters, we should fully consider the possible consequences of passing reference type parameters or passing reference type parameters by reference.

If a piece of data cannot be changed during delivery, it is necessary to build the object so that its value (field or property) is not changed.

1. Control of simple parameters

1. Value type parameter passing

In this case, because a copy of the parameter is passed, the original value is not affected and no control is needed.

2. Passing reference type parameters

a, a data structure composed of value types

The field needs to be set to read-only, and the property is only get. Assignment can only be done through constructors.

b, which contains the data structure of the reference type field

This situation is recursive, and it is necessary to ensure that the field is readonly and the attribute is get, and the type used by the reference type field also meets this requirement.


public class SuperClass
{
private readonly int _no;
private readonly SubClass _tag;
public int NO
{
get{ return _no;}
}
public SubClass Tag
{
get{ retirn _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;
}
}

2. Control over complex reference type parameter passing

Complex means that the parameter is of array or collection type, or the parameter contains data of these types. In this case, the above method cannot guarantee that the parameter data will not be modified, because even if the object is read-only, the array or collection fields (properties) in the object can still be modified.

1. Collection parameters (there are also 1 reference parameters including collection fields)

. net prior to 4.5 can replace concrete collection types with interfaces that do not contain methods for modifying collection elements. For example, using IEnumerable < T > Interface replaces List < T > . Version 4.5 can use the IReadOnlyCollection interface directly or the collection type that implements it.

2. Array parameters

There is no good way to protect array type parameters from modification, so try to avoid using array types as method parameters unless optional parameters are used.

3. To understand the above things, you need to distinguish the differences between the concepts under 1

1. The difference between value type and reference type

2. The difference between value passing and reference passing (ref and out)

3. The difference between passing reference type parameters and passing reference type parameters (ref and out) [This is the most confusing point]

The difference is that when a new object is created for the reference during the use of this parameter, the former does not affect the original value, while the latter affects the original value. 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 Or the original object  TEST
FunB(ref a);
Print(a); //a Become a new object  B
}

Remember one principle:

The value type passes a copy of the value, and the reference type passes an object reference, so the modification of the value parameter does not affect the original value, and the modification of the reference type affects the original value; Parameter construction of value passing does not affect the original value, and reference passing (ref and out) affects the original value.

The above content is this site to introduce the array in C # as a parameter transfer caused by the problem, I hope to help you!


Related articles: