C#——ValueTranser Or ReferenceTranser
程序员文章站
2024-02-11 19:31:10
...
C#——ValueTranser Or ReferenceTranser
- ValueTpyeTranserzByValue
- ReferenceTypeTranserByValue
- ValueTypeTranserByReference
- ReferenceTypeTranserByReference
Code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ReferenceAndValue : MonoBehaviour
{
struct Student
{
public string Name { get; set; }
}
void Start()
{
CommonUser2();
}
void CommonUse()
{
int oriNum = 5;
ValueTpyeTranserzByValue(oriNum);
Debug.LogFormat("After ValueTypeTranser()——oriNum={0},{1}", oriNum, "\n");
Student person = new Student() { Name = "OriginName" };
ChangeName(person);
Debug.LogFormat("After ChangeName(person)—{0}", person.Name);
Student person2 = new Student() { Name = "OriginName2" };
ChangePerson(person2);
Debug.LogFormat("After ChangePerson(person)—{0},{1}", person2.Name, "\n");
int oriRefNum = 3;
ValueTypeTranserByReference(ref oriRefNum);
Debug.LogFormat("After ValueTypeTranserByReference()——oriRefNum={0},{1}", oriRefNum, "\n");
int oriOutNum = 8;
ValueTypeTranserByReference2(out oriOutNum);
Debug.LogFormat("After ValueTypeTranserByReference2()——oriRefNum={0},{1}", oriOutNum, "\n");
Student refPerson = new Student() { Name = "OriRefName" };
RefChangeName(ref refPerson);
Debug.LogFormat("After RefChangeName(ref refPerson)—{0}", refPerson.Name);
Student refPerson2 = new Student() { Name = "OriRefName2" };
RefChangePerson(ref refPerson2);
Debug.LogFormat("After RefChangePerson(ref refPerson)—{0}", refPerson2.Name);
}
void CommonUser2()
{
#region ReferenceTypeTranserByValueOr
List<string> strList = new List<string>() { "111", "222", "333" };
List<string> strList2 = new List<string>();
strList2 = strList;
Debug.LogFormat("Before strlist.clear(): strList2 Cout={0}", strList2.Count);
strList.Clear();
Debug.LogFormat("After strlist.clear(): strList2 Cout={0}", strList2.Count);
#endregion
#region ValueTpyeTranserzByValue
int intNum = 7;
int intNum2;
intNum2 = intNum;
Debug.LogFormat("Before intNum=0: intNum2={0}", intNum2);
intNum = 0;
Debug.LogFormat("After intNum=0: intNum2={0}", intNum2);
#endregion
}
#region ValueTpyeTranserzByValue
void ValueTpyeTranserzByValue(int num)
{
var tempNum = num;
tempNum += tempNum;
}
#endregion
#region ReferenceTypeTranserByValue
void ChangePerson(Student person1)
{
person1 = new Student();
person1.Name = "Change Name";
Debug.LogFormat("ChangePerson—{0}", person1.Name);
}
void ChangeName(Student person2)
{
person2.Name = "Change Name";
Debug.LogFormat("ChangeName—{0}", person2.Name);
}
#endregion
#region ValueTypeTranserByReference
/// <summary>
/// ref must assin a value out of function
/// </summary>
/// <param name="num"></param>
void ValueTypeTranserByReference(ref int num)
{
num += num;
}
/// <summary>
/// out must assin a value in function
/// </summary>
/// <param name="num"></param>
void ValueTypeTranserByReference2(out int num)
{
num = 99;
}
#endregion
#region ReferenceTypeTranserByReference
void RefChangePerson(ref Student person3)
{
person3 = new Student();
person3.Name = "Change Name";
Debug.LogFormat("RefChangePerson—{0}", person3.Name);
}
void RefChangeName(ref Student person4)
{
person4.Name = "Change Name";
Debug.LogFormat("RefChangeName—{0}", person4.Name);
}
#endregion
}
Result
上一篇: Vue生命周期
下一篇: 人人网怎么删除好友 人人网删除好友教程