一周学C#之第二天(函数)

 

一周学C#_第二天(共分六个部分)

函数

1          前言

C#不支持——全局函数

所有的函数必须——在类内部声明

无——源文件和头文件之分

所有的函数必须——声明的时候被实现

int NotAllowed()//错误,C#没有全局函数

{

       ...

}

sealed class Methods

{

       void Inline()

       {

              ...

       }

       void Error()

       { ...

       };//错误,函数不能有结尾分号

       int AlsoError();//错误,函数必须声明的时候被实现

}

C#规定,所有的函数必须在【类或结构】内实现。

函数是类或结构的成员,函数也被称为方法。

记住,C#不允许在函数的声明时,加入结尾分号。

2          声明函数

函数参数列表

各参数以逗号隔开

参数必须命名

没有参数时括号不能省略

sealed class Methods

{

       void Error(float)//错误,参数没命名

       {...}

       void NoError(float delta)

       {...}

       int Error(void)//错误,无参数时不允许使用void

       {...}

       int NoError()

       {...}

}

3          值型参数

一般的函数参数是实参的一个拷贝

实参必须预先被赋值

实参可以是常量类型

sealed class ParameterPassing

{

       static void Method(int parameter)

       {

              parameter=42;

       }

       static void Main()

       {

              int arg=0;

              Console.Write(arg);//结果为0

              Method(arg);

              Console.Write(arg);//结果为0

       }

}

注意:没有【被ref】或者【out】修饰的参数,都是值型参数。

值型参数,只有在该参数所属的函数被调用之时才存在,并且用调用时所传递的实参的值来进行初始化。

当函数调用结束时,值型参数不复存在。

4          引用型参数

引用型参数是实参的一个别名

没有发生复制动作

实参必须预先被赋值

实参必须是一个变量类型(值型参数可以不是)

实参和函数参数都要有ref

sealed class ParameterPassing

{

       static void Method(ref int parameter)

       {

              parameter=42;

       }

       static void Main()

       {

              int arg=0;

              Console.Write(arg);//结果是0

              Method(ref arg);

              Console.Write(arg);//结果为42

       }

}

ref修饰符,才被称为引用型参数。

引用型参数不产生新的存储区间。

引用型参数实际上,只是实参所代表变量的另一个名字。

ref必须同时出现中函数声明语句和函数调用语句中。

引用型参数,需要给实参预先赋值;并且实参必须是变量类型。

5          out型参数

out型参数——是实参的一个别名

没有发生复制

实参不必预先赋值

实参必须是变量类型

函数参数必须被预先赋值才能使用

实参和函数参数都要有out

sealed class ParameterPassing

{

       static void Method(out int parameter)

       {

              parameter=44;

       }

       static void Main()

       {

              int arg;

              Method(out arg);

              Console.Write(arg);//结果为44

       }

}

6          in型参数

readonlyconstin——都是C#关键字

它们不能被用于函数参数

ref/out型参数总是被赋予写的权力

7          函数重载

一个类中的函数可以有相同的名字,这被称为重载

函数名和参数——称为标识

标识必须唯一

返回值类型不是标识

namespace System

{

       public sealed class Console

       {

              public static void WriteLine()

              {...}

              public static void WriteLine(int value)

              {...}

       ...

       }

}

注意:重载函数签名式差异:体现在参数上,而不是返回值

8          ref/out重载

ref/out在大部分情况下是标识的一部分

你可以重载一个ref型参数和一个普通参数

你可以重载一个out型参数和一个普通参数

你不可以重载一个ref型参数和一个out型参数

sealed class Overloading

{

       void Allowed(int parameter){...}

       void Allowed(ref int parameter){...}//正确,重载一个ref型和一个普通的

 

       void AlsoAllowed(int parameter){...}

       void AlsoAllowed(out int parameter){...}//正确,一个out型和一个普通的

 

       void NotAllowed(ref int parameter){}

       void NotAllowed(out int parameter){}//错误,不能重载一个ref型和一个out

}

9          访问规则

函数参数或返回值不能比所属函数的访问级别低

sealed class T{...}//此类的默认访问级别是internal

public sealed class Bad

{

       public void Parameter(T t)//错误,函数的访问级别是public,比参数高了

       {...}

       public T Return()//错误,函数的访问级别比返回值高

       {...}

}

public sealed class Good

{

       private void Parameter(T t)//正确,函数访问级别是private,比参数低

       {...}

       private T Return()//正确,函数访问级别比返回值低

       {...}

}

10      找错误

sealed class Buggy

{

       void Defaulted(double d=0.0)//错误,C#不能有缺省参数

       {...}

 

       void ReadOnly(const ref Wibble w)//错误,ref型参数不能用const修饰,因为ref型参数是可能变化的

       {...}

 

       ref int Return Type()//错误,ref型和out型只能用于函数参数和实参

       {...}

       ref int fieldModifier;//错误同上

}

C#中实现【缺省参数】可以通过【函数重载】的方法,如下:

sealed class Overload

{

       void DefaultArgument(){DefaultArgument(0.0);}

       void DefaultArgument(double d){...}

}

 

 

 

 

原文地址:https://www.cnblogs.com/lizunicon/p/1381289.html