C#---函数

一、函数

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

如果被调用者想要得到调用者的值:

1)、传递参数。
2)、使用静态字段来模拟全局变量(C#中没有全局变量

如果调用者想要得到被调用者的值:

1)、返回值

class Program
    {
        //字段  属于类的字段
        public static int _number = 9;
        static void Main(string[] args)
        {
            int a = 4;
            int b=Test(a);
            Console.WriteLine(b);
            Console.WriteLine(_number);
            Console.ReadKey();
        }
        public static int Test(int n)
        {
            n += _number;
            return n;
        }
    }

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

3.方法的功能一定要单一.

static void Main(string[] args)
        {
            Console.WriteLine("请输入一个数");
            String s = Console.ReadLine();
            int i= GetNumber(s);
            Console.WriteLine(i);
            Console.ReadKey();
        }
       // 读取输入的整数,定义成方法,多次调用(如果用户输入的是数字, 则返回, 否则提示用户重新输入)
        public static int GetNumber(String str)
        {
            while (true)
            {
                try
                {
                 int i = Convert.ToInt32(str);
                 return i;
                }
                catch
                {
                    Console.WriteLine("输入错误,请重新输入");
                    str = Console.ReadLine();
                }  
            } 
           
        }
    }

  //写一个方法求一个数组中的最大值、最小值、总和、平均值
            int[] numbers = { 1, 5, 3, 6, 9, 47, 55, 69 };
            //将要返回的4个值,放到一个数组中返回
            int[] res = GetMaxMinSumAVG(numbers);
            Console.WriteLine("最大值{0},最小值{1},总和是{2},平均值是{3}",res[0],res[1],res[0],res[3]);
            Console.ReadKey();
        }
        /// <summary>
        /// 计算一个数组的最大值、最小值、平均值、和
        /// </summary>
        /// <param name="nums">数组</param>
        /// <returns></returns>
        public static int[] GetMaxMinSumAVG(int[] nums)
        {
            int[] res = new int[4];
            //假设res[0]最大值 res[1]最小值 res[2]总和 res[3]平均值
            res[0] = nums[0];//max
            res[1] = nums[0];//min
            res[2] = 0;
            for (int i = 0; i < nums.Length; i++)
            {
                //如果当前循环到的元素比我假定的最大值还大
                if (nums[i]>res[0])
                {
                    // 将当前循环到的元素赋值给我的最大值
                    res[0] = nums[i];
                }
                if (nums[i] < res[1])
                {
                    res[0] = nums[i];
                }
                res[2] += nums[i];
            }
            res[3] = res[2] / nums.Length;
            return res;
        }

二、重要参数 out、 ref、params

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

static void Main(string[] args)
        {
            int[] numbers = { 1, 5, 3, 6, 9, 47, 55, 69 };
            int max;
            int min;
            int sum;
            int avg;
            Test(numbers,out max, out min, out avg, out sum);
            Console.WriteLine("最大值{0},最小值{1},总和是{2},平均值是{3}",max,min,sum,avg);
            Console.ReadKey();
        }
        /// <summary>
        /// 计算一个数组的最大值、最小值、平均值、和
        /// </summary>
        /// <param name="nums">要求值得数组</param>
        /// <param name="max">最大值</param>
        /// <param name="min">最小值</param>
        /// <param name="avg">平均值</param>
        /// <param name="sum">和</param>
        public static void Test(int[] nums,out int max,out int min,out int avg,out int sum)
        {
            // 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;
        }

//分别的提示用户输入用户名和密码
            //你写一个方法来判断用户输入的是否正确
            //返回给用户一个登陆结果,并且还要单独的返回给用户一个登陆信息/如果用户名错误,除了返回登陆结果之外,还要返回一个“用户名错误"
            //“密码错误”

static void Main(string[] args)
        {

            Console.WriteLine("请输入用户名");
            String username = Console.ReadLine();
            Console.WriteLine("请输入密码");
            String password = Console.ReadLine();
            String msg;
            bool b=IsLogin(username, password, out msg);
            Console.WriteLine("登陆结果{0}",b);
            Console.WriteLine("登陆信息{0}",msg);
            Console.ReadKey();
        }
        /// <summary>
        /// 判断登陆是否成功
        /// </summary>
        /// <param name="name">用户名</param>
        /// <param name="pwd">密码</param>
        /// <param name="msg">登陆信息</param>
        /// <returns>返回登陆结果</returns>
       public static bool IsLogin(String name,string pwd,out string msg)
        {
            if (name == "admin" && pwd == "888888")
            {
                msg = "登陆成功";
                return true;
            }else if (name == "admin")
            {
                msg = "密码错误";
                return false;
            }else if (pwd == "888888")
            {
                msg = "用户名错误";
                return false;
            }
            else
            {
                msg ="未知错误";
                return false;
            }
        }

2.ref参数

能够将一个变量带入一个方法中进行改变,改变完成后,再讲改变后的值带出方法。

