using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public struct student
{
public string name;
public int score;
}
public class Program
{
public static void fun(student pst)
{
pst.name = "";
pst.score = 90;
}
static void Main(string[] args)
{
student mystudent = new student() { name = "",score = 50};
Program.fun( mystudent);
student _student = mystudent;
}
}
}
这个是未加ref关键字的,mystudent的结果就是初始值,fun()只是一份拷贝,不会影响到最后的mystudent的值。
//
public static void fun( ref student pst)
{
pst.name = "";
pst.score = 90;
}
static void Main(string[] args)
{
student mystudent = new student() { name = "",score = 50};
Program.fun(ref mystudent);
student _student = mystudent;
}
以上加ref之后,传递的就是一个引用(只有一份),值也会经过fun()计算而改变。
mystudent的结果就是fun()处理过的值。