There are "value types" and "reference types" in the C # programming language. Before you talk about this concept, let's first explain what is a "formal parameter" (the "formal argument"), and what is "actual parameter" (the "argument").
"Formal parameter" ("Formal Parameters"): A list of parameters in a C # method, which can be called "formal parameters", because we cannot know exactly what the specific parameters are until the program is run.
"Argument" ("actual parameter"): The code value that is actually assigned to the parameter method defined by C #, called the "argument". Because we can know or estimate the number of numeric types and values of the parameters during the actual operation.
Once you know the above concepts, we'll explain "value passing" and "reference passing"
The so-called value pass means that "arguments" are passed to the "formal parameter" as a copy of itself.
The so-called reference pass means: "argument" is passed to "formal parameter" as its own "address" as a numeric value to the parameter.
The correct understanding of value transfer and reference transmission in software programming plays an important role in designing excellent software.
For the knowledge points in this section, we summarize the following rules:
Rule 1: The base data type (Int,float, Double,char) is a value pass.
Rule 2: Instantiating objects and arrays for a class are "reference" passes.
The rule 3:ref (input and output) keyword can pass the value of the base data type to "reference delivery."
Rule 4: A string is not a basic data type, but it is also a "value pass" by default and can, of course, be passed through ref.
Generally we study this section and add a common keyword: out. This keyword defines the parameter for the method as "output parameter", which makes a method with multiple output parameters. (otherwise only the return type of the method can be used to make a unique return type).
In order to better understand C # "formal parameters" and "Actual parameters", the author provides the following exercises for C # Beginners to study and study, unclear, welcome message discussion!
Exercise: Learning Object-Oriented Programming: value types and reference types
Class Program
{
Value type
public void Addnumber (int num1)//NUM1 called "Formal parameters"
{
NUM1 = num1 + 100;
}
Reference type
public void Addnumberwithref (ref int NUM1)
{
NUM1 = num1 + 100;
}
Reference type (pass-through)
public void Addnumberbyarray (int[] intarray)
{
Intarray[0] = intarray[0] + 100;
}
Reference delivery (object type)
public void Addnumberbyperson (person per)
{
Per. Intheight = 200;
Per.strname = "John Doe";
}
String passing
public void addnumberbystring (ref string str)
{
str = str + "Hello everyone!" ";
}
Demonstrate the role of out keywords
public void Computnumber (int intnum1,int intnum2,out int intaddresult,out int intsubresult)
{
Intaddresult = IntNum1 + intNum2;
Intsubresult = intnum1-intnum2;
}
static void Main1 (string[] args)
{
Person perobj = new person ();
Program obj = new program ();
int inttestnum = 10;
int[] Intarray = new Int[1];
Intarray[0] = 20;
String strtest = "schoolmates";
"Value passing" test, passing "replica"
Obj. Addnumber (Inttestnum); Inttestnum is called an "argument" and the result is: 10
"Reference data passing" test.
Obj. Addnumberwithref (ref inttestnum);//The result is: 110.
Obj. Addnumberbyarray (Intarray); The results were: 120;
"Object" Data passing
Obj. Addnumberbyperson (Perobj);
Passing Tests on strings
Obj. Addnumberbystring (ref strtest);
Demonstrate out keyword action
int intaddresult = 0;
int intsubresult = 0;
Obj.computnumber (Ten, Intaddresult, out Intsubresult);
Show results
Console.WriteLine ("10+20={0},10-20={1}", Intaddresult,intsubresult);
}
}
This article is from "Teacher Liu Speak Unity" blog, please be sure to keep this source http://liuguozhu.blog.51cto.com/9142031/1831810
C # for Unity programming language Quick Start Tutorial (serial 9) _c#oop programming values and reference types