zoukankan      html  css  js  c++  java
  • C#基础入门 四

    C#基础入门 四

    方法参数

    • 值参数:不附加任何修饰符;
    • 输出参数:以out修饰符声明,可以返回一个或多个给调用者;
      • 如果想要一个方法返回多个值,可以用输出参数来处理,输出参数由out关键字标识,如static void Car(out int x,out int y,int z){},与引用参数区别在于:调用方法前无需对输出参数进行初始化,输出型参数用于传递方法返回的数值。
      • 计算矩形面积的方法:(图8)
    static void rectangle(int length,int width, out int rec)
            {
                rec = length * width;
            }
            public static void Main(string[] args)
            {
                int a = 10;
                int b = 5;
                int r;
                rectangle(a, b, out r);
                Console.WriteLine("矩形长为"+a);
                Console.WriteLine("矩形宽为"+ b);
                Console.WriteLine( "面积为" + r);
            }
    
    • 如果函数有return,输出函数返回值(图9),这样就可以实现一个函数有多个返回值。
     static int rectangle(int length,int width, out int rec)
            {
                rec = length * width;
                return length + 1;
            }
            public static void Main(string[] args)
            {
                int a = 10;
                int b = 5;
                int r;
                int c=rectangle(a, b, out r);
                Console.WriteLine("矩形长为"+a);
                Console.WriteLine("矩形宽为"+ b);
                Console.WriteLine( "面积为" + r+"返回值"+c);
    
            }
    
    • 引用参数:以ref修饰符声明;
      • 引用参数并不创建新的存储位置,相反,引用参数表示的存储位置恰是在方法调用中作为参数给出的那个变量所表示的存储位置,当利用引用参数想方法传递形参时,编译程序将把实际值在内存中的地址传递给方法。
    • 数组参数:以params修饰符声明。
      • 如果形参表中包含了数组型参数,那么它必须在参数表中位于最后,而且必须是一维数组类型,另外,数组型参数不可能将params修饰符与ref和out修饰符组合起来使用。
      • 数组参数长度可变,可为0,一次性,只能使用一次,而且要放到最后。
      • foreach 语句为数组或对象集合中的每个元素重复一个嵌入语句组。foreach 语句用于循环访问集合以获取所需信息,但不应用于更改集合内容以避免产生不可预知的副作用。
      • foreach(数据类型 标识符 in 表达式){循环体}
      • 计算{1,2,3,4}数组内元素的平方结果如下图所示(图10)
    static void Square(params int[] s)
            {
                foreach (int g in s)
                {
                    int radius = g;
                    int square = radius * radius;
                    Console.WriteLine(square);
                }
            }
            public static void Main(string[] args)
            {
                int[] arr = { 1, 2, 3, 4 };
                Square(arr);
            }
    

    练习题

    求两个数的最大值

    static void Max(int x,int y, out int max)
            {
                if (x > y) max = x;
                else max = y;
                Console.WriteLine("max="+max);
            }
            public static void Main(string[] args)
            {
                int m;
                Console.WriteLine("请输入a的值:");
                int a = Convert.ToInt16(Console.ReadLine());
                Console.WriteLine("请输入b的值:");
                int b = Convert.ToInt16(Console.ReadLine());
                Max(a, b, out m);
    
            }
    
    • 结果如下图所示:(图11)

    重载

    • 方法重载实际上是函数名重载,即支持多个不同的方法采用同一名字。
    • 实现方法的重载必须满足下列条件之一:一是参数表中对应的参数类型不同;二是参数表中参数个数不同。
    • 方法重载只跟参数有关,跟返回值无关。图12
     static int Sum(int x,int y)
            {
                return x + y;
            }
            static float Sum(float x,float y)
            {
                return x+y;
            }
            static double Sum(int x,float y,double z){
                return x+y+z;
            }
            public static void Main(string[] args)
            {
                int a = 10, b = 20;
                float c = 10.5f, d = 34f;
                double e = 40;
                Console.WriteLine(Sum(a, b));
                Console.WriteLine(Sum(c, d));
                Console.WriteLine(Sum(a,c,e));
    
            }
    
    • 如果形参中存在两个以上的形参类型存在隐式转换关系,则可能产生二义性
    static double print(int i,double j){}
    static double print(double i,int j){}
    static void Main(string[] args)
    {
    	double x=print(5,5);//二义性
    }
    
  • 相关阅读:
    双六
    除法取模
    欧拉函数及费马小定理
    基础模运算
    Leeetcode--581. Shortest Unsorted Continuous Subarray
    Codeforces Round #541--1131F. Asya And Kittens(基础并查集)
    leetcode--200. Number of Islands
    leetcode--21. Merge Two Sorted Lists
    leetcode--155. Min Stack
    Codeforces Round #539--1113B
  • 原文地址:https://www.cnblogs.com/senlinmilelu/p/8445654.html
Copyright © 2011-2022 走看看