C# 学习之函数
2014-03-20 19:37
381 查看
C#学习之函数总结
1:函数结构同C++,java类似,
<修饰符> <返回类型> <函数名字> (参数列表)
{//函数体
}
2:参数中的特性--参数数组
C#中允许函数中指定一个特定的参数,这个参数必须是函数定义中的最后一个参数,毫无疑问这些参数必须是同类型的,这样的参数叫做参数数组
格式: <modify> <return type> <functionName>(type1,typ2,...paras typen[] <paraName>)
如:
3: 参数的类型,ref
C#中有两种类型的参数--传值参数,传引用参数,其中传引用参数在调用的时候需要加上一个关键词 ref,这个和C++还是有区别的
C#--
C++--
4: 输出参数 out
这个是C++中没有的,不过在HLSL中有类似的语法,out是一个关键字,其用法同ref,作用也类似ref,但是跟ref相比,它的不同之处是:在传入时候,可以未赋值.
5: Main函数
C#中的main函数格式
static void Main();
static void Main(string[] args);
static int Main();
static int Main(string[] args);
6: 委托特征
委托是C#相对C++的一种新的语法,委托类似于C/C++中的函数指针,但是相比指针,它更安全和强大。
委托的特点:
<1> 委托允许作为函数参数进行传递
<2> 委托可用于定义回调方法
<3> 委托可以连接在一起,对同一事件调用多个响应方法。
7:委托使用
8: 委托的实例化
<1> named method : del fp = funcName;
<2> anonymous method del fp = delegate(string str){//...};
<3> Lamda expression del fp = (x)->{//.....};
1:函数结构同C++,java类似,
<修饰符> <返回类型> <函数名字> (参数列表)
{//函数体
}
2:参数中的特性--参数数组
C#中允许函数中指定一个特定的参数,这个参数必须是函数定义中的最后一个参数,毫无疑问这些参数必须是同类型的,这样的参数叫做参数数组
格式: <modify> <return type> <functionName>(type1,typ2,...paras typen[] <paraName>)
如:
static int sumAdd(paras int[] datas) { int sum = 0; foreach(int x in datas) sum += x; return sum; } static void main() { sunAdd(1,2,3,4,5); }
3: 参数的类型,ref
C#中有两种类型的参数--传值参数,传引用参数,其中传引用参数在调用的时候需要加上一个关键词 ref,这个和C++还是有区别的
C#--
static void Main(string[] args) { Console.WriteLine("I love C&&"); int val = 10; Console.WriteLine("val's value is:{0}",val); changeValue(ref val); Console.WriteLine("val's value is:{0}",val); } static int changeValue(ref int val) { return val *= 2; }
C++--
void changeValue(int &val) { val *= 2; } void main() { int val = 10; cout<<"val's value is:"<<val<<endl; changeValue(val); cout<<"val's value is:"<<val<<endl; }
4: 输出参数 out
这个是C++中没有的,不过在HLSL中有类似的语法,out是一个关键字,其用法同ref,作用也类似ref,但是跟ref相比,它的不同之处是:在传入时候,可以未赋值.
static void outChange(int x, out int y) { y = 2 * x; } static void Main(string[] args) { int x = 100; int y; outChange(x,out y); Console.WriteLine("out y's value is:{0}",y); }
5: Main函数
C#中的main函数格式
static void Main();
static void Main(string[] args);
static int Main();
static int Main(string[] args);
6: 委托特征
委托是C#相对C++的一种新的语法,委托类似于C/C++中的函数指针,但是相比指针,它更安全和强大。
委托的特点:
<1> 委托允许作为函数参数进行传递
<2> 委托可用于定义回调方法
<3> 委托可以连接在一起,对同一事件调用多个响应方法。
7:委托使用
namespace ConsoleApplication1 { class Program { public delegate void delFunc(string str); // 委托的声明 public static void testDelOne(string str) //定义委托对应的函数1 { Console.WriteLine("This is delegate one:" + str); } public static void testDelTwo(string str) //定义委托对应的函数2 { Console.WriteLine("This is delegate two:" + str); } public static void testDelThree(string str) //定义委托对应的函数3 { Console.WriteLine("This is delegate three:" + str); } static void Main(string[] args) { Console.WriteLine("**************Test delegate use like C function pointer***************"); delFunc fp = testDelOne; fp(" in 1.1"); delFunc xxpp = new delFunc(testDelThree); xxpp("in 1.3"); Console.WriteLine("****************************Test delegate +=**************************"); fp += testDelTwo; fp(" in 2"); Console.WriteLine("****************************Test delegate + **************************"); delFunc fpp = testDelThree; //delFunc fpAll = fp + fpp; delFunc fpAll = fp + testDelThree; fpAll(" in 3"); Console.WriteLine("****************************Test delegate -=*************************"); fpp -= testDelTwo; fpp("in fpp 2"); fpAll -= testDelTwo; fpAll("in fpAll2"); Console.WriteLine("****************************Test delegate -**************************"); fpAll = fpAll - testDelOne; fpAll(" in last"); } } }
8: 委托的实例化
delegate void del(string str); void funcName(string str){//....}
<1> named method : del fp = funcName;
<2> anonymous method del fp = delegate(string str){//...};
<3> Lamda expression del fp = (x)->{//.....};
相关文章推荐
- c#调用COM组件
- Mootools 1.2教程 函数
- autoit InputBox 函数
- 文件遍历排序函数
- c#中虚函数的相关使用方法
- Oracle 函数大全[字符串函数,数学函数,日期函数]第1/4页
- ASP下经常用的字符串等函数参考资料
- 浅析C# web访问mysql数据库-整理归纳总结
- asp Chr 函数 数字转字母的方法
- C# IP地址与整数之间转换的具体方法
- C#中的多态深入理解
- C#实现的阴历阳历互相转化类实例
- C# 的关键字详细介绍
- c# 获得局域网主机列表实例
- C#基本概念列举详解
- C#中Convert.ToInt32()和int.Parse()的区别介绍
- C#面向对象特征的具体实现及作用详解
- c#静态方法和非静态方法详细介绍
- 关于C# Math 处理奇进偶不进的实现代码
- 探索PowerShell(十一)函数介绍