我在下面的代碼片段中總結了我的問題。值類型和參考類型
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication13
{
public class Student
{
public int Marks { get; set; }
public Student(int marks)
{
this.Marks = marks;
}
public void AssignMarks(Student st)
{
st = null;
}
public void AssignMarks(ref Student st)
{
st = null;
}
}
class Program
{
static void Main(string[] args)
{
Student st = new Student(50);
st.AssignMarks(st);
Console.WriteLine(st.Marks);
Student st1 = new Student(50);
st.AssignMarks(ref st1); // NullReferenceException
Console.WriteLine(st1.Marks);
}
}
}
爲什麼我上標有**
好吧,異常的類型可能有所幫助 – BlackICE 2010-12-08 15:35:48
我編輯了我的問題。我得到NullReferenceException – 2010-12-08 15:38:15