C# 基本数据类型(除string)都是值类型,类、数组、List<T>是引用类型。
参数传递:
既可以通过值传递也可以通过引用传递参数。通过引用传递参数允许函数成员(方法、属性、索引器、运算符和构造函数)更改参数的值,并保持该更改。
值传递:传递的是值类型 或者 引用类型的副本;
一、值类型的值传递和引用传递:
class Program值类型
{
static void Main值类型(string[] args)//值类型
{
Console.WriteLine("值类型");
int a = 10;
Console.WriteLine(a);//10
fun_int(a);
Console.WriteLine(a);//10
fun_int(ref a);
Console.WriteLine(a);//12
Console.ReadKey();
}
static void fun_int(int a)
{
a += 2;
Console.WriteLine("值传递:" + a);//12
}
static void fun_int(ref int a)
{
a += 2;
Console.WriteLine("引用传递:" + a);//12
}
}
二、引用类型的值传递和引用传递:
class Program引用类型
{
static void Main引用类型(string[] args)//引用类型
{
Console.WriteLine("引用类型");
Console.WriteLine("String");
string aa = "000";
Console.WriteLine(aa);//000
fun1(aa);
Console.WriteLine(aa);//000
fun2(ref aa);
Console.WriteLine(aa);//222
Console.ReadKey();
Console.WriteLine("Object");
Student xiaoming = new Student() { name = "小明", sex = "男", age = 10 };
Console.WriteLine(xiaoming.ToString());//小明 男 10岁
FunStudent(xiaoming);
Console.WriteLine(xiaoming.ToString());//小刚 男 12岁
FunStudent(ref xiaoming);
Console.WriteLine(xiaoming.ToString());//小芳 女 12岁
Console.ReadKey();
}
#region String
static void fun1(string aa)
{
aa = "111";
Console.WriteLine("值传递:" + aa);//111
}
static void fun2(ref string aa)
{
aa = "222";
Console.WriteLine("引用传递:" + aa);//222
}
#endregion
#region Object
class Student
{
public string name { get; set; }
public string sex { get; set; }
public int age { get; set; }
public override string ToString()
{
return String.Format("{0} {1} {2}岁", name, sex, age);
}
}
static void FunStudent(Student aa)
{
aa.name = "小刚";
aa.age = 12;
Console.WriteLine("值传递:" + aa.ToString());//小刚 男 12岁
}
static void FunStudent(ref Student aa)
{
aa.name = "小花";
aa = new Student() { name = "小芳", sex = "女", age = 12 };
Console.WriteLine("引用传递:" + aa.ToString());//小芳 女 12岁
}
#endregion
}
三、数组 的值传递和引用传递: (规律与类的值传递和引用传递 完全一致)
class Program数组
{
static void Main数组()//数组
{
int[] arr = { 1, 4, 5 };
System.Console.WriteLine("Main, the first element is: {0}", arr[0]);//1
Change(arr);
System.Console.WriteLine("Main, the first element is: {0}", arr[0]);//888
Change(ref arr);
System.Console.WriteLine("Main, the first element is: {0}", arr[0]);//-3
Console.ReadKey();
}
static void Change(ref int[] pArray)
{
pArray[0] = 888; // This change affects the original element.
pArray = new int[5] { -3, -1, -2, -3, -4 }; // This change is local.
System.Console.WriteLine("引用传递:Inside, the first element is: {0}", pArray[0]);//-3
}
static void Change(int[] pArray)
{
pArray[0] = 888; // This change affects the original element.
pArray = new int[5] { -3, -1, -2, -3, -4 }; // This change is local.
System.Console.WriteLine("值传递:Inside, the first element is: {0}", pArray[0]);//-3
}
}
class ProgramString
{
/// <summary>
/// https://www.cnblogs.com/smileberry/p/9914764.html
/// </summary>
static void MainString()//String
{
string s1 = "aaaa";
string s2 = s1;
System.Console.WriteLine("s1={0},s2={1}", s1, s2);
Console.WriteLine(Object.ReferenceEquals(s1, s2) ? "同一个" : "不是同一个");
Console.WriteLine();
change(s1, s2);
System.Console.WriteLine("s1={0},s2={1}", s1, s2);
Console.WriteLine(Object.ReferenceEquals(s1, s2) ? "同一个" : "不是同一个");
Console.WriteLine();
change(ref s1, s2);
System.Console.WriteLine("s1={0},s2={1}", s1, s2);
Console.WriteLine(Object.ReferenceEquals(s1, s2) ? "同一个" : "不是同一个");
//s1 = "bbbb";
//System.Console.WriteLine("s1={0},s2={1}", s1, s2);
//Console.WriteLine(Object.ReferenceEquals(s1, s2) ? "同一个" : "不是同一个");
Console.ReadKey();
}
static void change(string s1, string s2)
{
s1 = "bbbb";
System.Console.WriteLine("值传递:s1={0},s2={1}", s1, s2);
}
static void change(ref string s1, string s2)
{
s1 = "bbbb";
System.Console.WriteLine("引用传递:s1={0},s2={1}", s1, s2);
}
}