zoukankan      html  css  js  c++  java
  • C#代码示例_函数

    参数数组

    C#允许为函数指定一个(只能指定一个)特定的参数,这个参数必须是函数定义中的最后一个参数,称为参数数组。参数数组可以使用个数不定的参数调用函数,可以使用params关键字定义它们。

    参数数组可以简化代码,因为不必从调用代码中传递数组,而是传递同类型的几个参数,这些参数放在可在函数中使用的一个数组中。

    参数的数量不受限制,可以为0个。

     

    引用参数

    用作ref参数的变量有两个限制。首先,函数可能会改变引用参数的值,所以必须在函数调用中使用“非常量”变量。

    其次,必须使用初始化过的变量。C#不允许假定ref参数在使用它的函数中初始化。

     

    输出参数

    除了按引用传递值之外,还可以使用out关键字,指定所给的参数是一个输出参数。out关键字的使用方式与ref关键字相同。实际上,它的执行方式与引用参数完全一样,因为在函数执行完毕后,该参数的值将返回给函数调用中使用的变量。但是,存在一些重要区别。

    l  把未赋值的变量用作ref参数是非法的,但可以把未赋值的变量用作out参数。

    l 在函数使用out参数时,out参数必须看做是还未赋值。

    即调用代码可以把已赋值的变量用作out参数,存储在该变量中的值会再函数执行时丢失。

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace ConsoleApplication1
     8 {
     9     class Program
    10     {
    11         //parameter array - params
    12         static int SumVals(params int[] vals)
    13         {
    14             int sum = 0;
    15             foreach (int val in vals)
    16             {
    17                 sum += val;
    18             }
    19             return sum;
    20         }
    21 
    22         //reference parameter - ref
    23         static void ShowDouble(ref int val)
    24         {
    25             val *= 2;
    26             Console.WriteLine("val doubled = {0}",val);
    27         }
    28 
    29         //out parameter - out
    30         static int MaxValue(int[] intArray, out int maxIndex)
    31         {
    32             int maxVal = intArray[0];
    33             maxIndex = 0;
    34             for (int i = 1; i < intArray.Length; i++)
    35             {
    36                 if (intArray[i] > maxVal)
    37                 {
    38                     maxVal = intArray[i];
    39                     maxIndex = i;
    40                 }
    41             }
    42             return maxVal;
    43         }
    44 
    45 
    46         static void Main(string[] args)
    47         {
    48             int sum = SumVals(1, 4, 6, 9, 2);
    49             Console.WriteLine("Summed Values = {0}",sum);
    50 
    51             int myNum = 5;
    52             Console.WriteLine("myNum = {0}",myNum);
    53             ShowDouble(ref myNum);
    54             Console.WriteLine("myNum = {0}", myNum);
    55 
    56             int[] myArray = { 1,42,78,26,0,29};
    57             int maxIndex;
    58             Console.WriteLine("The maximum value in myArray is {0}", MaxValue(myArray,out maxIndex));
    59             Console.WriteLine("The first occurence of this value is at element {0}",maxIndex+1);
    60 
    61             
    62 
    63             Console.ReadLine();
    64 
    65         }
    66     }
    67 }

     

    Main()函数

    这个函数可以返回void或int,又一个可选参数string[] args。Main()函数可以使用如下4种版本:

    static void Main()

    static void Main(string[] args)

    static int Main()

    static int Main(string[] args)

    上面的第三、四个版本返回一个int值,它们可以用于表示应用程序如何终止,通常用作一种错误提示(但这不是强制的),一般情况下,返回0反映了“正常”的终止(即应用程序执行完毕,并安全地终止)。

    Main()的可选参数args是从应用程序的外部接受信息的方法,这些信息在运行期间指定,其形式是命令行参数。

    指定Main()参数: 

    1)打开项目的属性页面(在Solution Explorer窗口中右击项目名称,选择Properties选项)。

    2)选择Debug页面,在Command Line Arguments设置中天街所希望的命令行参数。

     

    结构函数

    结构可以包含函数和数据。

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace ConsoleApplication1
     8 {
     9     class Program
    10     {
    11         struct customername
    12         {
    13             public string firstName, lastName;
    14             public string Name()
    15             {
    16                 return firstName + " " + lastName;
    17             }
    18         }
    19 
    20         static void Main(string[] args)
    21         {
    22             customername myCustomer;
    23             myCustomer.firstName = "Steven";
    24             myCustomer.lastName = "Wang";
    25             Console.WriteLine(myCustomer.Name());
    26        
    27 
    28             Console.ReadLine();
    29 
    30         }
    31     }
    32 }

     

    委托(delegate)是一种可以把引用存储为函数的类型。委托的声明非常类似于函数,但不带函数体,且要使用delegate关键字。委托的声明制定了一个返回类型和一个参数列表。

    在定义了委托后,就可以声明该委托类型的变量。接着把这个变量初始化为与委托有相同返回类型和参数列表的函数引用。之后,就可以使用委托变量调用这个函数,就像该变量是一个函数一样。

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace ConsoleApplication1
     8 {
     9     class Program
    10     {
    11         delegate double ProcessDelegate(double param1, double param2);
    12 
    13         static double Multiple(double param1, double param2)
    14         {
    15             return param1 * param2;
    16         }
    17 
    18         static double Divide(double param1, double param2)
    19         {
    20             return param1 / param2;
    21         }
    22 
    23         static void ExecuteFunction(ProcessDelegate process)
    24         {
    25             Console.WriteLine(process(20, 4));
    26         }
    27 
    28         static void Main(string[] args)
    29         {
    30             ProcessDelegate process;
    31             Console.WriteLine("Enter 2 numbers separated with a comma:");
    32             string input = Console.ReadLine();
    33             int commaPos = input.IndexOf(',');
    34             double param1 = Convert.ToDouble(input.Substring(0,commaPos));
    35             double param2 = Convert.ToDouble(input.Substring(commaPos+1,input.Length-commaPos-1));
    36             Console.WriteLine("Enter M to multiple or D to divide:");
    37             input=Console.ReadLine();
    38             if(input.ToUpper()=="M")
    39             {
    40                 process=new ProcessDelegate(Multiple);
    41                // process = Multiple;       // or you can write this
    42             }
    43             else
    44             {
    45                 process=new ProcessDelegate(Divide);
    46                // process = Divide;         // or you can write this
    47             }
    48             Console.WriteLine("Result: {0}",process(param1,param2));
    49 
    50             ExecuteFunction(process);
    51 
    52             Console.ReadLine();
    53 
    54         }
    55     }
    56 }

    要把一个函数引用赋给委托变量,必须使用new关键字创建一个新委托。在这个关键字的后面,指定委托类型,提供一个引用所需函数的参数。参数是要使用的函数名,且不带括号。

     

  • 相关阅读:
    The Future of Middleware and the BizTalk Roadmap
    FW: How to spawn a process that runs under the context of the impersonated user in Microsoft ASP.NET pages
    Strips illegal Xml characters
    luogu P2280 激光炸弹(二维前缀和)
    luogu P2704 炮兵阵地(经典状态压缩DP)
    SP1716 GSS3 Can you answer these queries III (线段树维护最大连续子段和)
    二分图判定、匹配问题
    C++语法综合 | 基于char*设计一个字符串类MyString
    luogu P1044 火车进出栈问题(Catalan数)
    C++设计模式 | 三种设计模式基础
  • 原文地址:https://www.cnblogs.com/anything-but-ordinary/p/3537949.html
Copyright © 2011-2022 走看看