值参数
声明时不带修饰符的形参叫做值参数,一个值参数对应一个局部变量。
值参数的初始值来自该方法调用时所提供的实参的值。
GetHashCode()方法,返回代表对象的唯一编码。
一.传值参数
1.值类型传值参数
创建副本,不直接操作值。
class LearnArgs
{
static void Main(string[] args)
{
int num = 200;
Console.WriteLine("in main {0} and HashCode {1}", num, num.GetHashCode());
ChangeNum(num);
Console.WriteLine("in main after method {0} and HashCode {1}", num, num.GetHashCode());
}
static void ChangeNum(int num)
{
num = 100;
Console.WriteLine("in method {0} and HashCode {1}",num, num.GetHashCode());
}
}
2.引用类型传值参数
①方法体内创建新对象,不改变传入对象的状态
class LearnArgs
{
static void Main(string[] args)
{
; Student stu = new Student() { name = "SpongeBob" };
Console.WriteLine("in main name {0} and HashCode {1}", stu.name, stu.GetHashCode());
GrowStudent(stu);
Console.WriteLine("in main after method name {0} and HashCode {1}", stu.name, stu.GetHashCode());
}
static void GrowStudent(Student stu)
{
stu = new Student() { name = "Eilenial" };
Console.WriteLine("in method name {0} and HashCode {1}", stu.name, stu.GetHashCode());
}
}
其输出为:
②方法体内不创建新对象,直接操作传入对象
class LearnArgs
{
static void Main(string[] args)
{
; Student stu = new Student() { name = "SpongeBob" };
Console.WriteLine("in main name {0} and HashCode {1}", stu.name, stu.GetHashCode());
ChangeStudent(stu);
Console.WriteLine("in main after method name {0} and HashCode {1}", stu.name, stu.GetHashCode());
}
static void ChangeStudent(Student stu)
{
stu.name = "Eilenial";
Console.WriteLine("in method name {0} and HashCode {1}", stu.name, stu.GetHashCode());
}
}
其输出为:
二.引用参数(ref 修饰)
*注:引用参数在方法声明与调用时都必须用ref修饰
引用参数本质上引用的是对象的指针(即在内存中的存储地址)
1.值类型
class LearnArgs
{
static void Main(string[] args)
{
int x = 0;
Console.WriteLine("in main x = {0} and HashCode {1}", x, x.GetHashCode());
AddOne(ref x);
Console.WriteLine("in main after method x = {0} and HashCode {1}", x, x.GetHashCode());
}
static void AddOne(ref int x)
{
x++;
Console.WriteLine("in method x = {0} and HashCode {1}", x, x.GetHashCode());
}
}
其输出为:
2.引用类型
①方法体内创建新对象,对新对象进行操作后将新对象的值赋给传入的参数
class LearnArgs
{
static void Main(string[] args)
{
Student oldStu = new Student() { name = "SpongeBob" };
Console.WriteLine("in main name {0} and HashCode {1}", oldStu.name, oldStu.GetHashCode());
ChangeStudent(ref oldStu);
Console.WriteLine("in main after method name {0} and HashCode {1}", oldStu.name, oldStu.GetHashCode());
}
static void ChangeStudent(ref Student stu)
{
stu = new Student() { name = "Eilenial" };
Console.WriteLine("in method name {0} and HashCode {1}", stu.name, stu.GetHashCode());
}
}
其输出为:
②方法体内不创建新对象,直接操作传入的对象
class LearnArgs
{
static void Main(string[] args)
{
Student oldStu = new Student() { name = "SpongeBob" };
Console.WriteLine("in main name {0} and HashCode {1}", oldStu.name, oldStu.GetHashCode());
ChangeStudent(ref oldStu);
Console.WriteLine("in main after method name {0} and HashCode {1}", oldStu.name, oldStu.GetHashCode());
}
static void ChangeStudent(ref Student stu)
{
stu.name = "Eilenial";
Console.WriteLine("in method name {0} and HashCode {1}", stu.name, stu.GetHashCode());
}
}
其输出为:
三.输出参数(out修饰)
*注:输出参数在方法声明与调用时都必须用out修饰
C#中并不要求调用时一定要初始out参数,但在方法体内一定要初始化。
①值类型
class LearnArgs
{
static void Main(string[] args)
{
string x = Console.ReadLine();
double y = 0;
if (StringToDouble2(x, out y))
{
Console.WriteLine("Y is {0}",y);
}
else
{
return;
}
}
static bool StringToDouble2(string x, out double result)
{
try
{
result = Math.Round(double.Parse(x), 2);
return true;
}
catch
{
result = 0;
throw new Exception("Enter something wrong!");
//return false; 可以省略,抛出异常的同时方法执行中断了
}
}
}
②输出类型
class LearnArgs
{
static void Main(string[] args)
{
Student stu = new Student();
Console.WriteLine("Enter name: ");
string name = Console.ReadLine();
Console.WriteLine("Enter age: ");
int age = int.Parse(Console.ReadLine());
if (CreateStudent(name, age, out stu)) //调用方法
{
Console.WriteLine("Student {0}'s age is {1}", stu.name, stu.age);
}
else
{
Console.WriteLine("Enter something wrong.");
return;
}
}
//输出参数一般会与返回boolean的方法一起使用,起到判断传入参数是否符合规定,符合后再经过操作输出
static bool CreateStudent(string stuName, int stuAge,out Student stu)
{
stu = null;
if (!string.IsNullOrEmpty(stuName))//尽早return,提高代码的可读性
return false;
if (stuAge < 10 && stuAge > 60)
return false;
stu = new Student() { name = stuName, age = stuAge };
return true;
}
}
class Student
{
public string name { get; set; }
public int age { get; set; }
}
四.数组参数(params修饰)
数组参数必须是形参列表中的最后一个参数且只能有1个。
class LearnArgs
{
static void Main(string[] args)
{
int result = AddIntArray(2, 2, 3, 4);
//第一个2对应方法的形参 index,后面值的则被归纳进形参arr这个整型数组中
Console.WriteLine(result);
string name = "Tim,Willen.Hoolen!Ricky";
string[] nameList = name.Split(',', '.', '!');
//string.Split()方法也是一个常用的用到数组参数的方法
foreach (var n in nameList)
{
Console.WriteLine(n);
}
}
static int AddIntArray(int index, params int[] arr)
{
int result = 0;
for (int i = 0; i < index; i++)
{
result += arr[i];
}
return result;
}
}
五.具名参数
参数位置不再受约束(调用方法时使用)
class LearnArgs
{
static void Main(string[] args)
{
ShowSomething(name: "HallenWill", age: 52);
//此处用具名参数,调用方法时并没有遵照方法规定的参数顺序
}
static void ShowSomething(int age, string name)
{
Console.WriteLine("{0} and {1}",age,name);
}
}
六.可选参数
因参数有默认值变得可选(一般不使用,这样会造成代码可读性的减少)
*注:可选参数必须出现在所有的必须参数后
class LearnArgs
{
static void Main(string[] args)
{
ShowSomething();
ShowSomething(15,"Haaa");
ShowSomethingOther(10);
ShowSomethingOther(10,"Haaa");
}
static void ShowSomething(int age = 0, string name = "NoName")
{
Console.WriteLine("{0} and {1}",age,name);
}
static void ShowSomethingOther(int age, string name = "NoName")
{
Console.WriteLine("{0} and {1}", age, name);
}
}
七.扩展方法(this参数)
- 方法必须是公有的,静态的(public static修饰)
- 必须为形参列表的第一个,被this修饰
- 必须由一个静态类(static class [SomeType]Extension)来统一收纳SomeType类型的扩展方法
class LearnArgs
{
static void Main(string[] args)
{
double x = 1.23456;
double y = x.Round(2);
Console.WriteLine(y);
}
}
static class DoubleExtension
{
public static double Round(this double input, int digital)
{
return Math.Round(input, digital);
}
}
LINQ方法 - Language Integrated Querry语言集成查询方法
using System.Linq;
class KnowLINQ
{
static void Main(string[] args)
{
int[] myArray = { 1, 2, 3, 4, 5 };
bool result = myArray.All(i => i < 10);
Console.WriteLine(result);
}
}