C # property accessors, of actual and parameters, reference parameter REF, the output OUT parameter, the array parameters and default value of the parameter learning parmas

Property accessor

  • In general, the field will be set to private (private), when the outside world if you want to access, access method quickly set the field, read-only or write-only or read-write, you can use the property, also known as property accessor
public class People
{
	private string name;
	public string name
	{
		//get访问器用于读取字段,若属性内没有get访问器,则被认为是不可读的
		get{return name;}
		//set访问器用于写入字段,若属性内没有set访问器,则被认为是不可写的
		set{name = value}
		//value被视为写入的数值,如name = "xiaoming"
	}
}
  • Can also be used if private access to the outside world mark, which is set to read-only or write-only external, later you can use other attributes access modifiers, such as protected
//外界只读
public string name
{
	get{return name;}
	private set{name = value}
}
  • Fast writing property: public int Age {get; set;}, such an approach may not be previously defined field, as the compiler will automatically generate a field to help you
  • Write code in a property accessor, do not write the statement reads attribute in Get accessor, do not write START statement of the property within the Set accessor, otherwise, there will be a recursive cycle, the cycle of death

Method parameters

First, the argument between Form

  • Parameter: called "formal parameters" are parameters used in the definition of the function names and function of the body when the object is used to receive the transmission parameters when invoked
  • Argument, called the "actual parameter" is a parameter passed when calling the function, that is passed to the called function, the arguments can be constants, variables, expressions, functions, etc., regardless of what type of argument amount, during function calls, they must have a value determined so as to transfer the values ​​to the parameters. Therefore it should be pre-assigned using input arguments and other ways to make determination value is obtained
//类中定义一个交换方法
public void Swap(int x, int y)//x,y为形参
{
	int temp = x;
    x = y;
    y = temp;
}
//主函数调用
MathTool myMath = new MathTool();
int a = 10int b = 20;
myMath.Swap(a,b);//实参

Second, the reference parameter ref

MathTool myMath = new MathTool();
int a = 10int b = 20;
myMath.Swap(a,b);
Console.WriteLine(a);
Console.WriteLine(b);
  • The above code performs a and b and the output value does not change, just as a and b corresponding to the value assigned to x and y, they themselves had not changed, so in this case it is convenient with the reference parameter ref
public void Swap(ref int x, ref int y)
{
    int temp = x;
    x = y;
    y = temp;
}
//调用
myMath.Swap(ref a,ref b);
  • In this case again the statement is executed, a and b values ​​are swapped, at this time because the value is not passed, but addresses a and b
  • If no initial value, then there is no address, so the ref parameter must be a variable argument ⼀ had given the initial value is assigned
  • ref shoots as usual increase in the value of the type parameter before ⾯

Third, the output parameter out

  • When a method would like to return a plurality of result out of available output parameters
//在类中定义方法
public void Cal(int a,int b,out int r1,out int r2)
{
	r1 = a + b;
	r2 = a - b;
}
//主函数中调用
MathTool ml = new MathTool();
int result1;
int result2;
ml.Cal(3, 5, out result1, out result2);
//result1为8,result2为-2
  • Add a keyword parameters will be out ⼀ channel output
  • Parameter must be assigned before leaving Remedies
  • Argument must be a variable ⼀
  • Transfer shoots as usual argument is a value type
  • Using the output parameter, no matter if parameter or argument shall be added before ⾯ out Keywords

Fourth, the array parameter parmas

  • When the incoming parameters, you can use an array of parameter uncertainty is when several parmas
public void Sum(params int[] a)
{
    int result = 0;
    foreach (int x in a)
    {
        result += x;
    }
    Console.WriteLine("结果为: " + result);
}
myMath.Sum(1, 2, 3, 4, 5); 
  • Array parameters can only be used once, and the last to put the argument list

Fifth, the default value of the parameter

  • When a parameter in most cases is a value, you can add a default value of the parameter
/// <summary>
/// 获取你的年级
/// </summary>
/// <returns>返回你的年级.</returns>
/// <param name="yourAge">你的年龄.</param>
/// <param name="jumpCount">跳过几级.</param>
public int GetYourGrade(int yourAge, int jumpCount = 0)
{
    int grade = yourAge - 6 + jumpCount;
    grade = grade > 0 && grade < 13 ? grade : -1;
    return grade;
} 
Published 11 original articles · won praise 1 · views 437

Guess you like

Origin blog.csdn.net/weixin_43914767/article/details/104414834