C#基础--数组+函数

 1.数组的概念-存储相同类型的集合 变量类型[] 数组名称 = new 变量[数组的长度]{内容1,内容2}

#实例
int [] arr1; //不过之后用的时候还是要初始化
int [] arr2 = new int [5]
new int[]{}
{}
 

2.数组的使用

1)数组长度

int[] arry = {1,2,3,4,5,6};
Console.WriteLine(arry.Length);

2)获取数组中的元素

int[] arry = {1,2,3,4,5,6};
Console.WriteLine(arry[0]);

3.二维数组的申明

int[,] arr; //必须初始化
int[,] arr2 = new [2,3];
int[,] arr3 = new [2,3]{ {1,2,3},{2,3,4} }
int[,] arr4 = new [,]{ {1,2,3},{2,3,4} }

 4.二维数组查长度--分别查行和列

int[,] arr4 = new [,]{ {1,2,3},{2,3,4} }
Console.WriteLine(arr4.GetLength(0));
Console.WriteLine(arr4.GetLength(1));

 5.先写进去,在遍历一次

using System;

public class Program
{
    public static void Main()
    {
        int[,] arr = new int[10, 10];
        int count = 1;

        for (int i = 0; i < 10; i++)
        {
            for (int j = 0; j < 10; j++)
            {
                arr[i, j] = count;
                count++;
            }
        }

        for (int i = 0; i < 10; i++)
        {
            for (int j = 0; j < 10; j++)
            {
                Console.Write(arr[i, j] + "\t");
            }
            Console.WriteLine();
        }
    }
}

二。函数

1.函数的申明 static + 变量类型 + 函数名字(参数)

调用时直接函数名字(参数)

static int[] HanShu(int a, int b)
{
    int sum = a + b;
    int pro = a * b;
    return new int[]{sum, pro};
}

2.ref---当你在函数中改变参数的值,只是暂时修改,使用ref将变为全局更改,数组同理

int[] arr = {1,3,4}
static void HanShuRef(ref int[] arr)
{
    arr = new int[]{1,2,3};
}
#调用时
HanShuRef(ref arr);
//out 关键词使用也一样

3.ref--需要赋值     out--必须需要内部赋值

4. 参数过多--params

static cord HanShu(params int[] arr)

5.小作业

using System;
namespace Ref
{
    class Out
    {
        static bool PanDuan(string username, string passworld, out string message)
        {
            if (username == "hxn" && passworld == "0726")
            {
                message = "登陆成功";
                return true;
            }
            else
            {
                if (username != "hxn")
                {
                    message = "用户名错误";
                    return false;
                }
                else
                {
                    message = "用户密码错误";
                    return false;
                }
            }
        }
        static void Main(string[] args)
        {

            Console.WriteLine("请输入用户名");
            string username = Console.ReadLine();
            Console.WriteLine("请输入密码");
            string Password = Console.ReadLine();
            bool result = PanDuan(username, Password, out string message);
            Console.WriteLine(message);
            Console.WriteLine(result);

        }
    }
}
using System;

namespace SumAndAverage
{
    class Program
    {
        static void Main(string[] args)
        {
            // 调用SumAndAverage函数,并将数字作为参数传递
            double sum, avg;
            SumAndAverage(out sum, out avg, 1, 2, 3, 4, 5, 6, 7, 8, 9, 100);

            // 输出结果
            Console.WriteLine($"和为:{sum}"  );
            Console.WriteLine($"平均数为:{avg}");
            Console.ReadLine();
        }

        static void SumAndAverage(out double sum, out double avg, params int[] nums)
        {
            sum = 0;
            foreach (int num in nums)
            {
                sum += num;
            }
            avg = sum / nums.Length;
            
        }
    }
}

6.函数重载:同一语句块--函数名相同但是函数参数不同 | 参数位置不同。--提升代码的可读性

ref -out不能同时修饰重载函数 

using System;
namespace Tset
{
    class Comepare
    {
        static void Cpr(int a, int b)
        {
            if(a < b)
            {
                Console.WriteLine(b);
            }
            else
            {
                Console.WriteLine(a);
            }
        }
        static void Cpr(float a, float b)
        {
            if (a < b)
            {
                Console.WriteLine(b);
            }
            else
            {
                Console.WriteLine(a);
            }
        }
        static void Cpr(double a, double b)
        {
            if (a < b)
            {
                Console.WriteLine(b);
            }
            else
            {
                Console.WriteLine(a);
            }
        }

        static void Main(string[] args)
        {
            Console.WriteLine("ok");
            Cpr(1.1f, 3.4f);
        }
    }
}
using System;

namespace Tset
{
    class Compare
    {
        static int Cpr(out int message, params int[] nums)
        {
            message = nums[0];
            for (int i = 1; i < nums.Length; i++)
            {
                if (nums[i] > message)
                {
                    message = nums[i];
                }
            }
            return message;
        }

        static void Main(string[] args)
        {
            Console.WriteLine("ok");
            int message;
            Cpr(out message, 1, 3, 6, 23, 65, 2, 1, 88);
            Console.WriteLine(message);
        }
    }
}

递归函数--在函数中调用函数--必须要能停止

using System;

namespace DiGui
{
    class Digui
    {
        static void Main()
        {
            static void Print(int a = 1)
            {
                if (a < 11)
                {
                    Console.WriteLine(a);
                    ++a;
                    Print(a);
                }
            }
        
            Print();
        }
    }
}
using System;

namespace DiGui
{
    class Digui
    {
        static int Jiecheng(int a)
        {
            if (a == 1)
            {
                return 1;
            }
            else
            {
                return a * Jiecheng(a - 1);
            }
        }

        static void Main()
        {
            int result = Jiecheng(5);
            Console.WriteLine(result);
        }
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#中,有几种方法可以将数组作为函数的参数传入。第一种方法是使用参数数组,也就是在函数定义中的最后一个参数加上params关键字。通过这种方式,我们可以向函数传递不定数量的参数。例如,可以定义一个带有参数数组函数,如下所示:```static int SumVals(params int[] vals)```。在调用这个函数时,可以直接传入一组整数作为参数,例如```SumVals(1, 2, 3, 4, 5)```。函数内部可以通过for循环对参数数组进行遍历,实现对数组中元素的操作。 另一种方法是直接传递数组作为函数的参数。在函数定义中,可以将参数的类型指定为数组类型,例如```static int Sum(int[] arr)```。在调用这个函数时,可以创建一个数组,并将其作为参数传入,例如```Sum(new int { 1, 2, 3, 4 })```。函数内部同样可以通过for循环遍历数组,进行相应的操作。 总之,在C#中,可以通过参数数组或直接传递数组作为函数的参数来代入数组。这两种方法都可以实现对数组元素的操作,只是在使用上稍有不同。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [C#中参数数组、引用参数和输出参数示例详解](https://download.csdn.net/download/weixin_38650150/13992165)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [C#---参数数组数组参数](https://blog.csdn.net/qq_46289420/article/details/111324240)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值