| |

VerySource

 Forgot password?
 Register
Search
View: 1492|Reply: 11

By value or by instance?

[Copy link]

1

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-19 21:00:01
| Show all posts |Read mode
It is often seen that when inserting data, when using the data processing layer method, many people build a data class in the project. Then before invoking the data processing layer method, instantiate that data class and pay values ​​for the properties inside. Then pass the instance as a parameter to the data processing method?
I would like to ask why is this better than passing each variable directly as a parameter?
Can you talk about the advantages and disadvantages of these two methods of passing parameters?
Reply

Use magic Report

0

Threads

73

Posts

27.00

Credits

Newbie

Rank: 1

Credits
27.00

 China

Post time: 2020-1-27 15:27:01
| Show all posts
Because if the reference type is passed directly, it may be modified internally by the method
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-27 15:54:02
| Show all posts
No big difference!
Reply

Use magic Report

0

Threads

58

Posts

32.00

Credits

Newbie

Rank: 1

Credits
32.00

 China

Post time: 2020-1-27 16:09:01
| Show all posts
The advantage of passing objects is to keep the interface uniform. If there is a change, only change it internally instead of changing the interface.
Reply

Use magic Report

1

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-1-27 17:45:01
| Show all posts
Because if the reference type is passed directly, it may be modified internally by the method

Can you give an example?
That is to say, if the parameter data is not used after calling the method, are they the same?
So if there are many parameters, such as more than 10 parameters, will there be a difference in efficiency?
Reply

Use magic Report

1

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-1-27 19:27:02
| Show all posts
everever30
The advantage of passing objects is to keep the interface uniform. If there is a change, only change it internally instead of changing the interface.

That is to say, is the difference between the two just in writing code?
Is it the same when running the code?
Reply

Use magic Report

0

Threads

322

Posts

115.00

Credits

Newbie

Rank: 1

Credits
115.00

 China

Post time: 2020-1-27 20:54:01
| Show all posts
By default in C #, references are passed.

string [] ary = new string [] {"a"};
this.DoChante (ary);
Response.Write (ary [0]);


public void DoChante (string [] a)
{
a [0] = "B";
}
Reply

Use magic Report

0

Threads

73

Posts

27.00

Credits

Newbie

Rank: 1

Credits
27.00

 China

Post time: 2020-1-27 22:09:01
| Show all posts
shenmue024has been written, haha.
As for the efficiency, it is definitely worse to pass parameters by instance, but if the code is good enough, it should be ignored, huh.
Reply

Use magic Report

0

Threads

27

Posts

21.00

Credits

Newbie

Rank: 1

Credits
21.00

 Belgium

Post time: 2020-1-27 22:54:01
| Show all posts
Passing a reference or passing an instance depends on your specific application. Generally, references are passed. One is fast. The other is that you can modify the value of the variable inside the class. If you do not want to modify the original value, you can consider传 Instance.
Reply

Use magic Report

0

Threads

64

Posts

35.00

Credits

Newbie

Rank: 1

Credits
35.00

 China

Post time: 2020-1-27 23:27:01
| Show all posts
By abstracting data operations, you can use them longer and fewer problems, avoiding constant refactoring.

If the 20 subclasses of type A objects are related to the 20 subclasses of type B, 400 types of relationships may need to be defined, and 10 large books need to be written to explain, but if the subclasses inherit the parent class, you do not need to export the subclasses The connection was described piecemeal, and perhaps the agreement was made clear with only two pages of A4 paper.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list