在呼叫方法时,难免会需要传递参数,
那这些参数该如何传递呢?
在传递的过程中,究竟是传值还是传址呢?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CsharpDemo
{
class Program
{
static void Main(string[] args)
{
//宣告X整数
int x = Convert.ToInt32(Console.ReadLine());
//宣告y整数
int y = Convert.ToInt32(Console.ReadLine());
//呼叫Bigger方法,并将x,y的直传入
int reault = Bigger(x, y);
Console.WriteLine("x = " + x + " ,y = " + y);
Console.WriteLine("最大值为" + reault);
Console.ReadKey();
}
//比较两数大小的方法,将x y 传入
static int Bigger(int x, int y)
{
//判断x y谁大
int maxValue = (x >= y) ? x : y;
return maxValue;
}
}
}
输入:
1
5
结果:
1
5
x = 1 ,y = 5
最大值为5
我们这边使用到了三元条件运算子
,它的结构如下:
在想要停止的程序前面点一下,会出现小红点(快捷键:F9)
接下来我们执行程序,会发现执行到此行时程序停下来了
这时候我们到功能列上按逐步执行(快捷键:F11),会发现按一下就执行一行程序,这样我们就可以了解程序如何执行了
如果我们逐步执行程序後,会发现原来呼叫方法时,程序会先进入方法执行,执行完再回到原本程序继续执行
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CsharpDemo
{
class Program
{
static void Main(string[] args)
{
//宣告一个Llist
List<string> myList = new List<string>() { "孤独一只鸡", "人生一条虫", "寂寞一只鸟", "爆肝一只鹅" };
Console.WriteLine("原始笔数 = " + myList.Count());
//呼叫方法并传入List
int newCount = ListCount(myList);
Console.WriteLine("目前笔数 = " + newCount);
//这时候我们用foreach回圈来拆解原本的List
foreach(string item in myList)
{
Console.Write(item + " ");
}
Console.ReadKey();
}
//我们使用一个方法并接收list的传递,然後回传list的笔数
static int ListCount(List<string> newList)
{
//我们在这list偷偷加一只鱼
newList.Add("高冷一只鱼");
return newList.Count();
}
}
结果:
原始笔数 = 4
目前笔数 = 5
孤独一只鸡 人生一条虫 寂寞一只鸟 爆肝一只鹅 高冷一只鱼
我们可以发现,我们明明在新的方法,新的List去修改List,结果原来的List却受到影响,这就是因为他们指向的记忆体位置是同一个地方,所以只要一有变动所有资料都会受到影响
<<: OpenStack Neutron 介绍 — OVN Plug-in 架构
>>: Day3:进入新手村前先让我复习一下QQ-CSS-box-sizing
今天我们会把Route 53串接到昨天建置的ELB上。但开始之前,我们先问自己一个问题,Route ...
前言 Fcitx 5 是 Debian 11 预先安装好的中文输入法,目前,无法在 Debian 1...
Q: 9月的最後一天来点特别的吧? A: 常见的弹窗来一个! 样式先做好是很重要的! 首先来个简易...
今天稍微回头update一下musl这边的porting状况。 之前遇到message queue跟...
Day 10 - Kotlin的函式 不知不觉我们已经到达了第十天了,昨天讲的是when条件判断跟w...