传值参数
值类型
方法内传进来的是副本
stu.AddOne(y);
打出来是101,Console.WriteLine(y);
打出来是100。
using System;
using System.Collections.Generic;
using System.Timers;
using System.Runtime.InteropServices;
using System.Collections;
//using System.Windows.Forms;
namespace NamespaceEncasement
{
class Program
{
static void Main(string[] args)
{
Student stu = new Student() { Name = "Tim" };
SomeMethod(stu);
Console.WriteLine(stu.Name);
Console.ReadKey();
}
static void SomeMethod(Student stu)
{
stu = new Student { Name = "Tom" };
Console.WriteLine(stu.Name);
}
}
class Student
{
public string Name { get; set; }
}
}
只更新对象,不创建新对象
引用参数
值类型引用参数
using System;
using System.Collections.Generic;
using System.Timers;
using System.Runtime.InteropServices;
using System.Collections;
//using System.Windows.Forms;
namespace NamespaceEncasement
{
class Program
{
static void Main(string[] args)
{
int y = 1;
IWantSideEffect(ref y);
Console.WriteLine(y);
}
static void IWantSideEffect(ref int x)
{
x = x + 100;
}
}
}
打印结果为101
引用类型引用参数
using System;
using System.Collections.Generic;
using System.Timers;
using System.Runtime.InteropServices;
using System.Collections;
//using System.Windows.Forms;
namespace NamespaceEncasement
{
class Program
{
static void Main(string[] args)
{
Student outterSu = new Student() { Name = "Tim" };
Console.WriteLine("HashCode={0},Name={1}",outterSu.GetHashCode(),outterSu.Name);
Console.WriteLine("-----------------");
IWantSideEffect(ref outterSu);//方法体内
Console.WriteLine("HashCode={0},Name={1}", outterSu.GetHashCode(), outterSu.Name);//方法体外
Console.ReadKey();
}
static void IWantSideEffect(ref Student stu)
{
stu = new Student() { Name="Tom"};
Console.WriteLine("HashCod={0},Name={1}",stu.GetHashCode(),stu.Name);
}
}
class Student
{
public string Name { get; set; }
}
}
打印结果为:
HashCode=46104728,Name=Tim
-----------------
HashCod=12289376,Name=Tom
HashCode=12289376,Name=Tom
引用类型,不创建对象只改变对象值
using System;
using System.Collections.Generic;
using System.Timers;
using System.Runtime.InteropServices;
using System.Collections;
//using System.Windows.Forms;
namespace NamespaceEncasement
{
class Program
{
static void Main(string[] args)
{
Student outterSu = new Student() { Name = "Tim" };
Console.WriteLine("HashCode={0},Name={1}", outterSu.GetHashCode(), outterSu.Name);
Console.WriteLine("-----------------");
IWantSideEffect(ref outterSu);//方法体内
Console.WriteLine("HashCode={0},Name={1}", outterSu.GetHashCode(), outterSu.Name);//方法体外
Console.ReadKey();
}
static void IWantSideEffect(ref Student stu)
{
stu.Name = "Tom";
Console.WriteLine("HashCod={0},Name={1}", stu.GetHashCode(), stu.Name);
}
}
class Student
{
public string Name { get; set; }
}
}
打印结果为:
HashCode=46104728,Name=Tim
-----------------
HashCod=46104728,Name=Tom
HashCode=46104728,Name=Tom
虽然结果不应,但是HashCod
说明一直在操作同一个对象,
如果把ref关键字去掉,变为
using System;
using System.Collections.Generic;
using System.Timers;
using System.Runtime.InteropServices;
using System.Collections;
//using System.Windows.Forms;
namespace NamespaceEncasement
{
class Program
{
static void Main(string[] args)
{
Student outterSu = new Student() { Name = "Tim" };
Console.WriteLine("HashCode={0},Name={1}", outterSu.GetHashCode(), outterSu.Name);
Console.WriteLine("-----------------");
IWantSideEffect(outterSu);//方法体内
Console.WriteLine("HashCode={0},Name={1}", outterSu.GetHashCode(), outterSu.Name);//方法体外
Console.ReadKey();
}
static void IWantSideEffect(tudent stu)
{
stu.Name = "Tom";
Console.WriteLine("HashCod={0},Name={1}", stu.GetHashCode(), stu.Name);
}
}
class Student
{
public string Name { get; set; }
}
}
打印结果与加了ref
一样,但是内存机理不一样,这种传值函数创建了副本,这种方式static void IWantSideEffect(tudent stu)
中的stu
和Student outterSu = new Student() { Name = "Tim" }
中的outterSu
的地址不一样,但是这两个变量存储了同一个实例在堆上的地址。
而加上ref
以后,stu
和outterSu
的地址是一样的,并且也是存储的对象在堆内存上的地址。
输出参数
值类型输出参数
using System;
using System.Collections.Generic;
using System.Timers;
using System.Runtime.InteropServices;
using System.Collections;
//using System.Windows.Forms;
namespace NamespaceEncasement
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please input the first number");
string arg1 = Console.ReadLine();
double x = 0;
bool b1 = double.TryParse(arg1, out x);
if (b1 == false)
{
Console.WriteLine("Input error!");
return;
}
Console.WriteLine("Please input the second number");
string arg2 = Console.ReadLine();
double y = 0;
bool b2 = double.TryParse(arg2, out y);
if (b2 == false)
{
Console.WriteLine("Input error!");
return;
}
double z = x + y;
Console.WriteLine("{0}+{1}={2}",x,y,z);
Console.ReadKey();
}
}
}
案例2:
using System;
using System.Collections.Generic;
using System.Timers;
using System.Runtime.InteropServices;
using System.Collections;
//using System.Windows.Forms;
namespace NamespaceEncasement
{
class Program
{
static void Main(string[] args)
{
double x = 0;
bool b = DoubleParser.TryParse("7920",out x);
if (b == true)
{
Console.WriteLine(x+1);
}
Console.ReadKey();
}
}
class DoubleParser
{
public static bool TryParse(string input, out double result)
{
try
{
result = double.Parse(input);
return true;
}
catch
{
result = 0;
return false;
}
}
}
}
引用类型输出参数
using System;
using System.Collections.Generic;
using System.Timers;
using System.Runtime.InteropServices;
using System.Collections;
//using System.Windows.Forms;
namespace NamespaceEncasement
{
class Program
{
static void Main(string[] args)
{
Student stu = null;
bool b = StudentFactory.Create("Tim",50,out stu);
if (b==true)
{
Console.WriteLine("Student{0},age is{1}",stu.Name,stu.Age);
}
Console.ReadKey();
}
}
class Student
{
public int Age { get; set; }
public string Name { get; set; }
}
class StudentFactory
{
public static bool Create(string stuName, int stuAge,out Student result)
{
result = null;
if (string.IsNullOrEmpty(stuName))
{
return false;
}
if (stuAge<20||stuAge>80)
{
return false;
}
47== result = new Student() { Name = stuName, Age=stuAge};
return true;
}
}
}
数组参数
案例一:
案例二:
两个案例的结果是一样的,只不过,第二个用params修饰后,可以不声明数组,直接在方法内输入数组。
WriteLine
里面的重载就有params
分割参数
具名参数
不具名调用
方法的形参是什么类型,什么顺序,那么调用时的实参要一一对应。
具名参数
using System;
using System.Collections.Generic;
using System.Timers;
using System.Runtime.InteropServices;
using System.Collections;
//using System.Windows.Forms;
namespace NamespaceEncasement
{
class Program
{
static void Main(string[] args)
{
PrintInfo(name: "Tim", age: 40);
Console.ReadKey();
}
static void PrintInfo (string name,int age )
{
Console.WriteLine("Hello{0},you are {1}",name,age);
}
}
}
具名参数优点:
- 提高代码可读性,可以一眼看出参数是什么
- 参数顺序不受方法形参顺序约束
具名参数应该属于参数的使用方法,不是种类。
可选参数
using System;
using System.Collections.Generic;
using System.Timers;
using System.Runtime.InteropServices;
using System.Collections;
//using System.Windows.Forms;
namespace NamespaceEncasement
{
class Program
{
static void Main(string[] args)
{
PrintInfo();
Console.ReadKey();
}
static void PrintInfo (string name="Tim",int age=20 )
{
Console.WriteLine("Hello{0},you are {1}",name,age);
}
}
}
打印结果为
HelloTim,you are 20
扩展方法
using System;
using System.Collections.Generic;
using System.Timers;
using System.Runtime.InteropServices;
using System.Collections;
//using System.Windows.Forms;
namespace NamespaceEncasement
{
class Program
{
static void Main(string[] args)
{
var x = 3.14159;
var y = Math.Round(x, 4);//精确小数位数
Console.WriteLine(y);
Console.ReadKey();
}
}
}
打印结果改为:3.1416
double类型是没有Round方法的,只能调用Math,还可以使用扩展方法,扩展方法必须放在静态类里面。
using System;
using System.Collections.Generic;
using System.Timers;
using System.Runtime.InteropServices;
using System.Collections;
//using System.Windows.Forms;
namespace NamespaceEncasement
{
class Program
{
static void Main(string[] args)
{
var x = 3.14159;
var y =x.Round(4);//注意者接收了一个参数,第一个参数已经在.操作符前传入
Console.WriteLine(y);
Console.ReadKey();
}
}
static class DoubleExtention
{
public static double Round(this double input, int digits)//注意this关键字
{
double result = Math.Round(input, digits);
return result;
}
}
}
linq语句也可以用扩展方法
linq要引用using System.Linq;
using System;
using System.Collections.Generic;
using System.Timers;
using System.Runtime.InteropServices;
using System.Collections;
using System.Linq;
//using System.Windows.Forms;
namespace NamespaceEncasement
{
class Program
{
static void Main(string[] args)
{
List<int> OneList = new List<int>() {14,15,16};
bool result = Test(OneList);
Console.WriteLine(result);
Console.ReadKey();
}
static bool Test(List<int>intList)
{
foreach (var item in intList)
{
if (item<=10)
{
return false;
}
}
return true;
}
}
}
注意13行的变化
using System;
using System.Collections.Generic;
using System.Timers;
using System.Runtime.InteropServices;
using System.Collections;
using System.Linq;
//using System.Windows.Forms;
namespace NamespaceEncasement
{
class Program
{
static void Main(string[] args)
{
List<int> OneList = new List<int>() { 14,15,16};
bool result = OneList.All(x=>x>3);
Console.WriteLine(result);
Console.ReadKey();
}
static bool Test(List<int>intList)
{
foreach (var item in intList)
{
if (item<=10)
{
return false;
}
}
return true;
}
}
}
第十三行的All()就是扩展方法,是存在IEnumerable中的一个扩展方法