DataRow and Protected Internal Constructor

How can a DataTable create a new DataRow instance using the NewRow method if the constructor of the DataRow class is protected internally and the DataTable does not 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();
    }
}

      

+3


source to share


2 answers


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 it DataTable

has access to all DataRow

internal members.



+2




I hope you already have an answer to this.

But still I am adding my answer to this to address "Why is it built this way".

Like the "hvd" mentioned , they are in the same assembly, so the DataTable is able to instantiate the DataRow.

The reason for this approach:

• The data row contains values ​​for each column • Ideally, an array built in to store these values



• Thus, each row of data contains an array that contains values

• But the data line will not know the size of the array for initialization

• What depends on the number of columns in the data table

• But the data table knows how many columns are in the table

• This is why it takes responsibility for the creation OR sets the size of the DataRow array

0


source







All Articles