传值
传值参数
- 值参数创建变量的副本
- 对值参数的操作永远不影响变量的值(C语言中的取址调用改变),这是创建了一个新的对象
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 传值参数
{
class Program
{
static void Main(string[] args)
{
int a = 1;
Console.WriteLine(Data.Add(a));
Console.WriteLine(a);
}
}
class Data
{
public static int Add(int y)
{
return y + 1;
}
}
/*运行结果:2
* 1
*/
}
引用参数
- 引用参数并不创建变量的副本
- 使用ref修饰符显式指出——此方法的副作用就是会改变实例的值
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 引用参数
{
class Program
{
static void Main(string[] args)
{
int a = 10;
Console.WriteLine(Add(ref a));
Console.WriteLine(a);
}
static int Add(ref int i)
{
i = i + 100;
return i;
}
}
/*运行结果:110
* 110
*/
}
输出
输出参数
- 输出参数并不创建变量的副本
- 方法体内必需要对有对输出变量的赋值的操作
- 使用out修饰符显示式指出——此方法的副作用是通过参数向外输出值
- 从语义上来讲——ref为了改变,out是为了输出
调用带有输出参数的方法:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ParameterExample
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入一个数字:");
string number = Console.ReadLine();
double x = 0;
bool b1 = double.TryParse(number, out x); //out x表面将程序运行的结果直接赋值给了x
if (b1==false)
{
Console.WriteLine("你的输入错误!!!");
return; //main函数的返回值,直接输出空值,程序结束
}
Console.WriteLine("请输入第二个数字:");
string args1 = Console.ReadLine();
double y = 0;
bool b2 = double.TryParse(args1, out y);
if (b2 == false)
{
Console.WriteLine("你的输入错误!!!");
return;
}
double z = x + y;
Console.WriteLine("结果为:{0}",z);
}
}
/*输入:12 23
* 输出:35
*/
}
声明一个带有输出参数的方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 具有输出参数的方法
{
class Program
{
static void Main(string[] args)
{
double x = 0;
bool b = DoubleParser.TryParse("113", out x);
if (b==true)
{
Console.WriteLine("转换成功!!!");
}
else
{
Console.WriteLine("转换失败!!");
}
}
}
class DoubleParser
{
public static bool TryParse(string input,out double result)
{
try
{
result = double.Parse(input);
return true;
}
catch
{
result = 0; //转换失败后,原来的值将会被覆盖
return false;
}
}
}
}
params
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace params参数
{
class Program
{
static void Main(string[] args)
{
int result = Add(1, 2, 3, 4); //使用了params就不用再新建数组了
Console.WriteLine(result);
}
static int Add(params int[]Array)
{
int sum = 0;
foreach (var item in Array)
{
sum += item;
}
return sum;
}
}
}
注意:有些方法存在上述的设置,例如:string的split(可以分割字符串)方法
数组的参数中,由params修饰的参数必须是参数中的最后一个
具名参数
指出参数的名字,这样可以增强程序的可读性
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 具名参数
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Add(second_number: 23, first_number: 12));
}
static int Add(int first_number,int second_number)
{
return first_number + second_number;
}
}
}
可选参数
对于设定了默认的参数可以不用赋值,程序也可以运行
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 可选参数
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Add(second_number: 23, first_number: 12));
}
static int Add(int first_number, int second_number, int third=10)
{
return first_number + second_number + third;
}
}
}
扩展方法
对于一些固定的类(无法对其源码进行修改的),为他添加一些方法
- 扩展方法必须是共有的、静态的
- 必须是形参列表中的第一个,由this修饰
- 必须由一个静态类(一般类名为SomeTypeExtension)来统一收纳对SomeType类型的扩展方法
例如:为double类添加一个四舍五入的函数(double是没有自带四舍五入的方法的,该方法在Math中)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 拓展方法
{
class Program
{
static void Main(string[] args)
{
double x = 3.1415;
double y = x.Round(2);//注意:此处只接收一个参数
Console.WriteLine(y);
}
}
static class DoubleExtension
{
public static double Round(this double input,int idigit) //注意:这个位置的参数和之前的参数不一样
{
double result = Math.Round(input, idigit);
return result;
}
}
}
//运行结果:3.14
Linq(功能极其强大)
注意:需要先引入对应的名称空间
All():判断所有条件是不是符合某个条件,Any():判断是否存在某个元素符合某个条件
例如:判断列表中的数值是不是都大于10,利用All()方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq;
namespace Linq
{
class Program
{
static void Main(string[] args)
{
List<int> mylist = new List<int> { 12, 23, 4, 34, 334, 33 };
bool result = mylist.All(i => i > 10);
Console.WriteLine(result);
}
}
}
//运行结果:FALSE