Differences between ref and out type parameters in C # and usage of params type parameters

Source: Internet
Author: User

Http://www.cnblogs.com/ConjurerYang/archive/2009/07/17/1525643.html

You are welcome to visit my blog:http://www.ybloy.com

To tell you a story:

In ancient times, in a busy big city called C #, there were two jewelry processing shops, one called ref, and the other called out.

One day, two people named A and B each brought a kilo of gold to process the jewellery.

A went to ref shop, ref's shopkeeper told a said, treat the official a bit, we processing speed is very fast, about one hours or so. A said, I dare to time, can use my gold for ready-made jewelry. Ref boss said, Sir, is really sorry, we only for the guests processing, we do not have such services. If you dare to take the time, I recommend you go out shop, they specialize in doing such business.

B went out of the shop, said, the shopkeeper, you have to use my this kilogram of gold for my jewelry processing. Out shop the shopkeeper embarrassed smiled, Sir really is sorry we only use Gold Exchange processing good jewelry. We do not provide processing services, you can go to ref store, they specialize in doing such business.

In this way, the two stores to do their own, said that the peer is a friend. Two stores have a good relationship. The business was steaming up.

Both shops have been in peace for many years. Suddenly the east opened a shop called params. This is all processing, whether it is gold jewelry or black clouds. However, due to the lack of professionalism, there are not many guests.

(story only, if there is similarity.) Purely entertaining. )

Example code: 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace Refdifferencetoout
7 {
8 class Program
9 {
A static void Main (string[] args)
11 {
12//The arguments made here as ref types must be initialized. A ref type parameter passing in an uninitialized variable will have an error.
The string refteststr = string. Empty;
14
15//As an out type parameter can be uninitialized, because the out type parameter is assigned within the function.
String outteststr = String. Empty;
17
Program P = new program ();
19
Console.WriteLine ("Default NULL Output:");
Console.WriteLine (P.useref (ref refteststr));
Console.WriteLine (P.useout (out outteststr));
Console.WriteLine ("Refteststr:" + refteststr);
Console.WriteLine ("Outteststr:" + outteststr);
Console.WriteLine ("-------------------");
26
Refteststr = "1";
Outteststr = "2";
29
Console.WriteLine ("New assignment: Refteststr = \ 1\"; outteststr = \ "2\"; ");
Console.WriteLine (P.useref (ref refteststr));
Console.WriteLine (P.useout (out outteststr));
Console.WriteLine ("Refteststr:" + refteststr);
Console.WriteLine ("Outteststr:" + outteststr);
Console.WriteLine ("-------------------");
36
37
REFTESTSTR = "3";
OUTTESTSTR = "4";
Console.WriteLine ("New assignment: Refteststr = \ 3\"; outteststr = \ "4\"; ");
Console.WriteLine (P.useref (ref refteststr));
Console.WriteLine (P.useout (out outteststr));
Console.WriteLine ("Refteststr:" + refteststr);
Console.WriteLine ("Outteststr:" + outteststr);
Console.WriteLine ("-------------------");
46
47
Console.WriteLine ("--------params-------");
P.param ("Str_a", "2", "3", "4");
50
51
52}
53
The public string useref (ref string reftest)
55 {
The return reftest + + = "Rrrrrref";
57}
58
The public string useout (out string outteststr)
60 {
61//Here you need to assign a new value to Outtest
Outteststr = "0";
Outteststr = "Oooooout";
return outteststr;
65}
66
///<summary>
///params parameter exercises.
///</summary>
///<param name= "a" > same as String parameter </param>
///<param name= "List" > String type list Parameters </param>
params public void param (String A, string[] list)
73 {
Console.WriteLine (list. ToString ());
75
the int i = 0;
77
Console.WriteLine (a);
A-foreach (string s in list)
80 {
Bayi Console.WriteLine (i++ + "_" + s);
82}
83}
84}
85}
86

Output results:

Default NULL value output:

Rrrrrref

0oooooout

Refteststr:rrrrrref

Outteststr:0oooooout

-------------------

New assignment: Refteststr = "1"; outteststr = "2";

1rrrrrref

0oooooout

Refteststr:1rrrrrref

Outteststr:0oooooout

-------------------

New assignment: Refteststr = "3"; outteststr = "4";

3rrrrrref

0oooooout

Refteststr:3rrrrrref

Outteststr:0oooooout

-------------------

--------params-------

System.string[]

Str_a

0_2

1_3

2_4

Summarize:

Both ref and out take the form of a reference to a function parameter-whether it is a value type parameter or a reference type parameter, and must display the life parameter as a ref/out form when defining the function and calling the function. Both allow functions to pass back multiple results.

The difference between the two:

The design ideas of the two parameter types are different, the purpose of ref is to pass a value type parameter as a reference parameter to a function, which is an input parameter of a function, and any change within a function will affect the value of that parameter outside the function, and out is to get the return value of the function, which is the output parameter, The value computed from the function is then passed back to the function, so it must be assigned a value within the function, which will flush out any assignment outside the function, making the function outside assignment meaningless.

The performance is:

1, out must be initialized in the body of the function, which makes it meaningless to initialize outside. In other words, the parameters of the out type cannot get the initial values that are passed in in the function body.
2, ref must be initialized in the outside of the function.
3, any modification of both in the function body will affect the outside of the function body.

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.