傳值參數(shù)
1壮韭、值參數(shù)創(chuàng)建變量副本
2纹因、對值參數(shù)的操作永遠不影響變量的值
傳值參數(shù)——值類型
傳值參數(shù)——值類型
class Program
{
static void Main(string[] args)
{
int i = 5;
Function(i);
Console.WriteLine(i);
}
static void Function(int a)
{
a++;
Console.WriteLine(a);
}
}
結(jié)果
傳值參數(shù)——引用類型:創(chuàng)建新對象
傳值參數(shù)——引用類型:創(chuàng)建新對象
class Program
{
static void Main(string[] args)
{
Student student1 = new Student() { Name="Tom"};
Function(student1);
Console.WriteLine($"{student1.Name}");
}
static void Function(Student stu)
{
stu = new Student() {Name="Tim" };
Console.WriteLine($"{stu.Name}");
}
}
class Student
{
public string Name;
}
輸出結(jié)果
參數(shù)傳遞——引用類型:只操作對象,不創(chuàng)建新對象
參數(shù)傳遞——引用類型:只操作對象是牢,不創(chuàng)建新對象
class Program
{
static void Main(string[] args)
{
Student student1 = new Student() { Name="Tom"};
Function(student1);
Console.WriteLine($"{student1.Name}");
}
static void Function(Student stu)
{
stu.Name = "Tim";
Console.WriteLine(stu.Name);
}
}
class Student
{
public string Name;
}
運行結(jié)果
引用參數(shù)
1驳棱、引用類型并不創(chuàng)建變量的副本
2农曲、使用ref修飾符顯式指出——此方法的副作用是改變實際參數(shù)的值
引用參數(shù)——值類型
引用參數(shù)——值類型
class Program
{
static void Main(string[] args)
{
int i = 5;
Function(ref i);
Console.WriteLine(i);
}
static void Function(ref int a)
{
a++;
Console.WriteLine(a);
}
}
結(jié)果
引用參數(shù)——引用類型:創(chuàng)建新對象
引用參數(shù)——引用類型:創(chuàng)建新對象
class Program
{
static void Main(string[] args)
{
Student student1 = new Student() { Name="Tom"};
Console.WriteLine($"{student1.Name}");
Function(ref student1);
Console.WriteLine($"{student1.Name}");
}
static void Function(ref Student stu)
{
stu = new Student();
stu.Name = "Tim";
Console.WriteLine(stu.Name);
}
}
class Student
{
public string Name;
}
結(jié)果
引用參數(shù)——引用類型:不創(chuàng)建新對象
引用參數(shù)——引用類型:不創(chuàng)建新對象
class Program
{
static void Main(string[] args)
{
Student student1 = new Student() { Name="Tom"};
Console.WriteLine($"{student1.Name}");
Function(ref student1);
Console.WriteLine($"{student1.Name}");
}
static void Function(ref Student stu)
{
stu.Name = "Tim";
Console.WriteLine(stu.Name);
}
}
class Student
{
public string Name;
}
結(jié)果
輸出參數(shù)
1暮的、輸出菜蔬并不創(chuàng)建變量的副本
2、方法體內(nèi)必須要有對輸出變量賦值的操作
3猖腕、使用out修飾符顯式指出——此方法的副作用是通過參數(shù)向外輸出值
4恨闪、從語義來講——ref是為了改變,out是為了輸出
輸出參數(shù)——值類型
輸出參數(shù)——值類型
class Program
{
static void Main(string[] args)
{
double x = 0;
bool b = DoubleParser.TryParse("123",out x);
if (b == true)
{
Console.WriteLine(x);
}
else {
Console.WriteLine(x);
}
}
}
class DoubleParser
{
public static bool TryParse(string input,out double result)
{
try
{
result = double.Parse(input);
return true;
}
catch
{
result = 0;
return false;
}
}
}
結(jié)果
輸出參數(shù)——引用類型
輸出參數(shù)——引用類型
class Program
{
static void Main(string[] args)
{
Student student = null;
bool b = StudentFactory.Creat("Tom", 20, out student);
if (b == true)
{
Console.WriteLine($"student.Nmae={student.Name},student.Age={student.Age}");
}
}
}
class Student
{
public string Name { get; set; }
public int Age { get; set; }
}
class StudentFactory
{
public static bool Creat(string name, int age, out Student student)
{
student = null;
if (string.IsNullOrEmpty(name))
{
return false;
}
if (age > 100 && age < 0)
{
return false;
}
student = new Student() { Name = name, Age = age };
return true;
}
}
結(jié)果
數(shù)組參數(shù)
必需是形參列表中的最后一個參數(shù)蜡豹,有params修飾
Sting.Fromat和Sting.split方法就是用到了數(shù)組參數(shù)
具名參數(shù)
參數(shù)的位置不受約束
class Program
{
static void Main(string[] args)
{
Function(name: "Tom",age:20);
Function(age: 20,name: "Tom" );
}
static void Function(string name, int age)
{
Console.WriteLine($"Name={name},Age={age}");
}
}
可選參數(shù)
參數(shù)因為具有默認值而變得“可選”
class Program
{
static void Main(string[] args)
{
Function();
}
static void Function(string name="Tom", int age=20)
{
Console.WriteLine($"Name={name},Age={age}");
}
}
擴展方法(this參數(shù))
1余素、擴展方法必須是共有炊昆,靜態(tài)的
2、必須是參數(shù)列表中的第一個凤巨,由this修飾
3敢茁、必須由一個靜態(tài)類(一般名為SomeTypeExtent)來統(tǒng)一收納對Some Type類型的擴展方法
class Program
{
static void Main(string[] args)
{
double x = 3.1415926;
double y = x.round(5);
Console.WriteLine(y);
}
}
static class DoubleExtansion
{
static public double round(this double input ,int digits)
{
double result = Math.Round(input, digits);
return result;
}
}
各種參數(shù)的使用場景