美文网首页
.Net基础07

.Net基础07

作者: Nicole__Zhang | 来源:发表于2017-05-16 11:09 被阅读13次

方法的调用

namespace _01方法的调用
{
    class Program
    {
        //字段 属于类的字段 (C#中没有全局变量这一说)
        public static int _number = 10;//使用静态变量模拟 全局变量
        static void Main(string[] args)
        {
            int a = 3;
            int res = Test(a);//传参 a是实参
            Console.WriteLine(res);
            Console.WriteLine(a); //输出 3
            Console.WriteLine(_number);  //输出 10
            Console.ReadKey();
        }
        public static int Test(int a)  //a是形参
        {
            Console.WriteLine(_number); //输出 10
            a = a + 5;
            return a;
        }
        public static void TestTwo() //该方法未调用
        {
            Console.WriteLine(_number); //说明_number 可以被访问到
        }
    }
}
结果.png

我们在Main()函数中,调用Test()函数,我们管Main()函数称之为调用者,管Test()函数称之为被调用者。

  • 如果被调用者想要得到调用者的值:
    1)、传递参数。
    2)、使用静态字段来模拟全局变量。

  • 如果调用者想要得到被调用者的值:
    1)、返回值

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

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

  • 小练习

static void Main(string[] args)
{
    //写一个方法 判断一个年份是否是闰年
    bool result = isRunNian(Convert.ToInt32(Console.ReadLine()));
    Console.WriteLine(result);
    Console.ReadKey();
}

/// <summary>
/// 判断给定的年份是否是闰年
/// </summary>
/// <param name="year">年份</param>
/// <returns>结果</returns>
public static bool isRunNian(int year)
{
    bool b = (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
    return b;
}
结果.png

out参数

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _03out参数
{
    class Program
    {
        static void Main(string[] args)
        {
            //写一个方法 求一个整数数组中的 最大值最小值 总和 平均值
            int[] numbers = {1,3,4,5,74,43,23,21,5};

            int[] res = GetMaxMinSumAvg(numbers);
            Console.WriteLine("最大值{0},最小值{1},总和{2},平均值{3}", res[0], res[1], res[2], res[3]);

            int max = 0;
            int min = 0;
            int sum = 0;
            int avg = 0;
            Test(numbers, out max, out min, out sum, out avg);
            Console.WriteLine("使用了out 参数 最大值{0},最小值{1},总和{2},平均值{3}", max, min, sum, avg);

            Console.ReadKey();
        }

        /// <summary>
        /// 求一个 数组中的 最大值最小值 总和 平均值
        /// </summary>
        /// <param name="nums">最大值最小值 总和 平均值 一次排列的数组</param>
        /// <returns>结果数组</returns>
        public static int[] GetMaxMinSumAvg(int[] nums)
        {
            int[] result = new int[4];
            //假设 res[0] 最大值 res[1] 最小值 res[2] 总和 res[3] 平局值
            result[0] = nums[0];
            result[1] = nums[0];
            result[2] = 0;

            for (int i = 0; i < nums.Length; i++)
            {
                if (nums[i] > result[0])
                {
                    result[0] = nums[i];
                }
                if (nums[i] < result[1])
                {
                    result[1] = nums[i];
                }
                result[2] += nums[i];
            }
            result[3] = result[2] / nums.Length;

            return result;    
        }

        /// <summary>
        /// 求一个整数数组中的 最大值最小值 总和 平均值
        /// </summary>
        /// <param name="nums">要求值的数组</param>
        /// <param name="max">多余返回的最大值</param>
        /// <param name="min">多余返回的最小值</param>
        /// <param name="sum">多余返回的总和</param>
        /// <param name="avg">多余返回的平均值</param>
        public static void Test(int[] nums, out int max, out int min, out int sum, out int avg)
        {
            //out 参数要求在方法的内部必须为其赋值
            max = nums[0];
            min = nums[0];
            sum = 0;

            for (int i = 0; i < nums.Length; i++)
            {
                if (nums[i] > max)
                {
                    max = nums[i];
                }
                if (nums[i] < min)
                {
                    min = nums[i];
                }
                sum += nums[i];
            }
            avg = sum / nums.Length;
        }
    }
}
结果.png

ref参数

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

例子.png 运行结果.png
  • 小练习
class Program
{
    static void Main(string[] args)
    {
        //使用方法来交换两个 int类型的变量
        int a = 10;
        int b = 30;
        ExChange(ref a, ref b);

        Console.WriteLine("a = {0}, b = {1}",a ,b);
        Console.ReadKey();
        
    }
    public static void ExChange(ref int a, ref int b)
    {
        int temp = a;
        a = b;
        b = temp;
    }
}
结果.png

params可变参数

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

例子.png

方法的重载

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

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(M(10, 20));
        Console.WriteLine(M(10, 20, 30));
        Console.WriteLine(M(2.0, 4.6));
        Console.WriteLine(M("WQE", "DF"));

        Console.ReadKey();
    }
    public static int M(int n1, int n2)
    {
        return n1 + n2;
    }
    public static int M(int n1, int n2, int n3)
    {
        return n1 + n2 + n3;
    }
    public static double M(double n1, double n2)
    {
        return n1 + n2;
    }
    public static string M(string s1, string s2)
    {
        return s1 + s2;
    }
结果.png

往期回顾

图文无关.png

相关文章

  • .Net基础07

    方法的调用 我们在Main()函数中,调用Test()函数,我们管Main()函数称之为调用者,管Test()函数...

  • 用hexo搭建的博客

    用的参考资料链接 http://hifor.net/2015/07/01/零基础免费搭建个人博客-hexo-git...

  • c#基础

    .net架构图 c#基础语法 占位符由{数字}组成,数字由0开始编号屏幕快照 2014-07-29 上午10.56...

  • OISF IDS/IPS engine prototype in

    2009/01/07 原文地址:https://blog.inliniac.net/2009/01/07/oisf...

  • 运用Kubernetes进行分布式负载测试

    运用Kubernetes进行分布式负载测试-CSDN.NET 发表于2015-07-07 22:07| 3356次...

  • (码友推荐)2018-07-07 .NET及相关开发资讯速递

    (码友推荐)2018-07-07 .NET及相关开发资讯速递: 1.Different Ways to Compa...

  • 黑马训练营Asp.Net第2期完整版

    初级 .Net入门教程_.Net入门视频教程|黑马程序员 C#基础教程_C#基础视频教程_黑马程序员 .Net基础...

  • 机器学习与模式识别

    http://www.csdn.net/article/2015-07-30/2825343

  • .Net基础06

    主要内容 常量 语法: 什么时候会用到常量?声明后不想被人改变的量 枚举 语法: public:访问修饰符。公开的...

  • .Net基础11

    File类 上一篇文章漏了几个方法,这里补上。 绝对路径和相对路径 绝对路径:通过给定的这个路径直接能在我的电脑中...

网友评论

      本文标题:.Net基础07

      本文链接:https://www.haomeiwen.com/subject/doshxxtx.html