C#学习day06(方法/函数及其关键字,附思维导图)

一:方法/函数

方法就是一大段代码,给这一段代码起个名字,这就是方法名,每个方法都封装了其对应的功能,当你需要使用这个功能的时候直接调用该方法即可

访问权限修饰符 static 返回值类型 方法名(形参列表)

{

​ //方法体 -> 编写功能代码

}

访问权限修饰符:是否有权限调用该方法。

static:静态的。

返回值类型:方法执行完成后返回的结果类型。如果不需要返回结果使用void关键字。

方法名:自定义名称,大驼峰命名规范。

参数:方法运行时需要的数据。可以有参数,也可以没有参数。

定义在方法中的变量是局部变量

参数类型为值类型:值类型变量存储的为实际的值,作为参数传递时,传递的依然为实际的值

参数类型为引用类型:引用类型变量存储的为地址,作为参数传递时,传递的依然为地址

参数类型为string类型:string存储的值不允许被改变

ref:在方法的参数中使用ref,将值类型的值传递变为引用传递。

如:static void Main(string\[\] args)

{

int i = 10;

Test(ref i);

Console.WriteLine(i); //200

Console.ReadKey();

}

public static void Test(ref int i)

{

i = 200;

}

paeams:params主要是在声明方法时,参数个数不确定时使用,使用时,需注意以下几点:

  1. 参数数组必须是一维数组。
  2. 不允许将params修饰符与ref修饰符组合起来使用。
  3. 与参数数组对应的实参可以是同一类型的数组名,也可以是任意多个与该数组的元素属于同一类型的变量。
  4. 形参列表中有多个参数,params必须在最后一个。
    如:static void Main(string\[\] args)
    {
    Test(1,2,3);
    Console.ReadKey();
    }
    public static void Test(params int\[\] ints)
    {
    }

out:out一般用在函数需要有多个返回值的场景,out底层则是内部为外部变量赋值。

接收返回值的变量必须由out修饰

有多少种返回值,就可以声明多少个out修饰的返回值类型

tryParse方法:int.tryParse()方法:将字符串类型转换为int类型。

方法源码:

csharp 复制代码
public static bool TryParse(string s, out int result){//...}

方法特点:

  1. 转换成功,返回true,将成功转换的值由result返回。
  2. 转换失败,返回false,result赋值为0返回。
    示例:
csharp 复制代码
int result;
bool v = int.TryParse("abc", out result);
//bool v = int.TryParse("abc", out int result);
Console.WriteLine(v);
Console.WriteLine(result);
Console.ReadKey();

二:练习:

实现代码如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace lianxi1

{

internal class Program

{

static void Main(string\[\] args)

{

复制代码
        //1.编写一个方法,判断一个数是否为奇数,如果是奇数返回true,否则返回false。
        //while (true)
        //{
        //    Console.WriteLine("请输入一个数:");
        //    int num = Convert.ToInt32(Console.ReadLine());
        //    if (IsOdd(num))
        //    {
        //        Console.WriteLine("输入q结束");
        //        string input = Console.ReadLine();
        //        if (input == "q")
        //        {
        //            break;
        //        }
        //    }
        //}

        //2.编写一个方法,接收一个字符串数组,将数组中的元素拼接成一个字符串,元素之间用"|"分隔。
        //string[] mingXin = new string[] { "梅西","卡卡","郑大世"};
        //string result = JoinString(mingXin);
        //Console.WriteLine(result);

        //3.编写一个方法,接收一个整数数组,返回数组中的最大值和最小值。
        //int max, min;
        //int[] arr = new int[100];
        //int num = Convert.ToInt32(Console.ReadLine());
        //for (int i = 0; i < num; i++)
        //{
        //    arr[i] = Convert.ToInt32(Console.ReadLine());
        //}
        //FindNumber(arr, out  max, out  min, num);
        //Console.WriteLine($"最大值:{max}, 最小值:{min}");

        //4.编写一个方法,接收一个整数数组,对数组进行排序,并返回排序后的数组。
        //int[] arr = new int[] { 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 };
        //Sort(arr);

        //5.编写一个方法,接收一个整数成绩,根据成绩返回对应的等级,成绩在80分以上为"优",60-79分为"良",60分以下为"差"。
        //Console.WriteLine("请输入您的成绩:");
        //int score = Convert.ToInt32(Console.ReadLine());
        //Console.WriteLine(GetLevel(score));

        //6.编写一个方法,接收一个字符串数组,将数组中的元素进行反转,并返回反转后的数组。a
        string[] guoJia = new string[] { "中国","韩国", "美国", "日本"};
        Reverse(guoJia);
        Console.ReadKey();

    }
    public static void Reverse(string[] guoJia)
    {
        for (int i = 0; i < guoJia.Length / 2; i++)
        {
            string temp = guoJia[i];
            guoJia[i] = guoJia[guoJia.Length - 1 - i];
            guoJia[guoJia.Length - 1 - i] = temp;
        }
        foreach (var item in guoJia)
        {
            Console.Write(item + " ");
        }
    }
    public static string GetLevel(int score)
    {
        string result;
        if(score >= 80)
        {
            result = "优";
        }
        else if(score >= 60)
        {
            result = "良";
        }
        else
        {
            result = "差";
        }
        return result;
    }
    public static void Sort(int[] arr)
    {
        for (int i = 0; i < arr.Length - 1; i++)
        {
            for (int j = 0; j < arr.Length - 1 - i; j++)
            {
                if (arr[j] > arr[j + 1])
                {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
        foreach (var item in arr)
        {
            Console.Write(item + " ");
        }
    }
    public static void FindNumber(int[] arr, out int max, out int min, int num)
    {
        max = arr[0];
        min = arr[0];
        for (int i = 1; i < num; i++)
        {
            if (arr[i] > max)
            {
                max = arr[i];
            }
            if (arr[i] < min)
            {
                min = arr[i];
            }
        }
    }
    static string JoinString(string[] mingXin)
    {
        string result = "";
        for (int i = 0; i < mingXin.Length; i++)
        {
            result += mingXin[i];
            if (i < mingXin.Length - 1)
            {
                result += "|";
            }
        }
        return result;
    }
    static bool IsOdd(int num) 
    {
        if(num % 2 != 0)     return true;
        else return false;
    }
}

}

相关推荐
.Peter2 小时前
.NET/WPF 程序在部分 Windows 电脑无法保存用户环境变量:使用 DPAPI 实现安全兼容
windows·信息安全·c#·.net·wpf·环境变量·dpapi
xian_wwq2 小时前
【学习笔记】Loop Engineering,从手动提示到目标驱动自动化-13/16
笔记·学习·自动化
殷忆枫2 小时前
K210 BOX学习笔记:环境搭建及模型训练部署
学习
qingyunliushuiyu2 小时前
制造行业智能问数工具怎么选?2026年选型核心要点梳理
大数据·人工智能·经验分享·制造
lifallen2 小时前
Avernet:Agent 的组织网络
人工智能·学习·ai·开源软件·ai编程
农村小镇哥2 小时前
怎么把HTM格式转化成WORD
ui·c#·word
农村小镇哥2 小时前
C#读取CSV文件的方法
服务器·数据库·c#
我爱cope3 小时前
【计算机网络 | 数据链路层2:差错检测与纠正:奇偶校验、校验和、CRC 分别怎么工作?】
网络·学习·计算机网络
知识分享小能手3 小时前
线性代数学习教程,从入门到精通,矩阵及其运算 — 完整知识点与详细推导(3)
学习·线性代数·矩阵