In C #, the array is passed as a parameter,

Source: Internet
Author: User

In C #, the array is passed as a parameter,

Principle: control the modification of data as much as possible. If you can predict that a data will not or should not be changed, control it, instead of expecting that the caller who uses the data will not change its value.

If the parameter is accidentally modified during use, unpredictable results will be generated and such errors are hard to be detected. Therefore, when designing the method parameters, the possible consequences of passing a reference type parameter or a reference type parameter must be fully considered.

If a data cannot be changed during transmission, the value (field or attribute) of the object must not be changed when the object is built.

1. Simple Parameter Control

1. Value Type parameter transfer

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

2. Transfer of reference type parameters

A. Data Structure Composed of value types

You need to set the field to read-only, and the attribute is only get. The value assignment can only be performed by the constructor.

B. Data Structure containing reference type fields

This case is recursive. Make sure that the field is readonly and the attribute is get. the type used by the referenced 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 the transmission of parameters of complex reference types

The so-called complexity is that the parameter is an array or set type, or the parameter contains these types of data. In this case, the above method cannot ensure that the parameter data is not modified, because even if the object is read-only, however, the array or set field (attribute) in the object can still be modified.

1. Set parameters (the reference parameters containing the set fields are also the same)

In versions earlier than. net 4.5, you can use an interface that does not include the method for modifying the set element to replace the specific set type. For example, use the IEnumerable <T> interface instead of List <T>. In version 4.5, you can directly use the IReadOnlyCollection interface or set type that implements this interface.

2. array parameters

There is no good way to protect the array type parameters from being modified. Therefore, avoid using the array type as the method parameter unless an optional parameter is used.

 

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

1. Differences between value types and reference types

2. Differences between value transfer and reference transfer (ref and out)

3. Differences between passing a reference type parameter and passing a reference (ref and out) A reference type parameter [This is the easiest way to confuse]

The difference is that when you use this parameter to create an object for this reference, the former does not affect the original value, and the latter affects the original value. For example:

Void FunA (MyClass)

{

A = new MyClass ("");

}

 

Void FunB (ref MyClass)

{

A = new MyClass ("B ");

}

 

Void Test ()

{

MyClass a = new MyClass ("");

FunA ();

Print (a); // a is still the original object TEST

 

FunB (ref );

Print (a); // a changes to the new object B

}

Remember one principle:

The value type is a copy of the value, and the reference type is an object reference. Therefore, the modification of the value parameter does not affect the original value, and the modification of the reference type affects the original value; the construction of the parameter passed by the value does not affect the original value, and the reference transfer (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.