在很多應用中都需要實現一種類,只能自己實例化,而其他使用者只能使用而不能實例化,如DataRow這個類,它只能使用DataTable的NewRow()方法獲取DataRow的實例,而不能使用new DataRow來實例化.其實這個實現十分簡單,只需要申明構造函數的訪問限制爲internal,因爲internal是表示只能在所在的程序集中訪問,在其他的程序集中不能訪問,因此,其他使用者不能調用構造函數來實例化.
如下是MSDN中給出的DataRow的構造函數
protected internal DataRow( DataRowBuilder builder )