C# 方法(函数)

news/2024/4/27 17:26:11

文章目录

  • C# 方法(函数)
    • 简单示例
      • 程序代码
      • 运行效果
    • 值传递和引用传递
      • 示例程序
    • 运行效果
    • 按输出传递参数
      • 运行结果

C# 方法(函数)

简单示例

程序代码

访问的限制符
在这里插入图片描述

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConsoleApp1
{class Test{public int getPublicSumAge(int a,int b){int c;c = a + b;return c;}private int getPrivateSumAge(int a, int b){int c;c =( a + b ) * 10;return c;}//递归方法:factorialpublic int factorial(int num){/* 局部变量定义 */int result;if (num == 1){return 1;}else{result = factorial(num - 1) * num;return result;}}static void Main(string[] args){int age;Test test = new Test();age = test.getPublicSumAge(12, 8);Console.WriteLine("SumAgePublic:" + age);//Console.ReadLine();age = test.getPrivateSumAge(12, 8);Console.WriteLine("SumAgePrivate:" + age);Console.WriteLine("6 的阶乘是: {0}", test.factorial(6));Console.ReadLine();}}
}

运行效果

在这里插入图片描述

值传递和引用传递

示例程序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConsoleApp1
{class Test{//转换函数(值传递)public void swapValue(int x,int y){int temp;temp = x; /* 保存 x 的值 */x = y;    /* 把 y 赋值给 x */y = temp; /* 把 temp 赋值给 y */}//转换函数(引用传递)public void swapRef(ref int x,ref int y){int temp;temp = x; /* 保存 x 的值 */x = y;    /* 把 y 赋值给 x */y = temp; /* 把 temp 赋值给 y */}static void Main(string[] args){Test test = new Test();int a1 = 100;int b1 = 200;Console.WriteLine("在交换之前,a1 的值: {0}", a1);Console.WriteLine("在交换之前,b1 的值: {0}", b1);/* 调用函数来交换值 */test.swapValue(a1, b1);Console.WriteLine("在交换之后,a1 的值: {0}", a1);Console.WriteLine("在交换之后,b1 的值: {0}", b1);/* 局部变量定义 */int a2 = 300;int b2 = 400;Console.WriteLine("在交换之前,a2 的值: {0}", a2);Console.WriteLine("在交换之前,b2 的值: {0}", b2);/* 调用函数来交换值 */test.swapRef(ref a2, ref b2);Console.WriteLine("在交换之后,a2 的值: {0}", a2);Console.WriteLine("在交换之后,b2 的值: {0}", b2);Console.ReadLine();}}
}

运行效果

在这里插入图片描述

按输出传递参数

return 语句可用于只从函数中返回一个值。但是,可以使用 输出参数 来从函数中返回两个值。输出参数会把方法输出的数据赋给自己,其他方面与引用参数相似。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConsoleApp1
{class Test{public void getValue(out int x){int temp = 5;x = temp;}static void Main(string[] args){Test test = new Test();/* 局部变量定义 */int a = 100;Console.WriteLine("方法调用之前,a的值:{0}", a);test.getValue(out a);Console.WriteLine("方法调用之后,a的值:{0}", a);Console.ReadLine();}}
}

提供给输出参数的变量不需要赋值。当需要从一个参数没有指定初始值的方法中返回值时,输出参数特别有用。请看下面的实例,来理解这一点:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConsoleApp1
{class Test{public void getValues(out int x, out int y){Console.WriteLine("请输入第一个值: ");x = Convert.ToInt32(Console.ReadLine());Console.WriteLine("请输入第二个值: ");y = Convert.ToInt32(Console.ReadLine());}static void Main(string[] args){Test test = new Test();/* 局部变量定义 */int b, c;/* 调用函数来获取值 */test.getValues(out b, out c);Console.WriteLine("在方法调用之后,b 的值: {0}", b);Console.WriteLine("在方法调用之后,c 的值: {0}", c);Console.ReadLine();}}
}

运行结果

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.cpky.cn/p/10783.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

ElasticSearch安装

1 下载软件 Past Releases of Elastic Stack Software | Elastic 2.解压在没有中文和空格的文件夹内,注意层级别太深 3.在es服务内安装插件,分词器 ik分词器 Releases infinilabs/analysis-ik GitHub 下载对应版本,在es文件夹内找到plugins,创建ik文件夹,将下载的ik分词器…

Grok-1:参数量最大的开源大语言模型

Grok-1:参数量最大的开源大语言模型 项目简介 由马斯克领衔的大型模型企业 xAI 正式公布了一项重要动作:开源了一个拥有 3140 亿参数的混合专家模型(MoE)「Grok-1」,连同其模型权重和网络架构一并公开。 此举将 Gro…

2078: [蓝桥杯2023初赛] 01 串的熵

对于一个长度为 n 的 01 串 S x1x2x3...xn. 香农信息熵的定义为: 。 其中 p(0), p(1) 表示在这个 01 串中 0 和 1 出现的占比。 比如,对于S 100 来说,信息熵 H(S ) - 1/3 log2(1/3) - 2/3 log2(2/3) - 2/3 log2(2/3) 1.3083。 对于一个…

前端项目(vue3)自动化部署(Gitlab CI/CD)

天行健,君子以自强不息;地势坤,君子以厚德载物。 每个人都有惰性,但不断学习是好好生活的根本,共勉! 文章均为学习整理笔记,分享记录为主,如有错误请指正,共同学习进步。…

插入排序和希尔排序

目录 一、插入排序 1.思想 2.代码实现分析 3.测试结果: 二、希尔排序 1.思想 2.代码实现 3.测试 一、插入排序 1.思想 思想:将数组分为已排序区间和未排序区间两部分,初始时,已排序区间为空,从数组的第二…

Aivis:AI语音模仿系统

Aivis:AI语音模仿系统。 Aivis是一个AI语音模仿系统,它利用深度学习和神经网络技术来模仿特定人的声音。这种系统通常涉及以下几个关键步骤和技术: 声音采集:首先,需要收集目标人物的声音样本。这些样本可以是录音、演…