• C#和.Ne学习第七天


    1、我们在Main()函数中,调用Test()函数,我们管Main()函数称之为调用者,
    管Test()函数称之为被调用者。
    如果被调用者想要得到调用者的值:
    1)、传递参数。
    2)、使用静态字段来模拟全局变量。
    如果调用者想要得到被调用者的值:
    1)、返回值

    2、
    不管是实参还是形参,都是在内存中开辟了空间的。

    3、方法的功能一定要单一。
    GetMax(int n1,int n2)
    方法中最忌讳的就是出现提示用户输入的字眼。

    4、out、ref、params
    1)、out参数。
    如果你在一个方法中,返回多个相同类型的值的时候,可以考虑返回一个数组。
    但是,如果返回多个不同类型的值的时候,返回数组就不行了,那么这个时候,
    我们可以考虑使用out参数。
    out参数就侧重于在一个方法中可以返回多个不同类型的值。

    2)、ref参数
    能够将一个变量带入一个方法中进行改变,改变完成后,再讲改变后的值带出方法。
    ref参数要求在方法外必须为其赋值,而方法内可以不赋值。

    out和ref的区别:
    out和ref都传地址的,但是out只传出地址,传入地址是没有初始化的面ref则是传入了已经初始化的地址,并且付出也是这个地址,所以ref可以传入也可以付出有意义的数据,但是out只能传出。
    out参数的函数调用可以当作被传入变量的初始化,即调用之前,变量可以不必初始化,而是ref参数的函数调用的话,在调用 之前,变量一定要初始化。

    3)、params可变参数
    将实参列表中跟可变参数数组类型一致的元素都当做数组的元素去处理。
    params可变参数必须是形参列表中的最后一个元素。
    一个形参中只能存在一个params参数

    实例:

     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         static void Main(string[] args)
    12         {
    13             int sum=0;
    14             GetSum(ref sum, 1,2,3,4,5,6);
    15             Console.WriteLine(sum);
    16             Console.ReadKey();
    17         }
    18         /// <summary>
    19         /// 取得任意整型的和
    20         /// </summary>
    21         /// <param name="sum">用于保存数组的和</param>
    22         /// <param name="a">用于保存每个要加的整型值</param>
    23         public static void GetSum(ref int sum, params int[] a)
    24         {
    25             for (int t = 0; t < a.Length; ++t)
    26             {
    27                 sum += a[t];
    28             }
    29 
    30                 return;
    31         }
    32     }
    33 }
    View Code

    题目:

    提示用户输入两个数字,计算这两个数字之前所有整数的和
    * 1、用户只能输入数字
    * 2、计算两个数字之前和
    * 3、要求第一个数字必须比第二个数字小 否则就重新输入

     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         static void Main(string[] args)
    12         {
    13             /*
    14              * 提示用户输入两个数字,计算这两个数字之前所有整数的和
    15              * 1、用户只能输入数字
    16              * 2、计算两个数字之前和
    17              * 3、要求第一个数字必须比第二个数字小   否则就重新输入
    18              */
    19             int iNumberOne;
    20             int iNumberTwo;
    21 
    22             Console.Write("请输入第一个数字:");
    23             iNumberOne = GetNumber(Console.ReadLine());
    24             Console.Write("请输入第二个数字:");
    25             iNumberTwo = GetNumber(Console.ReadLine());
    26             Program.JudgeNumber(ref iNumberOne, ref iNumberTwo);
    27             int sum = Sum(iNumberOne, iNumberTwo);
    28 
    29             Console.WriteLine("1:{0}
    2:{1}", iNumberOne, iNumberTwo);
    30             Console.WriteLine("和为:"+sum);
    31 
    32             Console.ReadKey();
    33         }    
    34         /// <summary>
    35         /// 判断是否为数字
    36         /// 如果是返回数字
    37         /// 否则提示输入错误并重新输入
    38         /// </summary>
    39         /// <param name="str">接收用户输入</param>
    40         /// <returns></returns>
    41         public static int GetNumber(string str)
    42         {
    43             while (true)
    44             {
    45                 try
    46                 {
    47                     int numbert = Convert.ToInt32(str);
    48                     return numbert;
    49                 }
    50                 catch
    51                 {
    52                     Console.WriteLine("输入错误,请重新输入:");
    53                     str = Console.ReadLine();
    54                 }
    55             }
    56         }
    57         /// <summary>
    58         /// 断送第一个变量是否比第二个变量小,
    59         /// 如果是跳出方法
    60         /// 否则重新输入
    61         /// </summary>
    62         /// <param name="i">第一个变量</param>
    63         /// <param name="j">第二个变量</param>
    64         public static void JudgeNumber(ref int i, ref int j)
    65         {
    66             while (true)
    67             {
    68                 if (i < j)
    69                 {
    70                     return;
    71                 }
    72                 else
    73                 {
    74                     Console.Write("第一个数字不能大于或者等于第二个数字,请重新输入第一个数字:");
    75                     i = GetNumber(Console.ReadLine());
    76                     Console.Write("第一个数字不能大于或者等于第二个数字,请重新输入第二个数字:");
    77                     j = GetNumber(Console.ReadLine());
    78                 }
    79             }
    80         }
    81         /// <summary>
    82         /// 获得两个数之间的和
    83         /// </summary>
    84         /// <param name="i">第一个数</param>
    85         /// <param name="j">第二个数</param>
    86         /// <returns></returns>
    87         public static int Sum(int i, int j)
    88         {
    89             int sum = 0;
    90             for (int t = i; t <= j; ++t)
    91             {
    92                 sum += t;
    93             }
    94             return sum;
    95         }
    96     }
    97 }
    View Code

    5、方法的重载
    概念:方法的重载指的就是方法的名称相同给,但是参数不同。
    参数不同,分为两种情况
    1)、如果参数的个数相同,那么参数的类型就不能相同。
    2)、如果参数的类型相同,那么参数的个数就不能相同。
    ***方法的重载跟返回值没有关系。

    6、方法的递归
    方法自己调用自己。
    常用于找出一个文件夹中所有的文件。

    实例:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication3
    {
        class Program
        {
            static void Main(string[] args)
            {
                Program.Test(0);
    
                Console.ReadKey();
            }
            /// <summary>
            /// 递归输出
            /// </summary>
            /// <param name="i">控制递归输出的个数</param>
            public static void Test(int i)
            {
                if (i == 10)
                {
                    return;
                }
                Console.WriteLine("从前有坐山");
                Console.WriteLine("山里有从庙");
                Console.WriteLine("庙里有坐和尚");
                ++i;
                Test(i);
            }
        }
    }
    总结:
    这样写虽然也行但有一个缺点,
    每次递归都会声明并赋值一个变量,
    当需要大量方法调用自身时每次递归都要占用一个int内存空间就会大量占用内存。
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication3
    {
        class Program
        {
            public static int a = 0;
            static void Main(string[] args)
            {
                Program.Test();
    
                Console.ReadKey();
            }
            /// <summary>
            /// 递归输出
            /// </summary>
            /// <param name="i">控制递归输出的个数</param>
            public static void Test()
            {
                if (a == 10)
                {
                    return;
                }
                Console.WriteLine("从前有坐山");
                Console.WriteLine("山里有从庙");
                Console.WriteLine("庙里有坐和尚");
                ++a;
                Test();
            }
        }
    }
    总结:
    和上面的程序主要思想都是一样的,但这里把控制递归变量的通过静态变量模拟成全局变量,
    这样就不会重复声明int类型的变量大量的浪费内存空间。
    View Code
  • 相关阅读:
    买房的贷款时间是否是越长越好?https://www.zhihu.com/question/20842791
    asp.net cookie and session
    leelazero and google colab
    download file by python in google colab
    physical processor, core, logical processor
    通过powershell操作eventlog
    openxml in sql server
    get the page name from url
    How to Execute Page_Load() in Page's Base Class?
    Difference between HttpContext.Request and Request
  • 原文地址:https://www.cnblogs.com/2016Study/p/5474334.html
Copyright © 2020-2023  润新知