ref参数要求在方法外必须为其赋值,而方法内可以不赋值。

 static void Main(string[] args)
        {
            int n1 = 10;
            int n2 = 20;
            Exchang(ref n1, ref n2);
            Console.WriteLine(n1);
            Console.WriteLine(n2);
            Console.ReadKey();
        }

        public static void Exchang( ref int n1,ref int  n2)
        {
            int temp = n1;
            n1 = n2;
            n2 = temp;
            //n1=n1-n2;
            //n2=n1+n2;
            //n1=n2-n1;
        }

3.params可变参数

将实参列表中跟可变参数数组类型一致的元素都当做数组的元素去处理

params可变参数必须是形参列表中的最后一个元素。

 static void Main(string[] args)
        {
           // int[] num = { 95, 47, 02 };
            Test("战神", 95, 47, 02,63,25);
            Console.ReadKey();
        }

        public static void Test(String name, int id,params int[] score)
        {
            int sum = 0;
            for (int i = 0; i < score.Length; i++)
            {
                sum += score[i];
            }
            Console.WriteLine("{0}的总成绩为{1},学号为{2}",name,sum,id);
        }

方法的重载
概念: 方法的重载指的就是方法的名称相同给,但是参数不同。、

参数不同,分为两种情况
1)、如果参数的个数相同,那么参数的类型就不能相同。

2)、如果参数的类型相同,那么参数的个数就不能相同。

★★★方法的重教足返回值没有关系。
        public static void T(int n1,int n2)
        {
        }
        public static void T(double n1, double n2)
        {
        }
        public static void T(int n1, int n2,int n3)
        {
        }

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

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

namespace ConsoleApp3
{
    class Program
    {
        
        static void Main(string[] args)
        {
            // 提示用户输入两个数字计算这两个数字之间所有整数的和
            // 1、用户只能输入数字
            Console.WriteLine("请输入第一个数字");
            String strNumberOne = Console.ReadLine();
            int numberOne = GetNumber(strNumberOne);
            Console.WriteLine("请输入第二个数字");
            string strNumberTwo = Console.ReadLine();
            int numberTwo = GetNumber(strNumberTwo);
            //2、要求第一个数字必须比第二个数字小就重新输入
            JudgeNumber(ref numberOne, ref numberTwo);
            //3、计算两个数字之间和
            int sum = GetSum(numberOne, numberTwo);
            Console.WriteLine("{0}",sum);
            Console.ReadKey();
        }
        public static int GetSum(int n1,int n2)
        {
            int sum = 0;
            for (int i = n1; i <=n2; i++)
            {
                sum += i;
            }
            return sum;
        }
        public static int GetNumber(String s)
        {
            while (true)
            {
                try
                {
                 int number = Convert.ToInt32(s);
                  return number;
                }
                catch
                {
                    Console.WriteLine("输入错误!!!请重新输入");
                    s = Console.ReadLine();
                }   
            }
        }
        public static void JudgeNumber( ref int n1,ref int n2)
        {
           while (true)
           {
             if (n1 < n2)
            {
                    //符合题意
                    return;
            }
            else
            {
                Console.WriteLine("第一个数字不能大于或者等于第二个数字,请重新输入第一个数字");
                string s1 = Console.ReadLine();
                //调用Getnumber
                n1 = GetNumber(s1);
                Console.WriteLine("请重新输入第二个数字");
                string s2 = Console.ReadLine();
                n2 = GetNumber(s2);
            }
          }
           
        }
    }
    }

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#中,可以使用HttpWebRequest类来发送POST请求。然而,对于POST方式,HttpWebRequest类并没有提供一个很方便的方法来设置Content-Type。但是,你可以通过设置HttpWebRequest的ContentType属性来指定Content-Type。在你的代码中,你可以使用"multipart/form-data"作为ContentType来上传json表格和文件。\[2\] 在你的代码中,你可以使用PostIQ函数来发送POST请求。这个函数接受四个参数:URL(服务器路径),FileName(文件名),jsonstr(表格),FilePath(文件路径)。在函数内部,它使用FileStream来打开文件,并将文件流作为上传的一部分。同时,它还使用一个字典来存储POST请求的参数,其中"entity"是表格的名称。最后,它调用HttpUploadClient.Execute方法来执行POST请求,并返回结果。\[2\] 在你的代码中,还有一个UploadParameterType类,它定义了一些属性和方法来设置上传文件的参数。其中,Url属性用于指定上传地址,FileNameKey和FileNameValue属性用于指定文件名称的键值对,Encoding属性用于指定编码格式,UploadStream属性用于指定上传文件的流,PostParameters属性用于指定上传文件携带的参数集合。\[3\] 所以,通过设置HttpWebRequest的ContentType属性为"multipart/form-data",你可以在C#中发送POST请求,并指定Content-Type为"multipart/form-data"。\[2\] #### 引用[.reference_title] - *1* [C#中使用HttpWebRequest用Post提交MultiPart数据](https://blog.csdn.net/aohaizhi7722/article/details/102160156)[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^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [c# HTTP Post上传文件与表格 ContentType = “multipart/form-data](https://blog.csdn.net/www89574622/article/details/114641205)[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^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值