Solve the problem of passing C# array as parameter

黄舟
Release: 2017-09-19 11:28:21
Original
2242 people have browsed it

Principle: Control the modification of data as much as possible. If it can be predicted that a certain data will not or should not be changed, it must be controlled, and do not expect that the caller using this data will not change its value. .

If the parameters are accidentally modified during use, unpredictable results will occur, and this error is difficult to detect, so we must fully consider it when designing method parameters. Possible consequences of passing reference type parameters or passing reference type parameters by reference.

If a piece of data cannot be changed during the transfer process, the value (field or attribute) must not be changed when constructing the object.

1. Control of simple parameters

1. Passing value type parameters

In this case, because of the passed It is a copy of the parameter, does not affect the original value, and does not require control.

2. Passing reference type parameters

a. Data structure composed of value types

Need to set the fields to Read-only, attributes only get. Assignment can only be done through the constructor.

b. Data structure containing reference type fields

This situation is recursive. You need to ensure that the fields are readonly and the attributes are get. At the same time, the reference type fields are Usage types also meet 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;
     }
}
Copy after login

2. Control of passing complex reference type parameters

The so-called complexity means that the parameter is an array or collection type, or The parameters contain these types of data. 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 (the same applies to reference parameters containing collection fields)

.net versions prior to 4.5 can use interfaces that do not contain methods to modify collection elements instead of specific Collection type. For example, use the IEnumerable interface instead of List. Version 4.5 can directly use the IReadOnlyCollection interface or a collection type that implements this interface.

2. Array parameters

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

3. To understand the above things, you need to distinguish the difference between concepts

1. The difference between value type and reference type

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

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

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还是原始的对象 TEST
 

      FunB(ref a);

       Print(a);               //a变为新对象   B}
Copy after login

Remember one principle:

The value type transfers a copy of the value, and the reference type transfers the object reference, so the modification of the value parameter does not affect the original value, but the modification of the reference type does. Original value; parameter construction passed by value does not affect the original value, and reference passing (ref and out) affects the original value.

The above is the detailed content of Solve the problem of passing C# array as parameter. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!