How can a DataTable create a new instance of a DataRow with the NewRow method if the constructor of the DataRow class is protected internal and DataTable doesn't inherit from DataRow?
Example:
class Program
{
static void Main()
{
// error: inaccessible due to its protection level
DataRow dr = new DataRow();
// works
DataRow dr = new DataTable().NewRow();
}
}
protected internal
means "accessible by derived classes" and "accessible by other classes in the same assembly". DataTable
and DataRow
are in the same assembly, so DataTable
has access to all of DataRow
's internal members.
protected internal
means what I said it means. See http://msdn.microsoft.com/en-us/library/ms173121(v=vs.100).asp - NoName 2012-04-04 21:27
Hope you already got an answer for this.
But still I am adding my answer to this to address “Why it is designed in this way”.
As “hvd” mentioned they are in same assembly that’s why DataTable able to create instance of DataRow.
The reason for this approach is:
• Data row contains values for each column • Ideally an array internally used to store these values
• So each data row contains array which contains values
• But data row will not be knowing the size of the array to initialize
• Which depends on number of columns in the data table
• But data table knows how many columns in the table
• That’s why it takes responsibility to create OR set the array size of the DataRow