Problem solving for C # arrays as parameter passing

Source: Internet
Author: User
principle: Control changes to the data as much as possible, if you can predict that the data will not or should not be changed, it is necessary to control it, and do not expect the caller to use this data will not change its value.

If the parameter is accidentally modified during use, it will bring unpredictable results, and this error is difficult to check, so when we design the method parameters, we should consider the possible consequences of passing the reference type parameter or the reference way to pass the reference type parameter.

If a data cannot be changed during delivery, it is necessary to make its value (field or property) unchanged when the object is constructed.

First, the control of simple parameters

1. Value type parameter passing

This is because a copy of the parameter is passed, does not affect the original value, and does not require control.

2. Reference type parameter passing

A, a data structure composed of value types

The field needs to be set to read-only and the property is only get. Assignments can be made only through the construction method.

B. Data structures that contain reference type fields

This condition is recursive and requires a guarantee that the field is ReadOnly, and that the type used by the reference type fields satisfies the requirement while the property is get.


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 of parameter passing for complex reference types

The so-called complex is that the parameter is an array or collection type, or the parameter contains these types of data, in which case the above method does not guarantee that the parameter data is not modified, because even if the object is read-only, the array or collection fields (properties) in the object can be modified.

1. Set parameters (same as reference parameters containing collection fields)

Earlier versions of. NET 4.5 can replace specific collection types with interfaces that do not include methods to modify collection elements. For example, use the Ienumerable<t> interface instead of list<t>. Version 4.5 can use the Ireadonlycollection interface directly or the collection type that implements the interface.

2. Array parameters

There is no good way to protect the array type parameter from being modified, so try to avoid using the array type as a method parameter unless the optional parameter is used.

Third, understand the above things need to distinguish clearly the difference between the concept

1. Differences between value types and reference types

2. Difference between value passing and reference passing (ref and out)

3. Pass the difference between reference type parameters and reference pass (ref and out) reference type parameters [this is most easily confused]

The difference is that when you use this parameter to create a new object for the reference, the former does not affect the original value, which affects the original value, for 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 becomes new object   B}

Remember one rule:

A value type passes a copy of the value, the reference type passes the object reference, so the modification of the value parameter does not affect the original value, the modification of the reference type affects the original value, and the parameter construction of the value pass does not affect the original value, and the reference pass (ref and out) affects the original value.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.