值参数
class Program
{
static void Main(string[] args)
{
Student stu = new Student() { Name = "Bob" };
SomeMethod(stu);
Console.WriteLine("{0}:{1}",stu.Name,stu.GetHashCode());
}
static void SomeMethod(Student stu)
{
stu = new Student() { Name = "Tom" };
Console.WriteLine("{0}:{1}", stu.Name, stu.GetHashCode());
}
}
class Student
{
public string Name { get; set; }
}
创建了新的参数(副本)
引用参数
ref
值类型
static void Main(string[] args)
{
int y = 1;
IWantSideEffect(ref y);
Console.WriteLine(y);
}
static void IWantSideEffect(ref int x)
{
x += 100;
}
输出:101
引用类型
class Program
{
static void Main(string[] args)
{
Student outterStu = new Student() { Name = "Bob" };
Console.WriteLine("HashCode:{0},Name:{1}", outterStu.GetHashCode(), outterStu.Name);
Console.WriteLine("--------------------------------------------");
IWantEffect(ref outterStu);
Console.WriteLine("HashCode:{0},Name:{1}", outterStu.GetHashCode(), outterStu.Name);
}
static void IWantEffect(ref Student stu)
{
stu = new Student() { Name = "Tom" };
Console.WriteLine("HashCode:{0},Name:{1}", stu.GetHashCode(), stu.Name);
}
}
class Student
{
public string Name { get; set; }
}
不创建新对象只改变对象本身
只改变属性不改变对象本身
// 同上
static void IWantEffect(ref Student stu)
{
stu.Name = "Tom"; // 改变
Console.WriteLine("HashCode:{0},Name:{1}", stu.GetHashCode(), stu.Name);
}
输出参数
out
使用输出参数:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please input 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 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);
}
}
自己实现的Double.TryParse:
class Program
{
static void Main(string[] args)
{
double x = 0;
bool b = DoubleParser.TryParse("123", out x);
if (b==true)
{
Console.WriteLine(x+1);
}
}
}
class DoubleParser
{
public static bool TryParse(string input, out double result)
{
try
{
result = double.Parse(input);
return true;
}
catch
{
result = 0; // 输出参数必须赋值
return false;
}
}
}
引用类型的输出参数
class Program
{
static void Main(string[] args)
{
Student stu = null;
bool b = StudentFactory.Create("Tim", 34, out stu);
if (b==true)
{
Console.WriteLine("Student {0}'s age is {1}.", stu.Name, stu.Age);
}
}
}
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;
}
result = new Student() { Age = stuAge, Name = stuName };
return true;
}
}
数组参数
params 必须是参数列表的最后一个
class Program
{
static void Main(string[] args)
{
//int[] myIntArray = new int[] { 1, 2, 3 };
//int result = CalculateSum(myIntArray);
int result = CalculateSum(1, 2, 3); // 使用params参数直接输入数组元素即可,不必再声明新数组
Console.WriteLine(result) ;
}
static int CalculateSum(params int[] intArray)
{
int sum = 0;
foreach (var item in intArray)
{
sum += item;
}
return sum;
}
}
另一个例子:String.Split方法
static void Main(string[] args)
{
string str = "Tim,Tom;Amy.Lisa";
string[] result = str.Split(';', '.', ',');
foreach (var item in result)
{
Console.WriteLine(item);
}
}
具名参数
class Program
{
static void Main(string[] args)
{
// PrintInfo("Tim", 34); // 不具名
PrintInfo(age: 34, name: "Tim"); // 具名 参数直观且位置可变
}
static void PrintInfo(string name, int age)
{
Console.WriteLine("Hello {0}, you are {1}", name, age);
}
}
可选参数
class Program
{
static void Main(string[] args)
{
PrintInfo(); // 方法中定义过默认值后参数可不写
}
static void PrintInfo(string name = "Tim", int age = 34)
{
Console.WriteLine("Hello {0}, you are {1}", name, age);
}
}
扩展方法(this参数)
class Program
{
static void Main(string[] args)
{
double x = 3.1415926;
//double y = Math.Round(x, 4);
double y = x.Round(4); // 为目标数据类型追加方法
Console.WriteLine(y);
}
}
static class DoubleExtension // 必须由静态类收纳
{
public static double Round(this double input, int digits) // 添加this参数
{
double result = Math.Round(input, digits);
return result;
}
}
举例:LINQ方法
不用LINQ:
class Program
{
static void Main(string[] args)
{
List<int> myList = new List<int> { 11, 13, 24, 35, 45 };
bool reault = AllGreaterThanTen(myList);
Console.WriteLine(reault);
}
static bool AllGreaterThanTen(List<int> intList)
{
foreach (var item in intList)
{
if (item<=10)
{
return false;
}
}
return true;
}
}
使用LINQ方法:
static void Main(string[] args)
{
List<int> myList = new List<int> { 11, 13, 24, 35, 45 };
bool reault = myList.All(i => i > 10);
Console.WriteLine(reault);
}