Principle:Control modifications to data as much as possible. If you can predict that certain data will not or should not be changed, control it, and do not expect callers using this data to not change its value.

If a parameter is accidentally modified during use, it will bring unpredictable results, and such errors are difficult to detect. Therefore, when designing method parameters, we must fully consider the possible consequences of passing reference-type parameters or passing reference-type parameters by reference.

If a data cannot be changed during passing, its values (fields or properties) should be made unchangeable when the object is constructed.


I. Control of Simple Parameters

1. Passing Value-Type Parameters

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

2. Passing Reference-Type Parameters

a. Data Structures Composed of Value Types

Fields need to be set as read-only, and properties should only have getters. Assignment can only be done through constructors.

b. Data Structures Containing Reference-Type Fields

This situation is recursive. While ensuring fields are readonly and properties have only getters, the types used by reference-type fields must also meet the same requirements.

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;
    }
}

II. Control over Complex Reference-Type Parameter Passing

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

1. Collection Parameters (Same for Reference Parameters Containing Collection Fields)

For versions before .NET 4.5, you can use interfaces that do not include methods for modifying collection elements to replace concrete collection types. For example, use IEnumerableinterface instead of List.NET 4.5 can directly use the IReadOnlyCollection interface or collection types that implement this interface.

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, except when optional parameters are needed.


III. To Understand the Above, You Need to Clearly Distinguish the Differences Between the Following Concepts

  • 1. The Difference Between Value Types and Reference Types
  • 2. The Difference Between Passing by Value and Passing by Reference (ref and out)
  • 3. The Difference Between Passing Reference-Type Parameters and Passing Reference-Type Parameters by Reference (ref and out) [This point is the most confusing]

The difference is that when a new object is created for the reference during use of the 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还是原始的对象 TEST
    FunB(ref a);
    Print(a);               //a变为新对象   B
}

Remember one principle:Value types pass a copy of the value, reference types pass an object reference. Therefore, modifications to value parameters do not affect the original value, while modifications to reference types affect the original value; constructing a parameter passed by value does not affect the original value, while passing by reference (ref and out) affects the original value.

Original address: https://www.cnblogs.com/from1991/p/5335585.html